- Building Microservices with Go
- Nic Jackson
- 171字
- 2021-07-15 17:28:13
Returning errors
In the instance of failure, users of your API should be able to write one piece of code that handles errors across different endpoints. A standard error entity will help your consumers by enabling them to write DRY code whenever an error caused by client or server occurs.
The Microsoft API guidelines recommend the following format for these entities:
{
"error": {
"code": "BadArgument",
"message": "Previous passwords may not be reused",
"target": "password",
"innererror": a {
"code": "PasswordError",
"innererror": {
"code": "PasswordDoesNotMeetPolicy",
"minLength": "6",
"maxLength": "64",
"characterTypes": ["lowerCase","upperCase","number","symbol"],
"minDistinctCharacterTypes": "2",
"innererror": {
"code": "PasswordReuseNotAllowed"
}
}
}
}
}
ErrorResponse: Object
The ErrorResponse is the top level object which will be returned by our response and contains the following fields:

Error: Object
The Error object is the detail for our error response; it provides full detail for the reason that the error occurred:

InnerError: Object

Microsoft has provided an excellent API guidelines resource, you can read more about returning errors by looking at the following link:
https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md#51-errors