Skip to content

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:

{
  "code": 404,
  "type": "error",
  "message": "Pet not found"
}

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:

{
  "code": 400,
  "type": "error",
  "message": "Invalid request payload"
}

Resource Not Found

This error occurs when the requested resource does not exist in the system.

Example:

{
  "code": 404,
  "type": "error",
  "message": "Pet not found"
}

Authentication Failure

This error may occur when: - API key is missing - invalid credentials are provided - access is restricted

Example:

{
  "code": 401,
  "type": "error",
  "message": "Unauthorized"
}

Validation Error

Validation errors may occur when the submitted request does not meet API validation rules.

Example:

{
  "code": 405,
  "type": "error",
  "message": "Validation exception"
}

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:

  1. Verify the endpoint URL.
  2. Confirm that the HTTP method is correct.
  3. Check request headers and content type.
  4. Validate request body structure and required fields.
  5. Ensure that resource identifiers such as pet IDs or order IDs exist.
  6. Review the returned status code and error message for additional details.