From e4aa082cdbeb02d5beac1972b76d35bb7c2e6334 Mon Sep 17 00:00:00 2001 From: feichashao Date: Sat, 8 Jul 2023 00:12:24 +0800 Subject: [PATCH 1/2] Improve error handling for login connection issues --- cmd/ocm-backplane/login/login.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/ocm-backplane/login/login.go b/cmd/ocm-backplane/login/login.go index 1aeadd4d..2db13c67 100644 --- a/cmd/ocm-backplane/login/login.go +++ b/cmd/ocm-backplane/login/login.go @@ -50,6 +50,8 @@ var ( RunE: runLogin, SilenceUsage: true, } + + ErrLoginConnection = errors.New("unable to connect to backplane api") ) func init() { @@ -208,9 +210,7 @@ func runLogin(cmd *cobra.Command, argv []string) (err error) { // Hibernating, print an error return fmt.Errorf("cluster %s is hibernating, login failed", clusterKey) } - // Check API connection with configured proxy - err = bpConfig.CheckAPIConnection() - if err != nil { + if errors.Is(err, ErrLoginConnection) { return fmt.Errorf("cannot connect to backplane API URL, check if you need to use a proxy/VPN to access backplane: %v", err) } @@ -354,8 +354,8 @@ func doLogin(api, clusterId, accessToken string) (string, error) { if err != nil { // trying to determine the error errBody := err.Error() - if strings.Contains(errBody, "dial tcp") && strings.Contains(errBody, "i/o timeout") { - return "", fmt.Errorf("unable to connect to backplane api") + if strings.Contains(errBody, "dial tcp") || strings.Contains(errBody, "i/o timeout") { + return "", fmt.Errorf("%w: %w", ErrLoginConnection, err) } return "", err From e2dc46eb87a4624aaa3ebf2929d9a17af478f19d Mon Sep 17 00:00:00 2001 From: feichashao Date: Tue, 11 Jul 2023 16:51:15 +0800 Subject: [PATCH 2/2] change errLoginConnection to private --- cmd/ocm-backplane/login/login.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/ocm-backplane/login/login.go b/cmd/ocm-backplane/login/login.go index 2db13c67..1491a374 100644 --- a/cmd/ocm-backplane/login/login.go +++ b/cmd/ocm-backplane/login/login.go @@ -51,7 +51,7 @@ var ( SilenceUsage: true, } - ErrLoginConnection = errors.New("unable to connect to backplane api") + errLoginConnection = errors.New("unable to connect to backplane api") ) func init() { @@ -210,7 +210,7 @@ func runLogin(cmd *cobra.Command, argv []string) (err error) { // Hibernating, print an error return fmt.Errorf("cluster %s is hibernating, login failed", clusterKey) } - if errors.Is(err, ErrLoginConnection) { + if errors.Is(err, errLoginConnection) { return fmt.Errorf("cannot connect to backplane API URL, check if you need to use a proxy/VPN to access backplane: %v", err) } @@ -355,7 +355,7 @@ func doLogin(api, clusterId, accessToken string) (string, error) { // trying to determine the error errBody := err.Error() if strings.Contains(errBody, "dial tcp") || strings.Contains(errBody, "i/o timeout") { - return "", fmt.Errorf("%w: %w", ErrLoginConnection, err) + return "", fmt.Errorf("%w: %w", errLoginConnection, err) } return "", err