-
Notifications
You must be signed in to change notification settings - Fork 152
Error handling Status codes
The first indication that your API can give about errors is the HTTP status code. HTTP is the standard of the web and the HTTP status codes should be used appropriately, thus choose carefully!
First and foremost, decide if the error is a client or server problem:
- use a 4xx status code for client errors
- the client should be able to fix the error by sending a different request
- in this case, you should provide error details in the response body (see next section)
- if you return a single error then you SHOULD return the most precise code possible
- if you return multiple errors at once then you return a 400 status code and provide specific details in the error details (refer to the next sections)
- use a 5xx status code for server errors
- the request has failed due to some internal server error
- for server errors, it's best to disclose as little information as possible as that could introduce security weaknesses
Look at the list of HTTP status codes list and select an appropriate one depending on the error.
For example:
- entity not found: 404 Not Found
- entity already exists: 409 Conflict
- missing field: 400 Bad Request
- invalid input: 400 Bad Request
- validation exception: 422 Unprocessable Entity
- business exception: 422 Unprocessable Entity
- ...
For guidelines about the exact status codes to use depending on the situation, take a look at the HTTP status codes section.
This project is distributed under the terms of the EUPL FOSS license
REST Resources Design Workflow
REST Resources Single items and collections
REST Resources Many to many Relations
REST Resources Relations expansion
HTTP Status Codes Success (2xx)
HTTP Status Codes Redirection (3xx)
HTTP Status Codes Client Error (4xx)
HTTP Status Codes Server Error (5xx)
Pagination Out of range/bounds
Long-running Operations Example
Concurrency vs Delete operation
Caching and conditional requests About
Caching and conditional requests Rules
Caching and conditional requests HTTP headers
Error handling Example with a single error
Error handling Example with multiple errors
Error handling Example with parameters
Error handling Example with additional metadata
Bulk operations HTTP status codes
Bulk operations Resources naming convention
Bulk operations Creation example
Bulk operations Update example
Bulk operations Create and update example
File upload Simple file upload
File upload Simple file upload example
File upload Complex file upload
File upload Complex file upload example
REST Security General recommendations
REST Security Insecure direct object references