From 7404838a8372db8f6cfa85235f6172fba0b3288a Mon Sep 17 00:00:00 2001 From: Aleksey Savchuk Date: Wed, 16 Oct 2024 18:17:02 +0300 Subject: [PATCH] [#287] client: Fix error handling after dialing Before, when error was presented and neither `Canceled` nor `DeadlineExceeded`, that method returned no error on failed dial. Signed-off-by: Aleksey Savchuk --- client/client.go | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/client/client.go b/client/client.go index 10eb018..ff573e5 100644 --- a/client/client.go +++ b/client/client.go @@ -106,26 +106,28 @@ func (c *Client) Dial(ctx context.Context, prm PrmDial) error { _, err := rpc.Balance(&c.c, new(v2accounting.BalanceRequest), client.WithContext(ctx), ) - if err != nil { - var ctxErr error - // return context errors since they signal about dial problem - if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { - ctxErr = err - } else if st, ok := status.FromError(err); ok && st.Code() == codes.Canceled { - ctxErr = context.Canceled - } else if ok && st.Code() == codes.DeadlineExceeded { - ctxErr = context.DeadlineExceeded - } - if ctxErr != nil { - if conn := c.c.Conn(); conn != nil { - _ = conn.Close() - } - return ctxErr - } + if err == nil { + return nil } - return nil + var ctxErr error + + switch st, ok := status.FromError(err); { + case errors.Is(err, context.Canceled) || (ok && st.Code() == codes.Canceled): + ctxErr = context.Canceled + case errors.Is(err, context.DeadlineExceeded) || (ok && st.Code() == codes.DeadlineExceeded): + ctxErr = context.DeadlineExceeded + default: + } + + if ctxErr == nil { + return err + } + if conn := c.c.Conn(); conn != nil { + _ = conn.Close() + } + return ctxErr } // sets underlying provider of frostFSAPIServer. The method is used for testing as an approach