Updated on Sep 22, 2023

3

min read

Error handling

When interacting with the Movies API, it's important to understand how errors are handled and how to troubleshoot issues that may arise during API requests. This document covers the various aspects of error handling within the API.

Error Response Format

The Movies API uses JSON for error responses. When an error occurs, the API responds with a JSON object containing the following fields:

  • status: The HTTP status code indicating the type of error.

  • message: A human-readable error message describing the issue.

  • details (optional): Additional details about the error, if available.

Here's an example error response:

{
  "status": 404,
  "message": "Resource not found",
  "details": "The requested movie does not exist."
}

Common HTTP Status Codes

The Movies API uses standard HTTP status codes to indicate the outcome of a request. Here are some of the common status codes you may encounter:

  • 200 OK: The request was successful, and the API response contains the requested data.

  • 201 Created: The resource was successfully created (e.g., during a POST request).

  • 204 No Content: The request was successful, but there is no response body (e.g., during a DELETE request).

  • 400 Bad Request: The request is malformed or invalid. Check the request parameters.

  • 401 Unauthorized: Authentication is required, or the provided credentials are invalid.

  • 403 Forbidden: The request is understood but not allowed. The user may lack the necessary permissions.

  • 404 Not Found: The requested resource does not exist.

  • 405 Method Not Allowed: The HTTP method used is not allowed for the requested resource.

  • 500 Internal Server Error: An unexpected server error occurred. Please contact support.

Handling Errors in Code

When making API requests in your code, it's essential to handle errors gracefully. Here's an example in JavaScript:

fetch(apiUrl)
  .then(response => {
    if (!response.ok) {
      throw new Error(`API Error: ${response.status} - ${response.statusText}`);
    }
    return response.json();
  })
  .then(data => {
    console.log("API Response:", data);
  })
  .catch(error => {
    console.error("Error:", error.message);
  });

In this example, we check if the HTTP response status indicates an error (not in the 200-299 range) and throw an error with details if necessary. This allows you to handle errors in a structured manner.

Contact Support

If you encounter persistent issues or have questions about specific error messages, please contact our support team at support@moviesapi.com for assistance.

Understanding error handling and status codes is essential for effectively using the Movies API and ensuring a smooth user experience.