Skip to content

Error Handling

Effective error handling is crucial for developing reliable applications that interact with external APIs like Mira. This document outlines common errors that may occur during API calls, their meanings, and how to handle them gracefully.

Understanding API Errors

When an API request fails, Mira returns an HTTP status code and a JSON response containing details about the error. These errors can arise from a variety of reasons such as invalid inputs, authentication issues, or server-side problems.

Common HTTP Status Codes

Here are some of the most common HTTP status codes you might encounter when using the Mira API:

  • 400 Bad Request: The request was unacceptable, often due to missing a required parameter or malformed JSON.
  • 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.
  • 429 Too Many Requests: Too many requests hit the API too quickly. We recommend handling this with exponential backoff.
  • 500 Internal Server Error: We had a problem with our server. Try again later.

Error Response Structure

Mira provides a consistent structure for error messages, making it easier to programmatically process and handle them. Here is an example of a typical error response:

json
{
  "status": "error",
  "message": "Invalid input parameters.",
  "code": 400
}

Best Practices for Handling Errors

  1. Check Status Codes: Always check the HTTP status code that is returned. Handle each status code appropriately in your code.
  2. Log Errors: Log errors to a file or a logging system for further analysis and debugging.
  3. User Feedback: Provide clear, user-friendly error messages to the end-user if applicable.
  4. Retry Logic: Implement retry logic for errors that may be resolved by a repeat attempt, such as 429 Too Many Requests or 500 Internal Server Error.
  5. Error Routing: Route critical errors that might require developer intervention to a monitoring system or an alerting tool.
  6. Update and Validate: Regularly update your API integration code and validate inputs to prevent common 400 Bad Ltiion.

Handling Specific Errors

Example in Node.js

Below is an example of how you could structure error handling in a Node.js application using the Axios library:

javascript
import axios from "axios";

axios
  .post("https://console.mira.network/v1/@org/flow-name")
  .then((response) => {
    console.log("Data:", response.data);
  })
  .catch((error) => {
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      console.error("Error response:", error.response.data);
      console.error("Status:", error.response.status);
      console.error("Headers:", error.response.headers);
    } else if (error.request) {
      // The request was made but no response was received
      console.error("Error request:", error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.error("Error message:", error.message);
    }
  });

Example in Python

Here's how you might handle errors in a Python application using the requests library:

python
import requests

try:
    response = requests.post('https://console.mira.network/v1/@org/flow-name')
    response.raise_for_status()
    data = response.json()
    print('Data:', data)
except requests.exceptions.HTTPError as errh:
    print ("Http Error:",errh)
except requests.exceptions.ConnectionError as errc:
    print ("Error Connecting:",errc)
except requests.exceptions.Timeout as errt:
    print ("Timeout Error:",errt)
except requests.exceptions.RequestException as err:
    print ("OOps: Something Else",err)

Conclusion

Proper error handling is essential for maintaining a robust integration with the Mira API. By understanding the types of errors that can occur and implementing strategies to manage them, you can ensure that your application handles API interactions smoothly and reliably.