• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb / mongodb-atlas-cli / 26228926989

21 May 2026 01:27PM UTC coverage: 22.63% (-41.6%) from 64.198%
26228926989

push

github

apix-bot[bot]
Update compliance report for v1.55.0

8987 of 39713 relevant lines covered (22.63%)

0.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.67
/internal/cli/commonerrors/errors.go
1
// Copyright 2022 MongoDB Inc
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package commonerrors
16

17
import (
18
        "errors"
19
        "net/http"
20

21
        atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
22
        atlasv2 "go.mongodb.org/atlas-sdk/v20250312018/admin"
23
        atlas "go.mongodb.org/atlas/mongodbatlas"
24
        "golang.org/x/oauth2"
25
)
26

27
var (
28
        errClusterUnsupported         = errors.New("atlas supports this command only for M10+ clusters. You can upgrade your cluster by running the 'atlas cluster upgrade' command")
29
        errOutsideVPN                 = errors.New("forbidden action outside access allow list, if you are a MongoDB employee double check your VPN connection")
30
        errAsymmetricShardUnsupported = errors.New("trying to run a cluster wide scaling command on an independent shard scaling cluster. Use --autoScalingMode 'independentShardScaling' instead")
31
        ErrUnauthorized               = errors.New(`unauthorized
32

33
To log in using your Atlas username and password or to set credentials using a Service account or API keys, run: atlas auth login`)
34
        ErrInvalidRefreshToken = errors.New(`session expired
35

36
Please note that your session expires periodically. 
37
If you use Atlas CLI for automation, see https://www.mongodb.com/docs/atlas/cli/stable/atlas-cli-automate/ for best practices.
38
To login, run: atlas auth login`)
39
)
40

41
const (
42
        unknownErrorCode                        = "UNKNOWN_ERROR"
43
        asymmetricShardUnsupportedErrorCode     = "ASYMMETRIC_SHARD_UNSUPPORTED"
44
        tenantClusterUpdateUnsupportedErrorCode = "TENANT_CLUSTER_UPDATE_UNSUPPORTED"
45
        globalUserOutsideSubnetErrorCode        = "GLOBAL_USER_OUTSIDE_SUBNET"
46
        unauthorizedErrorCode                   = "UNAUTHORIZED"
47
        invalidRefreshTokenErrorCode            = "INVALID_REFRESH_TOKEN"
48
        invalidServiceAccountClient             = "invalid_client"
49
)
50

51
// Check checks the error and returns a more user-friendly error message if applicable.
52
func Check(err error) error {
1✔
53
        if err == nil {
2✔
54
                return nil
1✔
55
        }
1✔
56

57
        apiErrorCode := getErrorCode(err)
1✔
58

1✔
59
        switch apiErrorCode {
1✔
60
        case unauthorizedErrorCode:
1✔
61
                return ErrUnauthorized
1✔
62
        case invalidRefreshTokenErrorCode:
1✔
63
                return ErrInvalidRefreshToken
1✔
64
        case tenantClusterUpdateUnsupportedErrorCode:
1✔
65
                return errClusterUnsupported
1✔
66
        case globalUserOutsideSubnetErrorCode:
×
67
                return errOutsideVPN
×
68
        case asymmetricShardUnsupportedErrorCode:
1✔
69
                return errAsymmetricShardUnsupported
1✔
70
        case invalidServiceAccountClient: // oauth2 error
×
71
                return ErrUnauthorized
×
72
        }
73

74
        apiError := getError(err) // some `Unauthorized` errors do not have an error code, so we check the HTTP status code
1✔
75

1✔
76
        if apiError == http.StatusUnauthorized {
1✔
77
                return ErrUnauthorized
×
78
        }
×
79

80
        return err
1✔
81
}
82

83
// getErrorCode extracts the error code from the error if it is an Atlas error.
84
// This function checks for v2 SDK, the pinned clusters SDK, the old SDK errors
85
// and oauth2 errors.
86
// If the error is not any of these errors, it returns "UNKNOWN_ERROR".
87
func getErrorCode(err error) string {
1✔
88
        if err == nil {
1✔
89
                return unknownErrorCode
×
90
        }
×
91

92
        var atlasErr *atlas.ErrorResponse
1✔
93
        if errors.As(err, &atlasErr) {
2✔
94
                return atlasErr.ErrorCode
1✔
95
        }
1✔
96
        if sdkError, ok := atlasv2.AsError(err); ok {
2✔
97
                return sdkError.ErrorCode
1✔
98
        }
1✔
99
        if sdkPinnedError, ok := atlasClustersPinned.AsError(err); ok {
2✔
100
                return sdkPinnedError.GetErrorCode()
1✔
101
        }
1✔
102
        var oauth2Err *oauth2.RetrieveError
1✔
103
        if errors.As(err, &oauth2Err) {
1✔
104
                return oauth2Err.ErrorCode
×
105
        }
×
106

107
        return unknownErrorCode
1✔
108
}
109

110
// getError extracts the HTTP error code from the error if it is an Atlas error.
111
// This function checks for v2 SDK, the pinned clusters SDK and the old SDK errors.
112
// If the error is not any of these Atlas errors, it returns 0.
113
func getError(err error) int {
1✔
114
        if err == nil {
1✔
115
                return 0
×
116
        }
×
117

118
        var atlasErr *atlas.ErrorResponse
1✔
119
        if errors.As(err, &atlasErr) {
2✔
120
                return atlasErr.HTTPCode
1✔
121
        }
1✔
122
        if apiError, ok := atlasv2.AsError(err); ok {
2✔
123
                return apiError.GetError()
1✔
124
        }
1✔
125
        if apiPinnedError, ok := atlasClustersPinned.AsError(err); ok {
2✔
126
                return apiPinnedError.GetError()
1✔
127
        }
1✔
128

129
        return 0
1✔
130
}
131

132
func IsAsymmetricShardUnsupported(err error) bool {
×
133
        apiError, ok := atlasv2.AsError(err)
×
134
        if !ok {
×
135
                return false
×
136
        }
×
137
        return apiError.GetErrorCode() == asymmetricShardUnsupportedErrorCode
×
138
}
139

140
func IsCannotUseFlexWithClusterApis(err error) bool {
×
141
        apiError, ok := atlasv2.AsError(err)
×
142
        if !ok {
×
143
                return false
×
144
        }
×
145
        return apiError.GetErrorCode() == "CANNOT_USE_FLEX_CLUSTER_IN_CLUSTER_API"
×
146
}
147

148
func IsInvalidRefreshToken(err error) bool {
×
149
        errCode := getErrorCode(err)
×
150
        return errCode == invalidRefreshTokenErrorCode
×
151
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc