Error Handling¶
The Petstore API uses standard HTTP status codes to indicate whether a request was processed successfully or if an error occurred during request validation or execution.
When an error occurs, the API returns a response containing an HTTP status code along with additional error details that can help identify the issue.
Proper error handling allows applications to detect failures, troubleshoot request issues, and implement retry or validation logic when required.
Error Response Structure¶
Most error responses returned by the API follow a standard response format.
Example:
Error Response Fields¶
| Field | Description |
|---|---|
| code | HTTP status code returned by the API |
| type | Error category or response type |
| message | Description of the error |
Common HTTP Error Codes¶
The following status codes are commonly returned by the API.
| Status Code | Description |
|---|---|
| 200 | Request completed successfully |
| 400 | Invalid request or incorrect input |
| 401 | Authentication failed or missing credentials |
| 403 | Access to the requested resource is denied |
| 404 | Requested resource was not found |
| 405 | Method not allowed or validation error |
| 500 | Internal server error |
Common Error Scenarios¶
Invalid Request Payload¶
This error may occur when: - required fields are missing - invalid data types are provided - request body format is incorrect
Example:
Resource Not Found¶
This error occurs when the requested resource does not exist in the system.
Example:
Authentication Failure¶
This error may occur when: - API key is missing - invalid credentials are provided - access is restricted
Example:
Validation Error¶
Validation errors may occur when the submitted request does not meet API validation rules.
Example:
Error Handling Recommendations¶
While integrating with the API, consider the following recommendations:
- Validate request payloads before sending API requests.
- Verify required parameters and request headers.
- Handle non-200 responses appropriately in the client application.
- Implement retry logic only for temporary failures when applicable.
- Log error responses for troubleshooting and debugging purposes.
- Avoid exposing sensitive error details to end users.
Troubleshooting Tips¶
If an API request fails:
- Verify the endpoint URL.
- Confirm that the HTTP method is correct.
- Check request headers and content type.
- Validate request body structure and required fields.
- Ensure that resource identifiers such as pet IDs or order IDs exist.
- Review the returned status code and error message for additional details.