Docs Menu
Docs Home
/
MongoDB Atlas
/ / /

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.

To fetch the error object, execute the following:

// Surrounding code omitted for brevity
projects, response, err := admin.ProjectsApi.ListProjects(ctx).Execute()
apiError, ok := admin.AsError(err)
fmt.Println(apiError)

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

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

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

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

SDK errors can be mocked by creating instance of GenericOpenAPIError struct.

// Surrounding code omitted for brevity
apiError := admin.GenericOpenAPIError{}
apiError.SetModel(admin.ApiError{
Detail: admin.PtrString("Error when listing clusters"),
Error: admin.PtrInt(400),
ErrorCode: admin.PtrString("CLUSTERS_UNREACHABLE"),
Reason: admin.PtrString("Clusters unreachable"),
})
apiError.SetError("Mocked error")

Struct can be passed as error for the all SDK mocked methods.

Back

Fundamentals