Errors
Learn how to interpret and handle HTTP errors when working with the Straddle API
When working with the Straddle API, it’s crucial to properly handle HTTP errors to ensure your application can gracefully manage unexpected situations. This guide will walk you through the different types of errors you might encounter, how to interpret them, and best practices for error handling.
Common HTTP Errors
Here’s a quick reference table of common HTTP errors you might encounter when using the Straddle API:
Status Code | Error Type | Description | Common Causes |
---|---|---|---|
400 | Bad Request | The request was unacceptable, often due to missing a required parameter. |
|
401 | Unauthorized | No valid API key provided. |
|
403 | Forbidden | The API key doesn’t have permissions to perform the request. |
|
404 | Not Found | The requested resource doesn’t exist. |
|
422 | Unprocessable Entity | The request was well-formed but was unable to be followed due to semantic errors. |
|
429 | Too Many Requests | Too many requests hit the API too quickly. |
|
500 | Internal Server Error | Something went wrong on Straddle’s end. |
|
Understanding HTTP Status Codes
Straddle uses standard HTTP status codes to indicate the success or failure of an API request. Here are the main categories:
- 2xx: Success
- 4xx: Client errors
- 5xx: Server errors
Common Error Responses
When an error occurs, Straddle returns a JSON object with details about the error. The structure of this object is consistent across all error types:
Types of Errors
400 Bad Request
This error occurs when the request is malformed or contains invalid parameters.
Example response:
How to handle:
- Check your request parameters for any missing or invalid values.
- Ensure all required fields are included and formatted correctly.
401 Unauthorized
This error occurs when authentication fails, typically due to an invalid API key.
Example response:
How to handle:
- Verify that you’re using the correct API key.
- Check if your API key has been revoked or expired.
- Ensure you’re using the correct environment (test vs. production).
403 Forbidden
This error occurs when the authenticated user doesn’t have permission to perform the requested action.
Example response:
How to handle:
- Check if the authenticated account has the necessary permissions.
- If you’re using a platform account, ensure you’ve correctly specified the
Straddle-Account-Id
header.
404 Not Found
This error occurs when the requested resource doesn’t exist.
Example response:
How to handle:
- Verify that you’re using the correct resource ID.
- Check if the resource has been deleted or hasn’t been created yet.
422 Unprocessable Entity
This error occurs when the request is well-formed, but contains invalid data.
Example response:
How to handle:
- Review the error message for specific details about which parameter is invalid.
- Adjust your request data accordingly.
429 Too Many Requests
This error occurs when you’ve exceeded the rate limit for API requests.
Example response:
How to handle:
- Implement exponential backoff and retry logic in your application.
- Consider optimizing your code to make fewer API requests.
500 Internal Server Error
This error indicates an unexpected condition on the server side.
Example response:
How to handle:
- Implement a retry mechanism with exponential backoff.
- If the error persists, contact Straddle support and provide the Request-Id and Correlation-Id.
Best Practices for Error Handling
-
Always check the HTTP status code: Don’t assume a request was successful. Always check the HTTP status code before processing the response.
-
Parse the error response: Extract the error details from the JSON response to get more information about what went wrong.
-
Log errors with context: When logging errors, include relevant context such as the Request-Id, Correlation-Id, and any pertinent request details.
-
Implement retry logic: For transient errors (like rate limiting or temporary server issues), implement a retry mechanism with exponential backoff.
-
Provide meaningful error messages to users: Translate API errors into user-friendly messages in your application’s UI.
-
Monitor error rates: Keep track of the frequency and types of errors you’re encountering to identify and address recurring issues.
Example: Error Handling in Node.js
Here’s an example of how you might handle errors when making a request to the Straddle API:
This example demonstrates how to catch and handle different types of errors, including implementing a simple retry mechanism for rate limiting errors.
Conclusion
Proper error handling is crucial for building robust applications that integrate with the Straddle API. By understanding the different types of errors, implementing appropriate error handling strategies, and following best practices, you can create a smoother experience for your users and more easily diagnose and resolve issues when they arise.
Remember to always refer to the API Reference documentation for the most up-to-date information on error codes and handling for specific endpoints.