Skip to content

Commit 14aa81d

Browse files
committed
Moves error handling from transport to atlas.go
1 parent 5fbb105 commit 14aa81d

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

cmd/atlas/atlas.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
"github.com/AlecAivazis/survey/v2/core"
24+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/commonerrors"
2425
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/root"
2526
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
2627
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry"
@@ -36,6 +37,10 @@ func execute(rootCmd *cobra.Command) {
3637
3738
To learn more, see our documentation: https://www.mongodb.com/docs/atlas/cli/stable/connect-atlas-cli/`
3839
if cmd, err := rootCmd.ExecuteContextC(ctx); err != nil {
40+
errMsg := commonerrors.CheckHTTPError(err)
41+
if errMsg != nil {
42+
fmt.Println(errMsg)
43+
}
3944
if !telemetry.StartedTrackingCommand() {
4045
telemetry.StartTrackingCommand(cmd, os.Args[1:])
4146
}

internal/cli/commonerrors/errors.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package commonerrors
1616

1717
import (
1818
"errors"
19+
"strings"
1920

2021
"go.mongodb.org/atlas-sdk/v20250312005/admin"
2122
)
@@ -24,6 +25,9 @@ var (
2425
errClusterUnsupported = errors.New("atlas supports this command only for M10+ clusters. You can upgrade your cluster by running the 'atlas cluster upgrade' command")
2526
errOutsideVPN = errors.New("forbidden action outside access allow list, if you are a MongoDB employee double check your VPN connection")
2627
errAsymmetricShardUnsupported = errors.New("trying to run a cluster wide scaling command on an independent shard scaling cluster. Use --autoScalingMode 'independentShardScaling' instead")
28+
errUnauthorized = errors.New(`this action requires authentication
29+
30+
To log in using your Atlas username and password or to set credentials using API key, run: atlas auth login`)
2731
)
2832

2933
const (
@@ -49,6 +53,13 @@ func Check(err error) error {
4953
return err
5054
}
5155

56+
func CheckHTTPError(err error) error {
57+
if strings.Contains(err.Error(), "401") && strings.Contains(err.Error(), "Unauthorized") {
58+
return errUnauthorized
59+
}
60+
return nil
61+
}
62+
5263
func IsAsymmetricShardUnsupported(err error) bool {
5364
apiError, ok := admin.AsError(err)
5465
if !ok {

internal/cli/commonerrors/errors_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,33 @@ func TestCheck(t *testing.T) {
6767
})
6868
}
6969
}
70+
71+
func TestCheckCommonErrors(t *testing.T) {
72+
dummyErr := errors.New("dummy error")
73+
testErr := errors.New(`Error: https://cloud.mongodb.com/api/atlas/v2/groups/670e34d35a4f587387db2102/clusters GET: HTTP 401 Unauthorized (Error code: "") Detail: You are not authorized for this resource. Reason: Unauthorized. Params: []`)
74+
75+
testCases := []struct {
76+
name string
77+
err error
78+
want error
79+
}{
80+
{
81+
name: "unauthorized error",
82+
err: testErr,
83+
want: errUnauthorized,
84+
},
85+
{
86+
name: "arbitrary error",
87+
err: dummyErr,
88+
want: nil,
89+
},
90+
}
91+
92+
for _, tc := range testCases {
93+
t.Run(tc.name, func(t *testing.T) {
94+
if got := CheckHTTPError(tc.err); !errors.Is(got, tc.want) {
95+
t.Errorf("CheckHTTPError(%v) = %v, want %v", tc.err, got, tc.want)
96+
}
97+
})
98+
}
99+
}

internal/store/store.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ func (s *Store) httpClient(httpTransport http.RoundTripper) (*http.Client, error
7171

7272
return &http.Client{Transport: tr}, nil
7373
default:
74-
tr := &transport.AuthRequiredRoundTripper{Base: httpTransport}
75-
return &http.Client{Transport: tr}, nil
74+
return &http.Client{Transport: httpTransport}, nil
7675
}
7776
}
7877

internal/transport/transport.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
package transport
1616

1717
import (
18-
"fmt"
1918
"net"
2019
"net/http"
2120
"time"
2221

2322
"github.com/mongodb-forks/digest"
2423
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
2524
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/oauth"
26-
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/validate"
2725
atlasauth "go.mongodb.org/atlas/auth"
2826
)
2927

@@ -112,24 +110,3 @@ func (tr *tokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
112110

113111
return tr.base.RoundTrip(req)
114112
}
115-
116-
type AuthRequiredRoundTripper struct {
117-
Base http.RoundTripper
118-
}
119-
120-
func (tr *AuthRequiredRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
121-
resp, err := tr.Base.RoundTrip(req)
122-
if resp != nil && resp.StatusCode == http.StatusUnauthorized {
123-
return nil, fmt.Errorf(
124-
`%w
125-
126-
To log in using your Atlas username and password, run: atlas auth login
127-
To set credentials using API keys, run: atlas config init`,
128-
validate.ErrMissingCredentials,
129-
)
130-
}
131-
if err != nil {
132-
return nil, err
133-
}
134-
return resp, nil
135-
}

0 commit comments

Comments
 (0)