DEV Community

Paramanantham Harrison
Paramanantham Harrison

Posted on โ€ข Originally published at learnwithparam.com

4 1

How to provide meaningful error messages in your API response

Let's learn how to provide meaniful error messages through examples.

Clear and concise messages

Make sure the message is clear and easy to understand for the end user.

{
  "error": "Invalid input: Missing required parameter 'email'."
}
Enter fullscreen mode Exit fullscreen mode

Include error codes

Include error codes in the response to help developers debug the issue.

{
  "error": {
    "code": 400,
    "message": "Bad Request: Invalid input data."
  }
}
Enter fullscreen mode Exit fullscreen mode

Provide suggestion to fix the issue

Sometimes the error message is not enough to fix the issue. In that case, provide a suggestion to help the user fix the issue.

{
  "error": {
    "code": 404,
    "message": "Not Found: The requested resource does not exist. Please check the URL and try again."
  }
}
Enter fullscreen mode Exit fullscreen mode

Make sure to not leak internal errors

Do not leak internal errors to the end user. Instead, log the error and provide a generic error message.

{
  "error": {
    "code": 500,
    "message": "Internal Server Error: Something went wrong. Please try again later."
  }
}
Enter fullscreen mode Exit fullscreen mode

Localization

It is optional and only valid if your API supports multiple languages.

{
  "error": {
    "code": 401,
    "message": {
      "en": "Unauthorized: Please log in to access this resource.",
      "es": "No autorizado: Por favor, inicie sesiรณn para acceder a este recurso."
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Hope this helps you to learn some techniques to provide meaningful error messages in your API response to help developers and users ๐Ÿš€

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry ๐Ÿ‘€

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more โ†’

Top comments (1)

Collapse
 
ajitforger97 profile image
Ajit Forger โ€ข

Great

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay