REST API Error Handling Best Practices

REST API Error Handling Best Practices

References

HTTP status

  • https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  • 200 OK
  • 201 Created
  • 202 Accepted
  • 204 No Content
  • 301 Moved Permanently
  • 304 Not Modified
  • 307 Temporary Redirect
  • 308 Permanent Redirect
  • 400 Bad Request
  • 401 Unauthorized
  • 403 Forbidden
  • 404 NOT FOUND
  • 405 Method Not Allowed
  • 409 Conflict
  • 410 Gone
  • 412 Precondition Failed
  • 429 Too Many Requests
  • 500 Internal Server Error
  • 501 Not Implemented
  • 502 Bad Gateway
  • 503 Service Unavailable
  • 508 Loop Detected

Sample

1
2
3
4
5
6
7
8
9
10
11
12
HTTP/1.1 403 Forbidden
Content-Type: application/problem+json
Content-Language: en

{
"timestamp":"2019-09-21 13:01:59.667+08:00",
"path":"/api/books/12345"
"status" : 403,
"code" : 123,
"error":"NoHostAvailable",
"message": "Your current balance is 30, but that costs 50."
}