Skip to content

Commit 0e2a931

Browse files
author
Anton
authored
CLOUDP-78002: adds support for Atlas Error codes (#157)
1 parent 8354115 commit 0e2a931

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

mongodbatlas/mongodbatlas.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,12 @@ type ListOptions struct {
146146
type ErrorResponse struct {
147147
// HTTP response that caused this error
148148
Response *http.Response
149-
// The error code, which is simply the HTTP status code.
150-
ErrorCode int `json:"Error"`
149+
150+
// The error code as specified in https://docs.atlas.mongodb.com/reference/api/api-errors/
151+
ErrorCode string `json:"errorCode"`
152+
153+
// HTTP status code.
154+
HTTPCode int `json:"error"`
151155

152156
// A short description of the error, which is simply the HTTP status phrase.
153157
Reason string `json:"reason"`
@@ -421,7 +425,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
421425

422426
func (r *ErrorResponse) Error() string {
423427
return fmt.Sprintf("%v %v: %d (request %q) %v",
424-
r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.Reason, r.Detail)
428+
r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.ErrorCode, r.Detail)
425429
}
426430

427431
// CheckResponse checks the API response for errors, and returns them if present. A response is considered an

mongodbatlas/mongodbatlas_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,11 @@ func TestCheckResponse(t *testing.T) {
360360
res := &http.Response{
361361
Request: &http.Request{},
362362
StatusCode: http.StatusBadRequest,
363-
Body: ioutil.NopCloser(strings.NewReader(`{"error":400, "reason":"r", "detail":"d"}`)),
363+
Body: ioutil.NopCloser(
364+
strings.NewReader(
365+
`{"error":409, "errorCode": "GROUP_ALREADY_EXISTS", "reason":"Conflict", "detail":"A group with name \"Test\" already exists"}`,
366+
),
367+
),
364368
}
365369
err := CheckResponse(res).(*ErrorResponse)
366370

@@ -370,9 +374,10 @@ func TestCheckResponse(t *testing.T) {
370374

371375
expected := &ErrorResponse{
372376
Response: res,
373-
ErrorCode: 400,
374-
Reason: "r",
375-
Detail: "d",
377+
HTTPCode: 409,
378+
ErrorCode: "GROUP_ALREADY_EXISTS",
379+
Reason: "Conflict",
380+
Detail: `A group with name "Test" already exists`,
376381
}
377382
if !reflect.DeepEqual(err, expected) {
378383
t.Errorf("Error = %#v, expected %#v", err, expected)

0 commit comments

Comments
 (0)