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

mongodb / mongodb-atlas-cli / 16677365954

01 Aug 2025 01:40PM UTC coverage: 57.846% (-0.1%) from 57.953%
16677365954

Pull #4075

github

cveticm
Addes unit tests
Pull Request #4075: CLOUDP-330236: Adds NewServiceAccountTransport

0 of 12 new or added lines in 1 file covered. (0.0%)

518 existing lines in 22 files now uncovered.

23774 of 41099 relevant lines covered (57.85%)

2.71 hits per line

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

50.72
/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/v20250312005/admin"
23
        atlas "go.mongodb.org/atlas/mongodbatlas"
24
)
25

26
var (
27
        errClusterUnsupported         = errors.New("atlas supports this command only for M10+ clusters. You can upgrade your cluster by running the 'atlas cluster upgrade' command")
28
        errOutsideVPN                 = errors.New("forbidden action outside access allow list, if you are a MongoDB employee double check your VPN connection")
29
        errAsymmetricShardUnsupported = errors.New("trying to run a cluster wide scaling command on an independent shard scaling cluster. Use --autoScalingMode 'independentShardScaling' instead")
30
        ErrUnauthorized               = errors.New(`this action requires authentication
31
        
32
To log in using your Atlas username and password or to set credentials using API keys, run: atlas auth login`)
33
        ErrInvalidRefreshToken = errors.New(`session expired
34

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

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

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

55
        apiErrorCode := getErrorCode(err)
1✔
56

1✔
57
        switch apiErrorCode {
1✔
UNCOV
58
        case unauthorizedErrorCode:
×
UNCOV
59
                return ErrUnauthorized
×
UNCOV
60
        case invalidRefreshTokenErrorCode:
×
UNCOV
61
                return ErrInvalidRefreshToken
×
UNCOV
62
        case tenantClusterUpdateUnsupportedErrorCode:
×
63
                return errClusterUnsupported
×
64
        case globalUserOutsideSubnetErrorCode:
×
UNCOV
65
                return errOutsideVPN
×
UNCOV
66
        case asymmetricShardUnsupportedErrorCode:
×
UNCOV
67
                return errAsymmetricShardUnsupported
×
68
        }
69

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

1✔
72
        if apiError == http.StatusUnauthorized {
2✔
73
                return ErrUnauthorized
1✔
74
        }
1✔
75

76
        return err
1✔
77
}
78

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

87
        var atlasErr *atlas.ErrorResponse
1✔
88
        if errors.As(err, &atlasErr) {
1✔
UNCOV
89
                return atlasErr.ErrorCode
×
UNCOV
90
        }
×
91
        if sdkError, ok := atlasv2.AsError(err); ok {
2✔
92
                return sdkError.ErrorCode
1✔
93
        }
1✔
94
        if sdkPinnedError, ok := atlasClustersPinned.AsError(err); ok {
1✔
UNCOV
95
                return sdkPinnedError.GetErrorCode()
×
UNCOV
96
        }
×
97

98
        return unknownErrorCode
1✔
99
}
100

101
// getError extracts the HTTP error code from the error if it is an Atlas error.
102
// This function checks for v2 SDK, the pinned clusters SDK and the old SDK errors.
103
// If the error is not any of these Atlas errors, it returns 0.
104
func getError(err error) int {
1✔
105
        if err == nil {
1✔
UNCOV
106
                return 0
×
UNCOV
107
        }
×
108

109
        var atlasErr *atlas.ErrorResponse
1✔
110
        if errors.As(err, &atlasErr) {
1✔
UNCOV
111
                return atlasErr.HTTPCode
×
UNCOV
112
        }
×
113
        if apiError, ok := atlasv2.AsError(err); ok {
2✔
114
                return apiError.GetError()
1✔
115
        }
1✔
116
        if apiPinnedError, ok := atlasClustersPinned.AsError(err); ok {
1✔
UNCOV
117
                return apiPinnedError.GetError()
×
UNCOV
118
        }
×
119

120
        return 0
1✔
121
}
122

UNCOV
123
func IsAsymmetricShardUnsupported(err error) bool {
×
UNCOV
124
        apiError, ok := atlasv2.AsError(err)
×
UNCOV
125
        if !ok {
×
UNCOV
126
                return false
×
UNCOV
127
        }
×
UNCOV
128
        return apiError.GetErrorCode() == asymmetricShardUnsupportedErrorCode
×
129
}
130

131
func IsCannotUseFlexWithClusterApis(err error) bool {
1✔
132
        apiError, ok := atlasv2.AsError(err)
1✔
133
        if !ok {
1✔
UNCOV
134
                return false
×
UNCOV
135
        }
×
136
        return apiError.GetErrorCode() == "CANNOT_USE_FLEX_CLUSTER_IN_CLUSTER_API"
1✔
137
}
138

UNCOV
139
func IsInvalidRefreshToken(err error) bool {
×
UNCOV
140
        errCode := getErrorCode(err)
×
UNCOV
141
        return errCode == invalidRefreshTokenErrorCode
×
UNCOV
142
}
×
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