edit-icon download-icon

Handle errors

Last Updated: Mar 20, 2018

Alibaba Cloud Go SDK returns corresponding exceptions when an error occurs on the service side or the SDK side. These exceptions contain detailed error information, including the error code (ErrCode) and error message (ErrMsg).

These errors conform to the Go error API, so you can handle errors returned by Alibaba Cloud Go SDK in the same way as handling Go errors.

Client error

When an error occurs to any call in SDK and cannot be handled automatically, SDK returns a ClientError.

You can add the following codes to determine and obtain the error in ClientError:

  1. response, err := ecsClient.DescribeInstances(request)
  2. if clientError, ok := err.(*errors.ClientError); ok{
  3. // Obtain the error code
  4. clientError.ErrorCode()
  5. // Obtain the error description
  6. clientError.Message()
  7. // Obtain the original error (may be nil)
  8. clientError.OriginError()
  9. }

Server error

When the server side returns an error message, SDK wraps the response into a ServerError error and returns it.

Note: In this case, you can still obtain the original HTTP response from the response.

You can add the following codes to determine and obtain the error in ServerError:

  1. response, err := ecsClient.DescribeInstances(request)
  2. if serverError, ok := err.(*errors.ServerError); ok{
  3. // Obtain the error code
  4. serverError.ErrorCode()
  5. // Obtain the error description
  6. serverError.Message()
  7. // Obtain the original http response
  8. response.GetOriginHttpResponse()
  9. }
Thank you! We've received your feedback.