Skip to content

Latest commit

 

History

History
44 lines (30 loc) · 1.44 KB

doc_2_error_handling.md

File metadata and controls

44 lines (30 loc) · 1.44 KB

Error Handling

You can obtain detailed information about errors returned by the API when you use the Atlas Go SDK. Use the error code to determine the cause of the error. To learn more about API error codes, see Atlas Administration API Error Codes.

Errors are represented by ApiErrorObject.

Fetching Error Object

To fetch the error object, execute the following:

import "go.mongodb.org/atlas-sdk/v20231115014/admin"

projects, response, err := admin.ProjectsApi.ListProjects(ctx).Execute()
apiError, ok := admin.AsError(err)
fmt.Println(apiError)

Checking for the Existence of a Specific Error Code

To check for the existence of a specific error code (e.g. MAXIMUM_INDEXES_FOR_TENANT_EXCEEDED), execute the following:

import admin "go.mongodb.org/atlas-sdk/v20231115014/admin"

projects, response, err := admin.ProjectsApi.ListProjects(ctx).Execute()
if admin.IsErrorCode(err, "code"){
 // Do something
}

Checking for the Existence of a Specific Response Status Code

To check for the existence of a specific HTTP response error code, execute the following:

import admin "go.mongodb.org/atlas-sdk/v20231115014/admin"

projects, response, err := admin.ProjectsApi.ListProjects(ctx).Execute()
apiError, ok := admin.AsError(err)
if ok && apiError.GetError() == 404 {
 // Do something
}