Errors

In this guide, we will talk about how errors are handled in the Gatsby API. When an error occurs, the API will return an appropriate HTTP status code along with a simple error message to help you understand what went wrong.

You can determine if your request was successful by checking the status code of the API response. If a request fails, the response will include an error message that describes the issue, helping you to quickly identify and fix the problem.


Status codes

Here is a list of the different categories of status codes returned by the Gatsby API:

  • Name
    2xx
    Description

    A 2xx status code indicates a successful response.

  • Name
    4xx
    Description

    A 4xx status code indicates a client error — such as invalid input, missing parameters, or authentication issues.

  • Name
    5xx
    Description

    A 5xx status code indicates a server error — if you receive these consistently, please contact support.


Error format

When a request fails, the Gatsby API returns a simple error object with a message property that explains what went wrong. This straightforward format makes it easy to identify issues and take appropriate action.

Common error scenarios include:

  • Name
    400 Bad Request
    Description

    The request was malformed or missing required parameters.

  • Name
    401 Unauthorized
    Description

    Authentication credentials were missing or invalid.

  • Name
    403 Forbidden
    Description

    The authenticated user doesn't have permission to access the requested resource.

  • Name
    404 Not Found
    Description

    The requested resource doesn't exist.

  • Name
    422 Unprocessable Entity
    Description

    The request was well-formed but contained invalid parameters.

Error response

{
  "message": "Invalid email or password"
}

Another example

{
  "message": "Missing required header: organizationSlug"
}

Handling errors

When working with the Gatsby API, it's important to handle errors properly in your code:

import axios from 'axios';

try {
  const response = await axios.get('https://rest.gatsby.events/person', {
    headers: {
      'Authorization': `Bearer ${accessToken}`,
      'organizationSlug': organizationSlug
    }
  });
  // Process successful response
} catch (error) {
  if (error.response) {
    // The request was made and the server responded with an error status
    console.error('Error:', error.response.data.message);
    console.error('Status:', error.response.status);
  } else if (error.request) {
    // The request was made but no response was received
    console.error('No response received:', error.request);
  } else {
    // Something happened in setting up the request
    console.error('Request error:', error.message);
  }
}

Was this page helpful?