Error

This section provides information on potential errors that may occur during API interactions and how to handle them.

Common error codes and descriptions

Code & Error
Description
400 Bad Request
The request is invalid or missing required parameters.
401 Unauthorized
Invalid API key or authentication credentials.
403 Forbidden
Insufficient permissions to access the resource.
404 Not Found
The requested resource was not found.
422 Unprocessable Entity
The request is well-formed but semantically incorrect.
500 Internal Server Error
An unexpected error occurred on the server.

Error handling strategies

1. Check HTTP Status Codes:

  • Use the status_code property of the response to identify errors.
  • Handle specific error codes accordingly.
  • 2. Parse Error Messages:

  • Examine the error message in the response body for more details.
  • Use the error message to provide informative feedback to the user.
  • 3.Retry Failed Requests:

  • Implement retry logic with exponential backoff to handle transient errors.
  • Avoid excessive retries to prevent overloading the server.
  • 4.Log Errors:

  • Log errors to a log file or a centralized logging service for analysis and debugging.
  • Include relevant information such as timestamps, error messages, and request/response details.
  • javascript
    
        const myHeaders = new Headers();
    myHeaders.append("token", "TlAWiePV51isvfuRZEHuCOW1ZDBeDHep2rnosIXHRVGvMwyFTMJZUVCeIneQJJ0h");
    myHeaders.append("Content-Type", "application/json");
    
    const raw = JSON.stringify({
      "customerId": "49438567-bc45-494c-b57d-80b41f193de7",
      "currency": "kes",
      "channel": "fiat"
    });
    
    const requestOptions = {
      method: "POST",
      headers: myHeaders,
      body: raw,
      redirect: "follow"
    };
    
    fetch("https://api.switch.tulupay.com/customer/add-account", requestOptions)
      .then((response) => response.text())
      .then((result) => console.log(result))
      .catch((error) => console.error(error));
    
        }

    By implementing robust error handling, you can improve the reliability and user experience of your application.