Friday, October 21, 2022

8 Common API Error Examples and Use Cases

Introduction

The following list contains common API error codes and their use cases.

404

404 – The requested resource could not be found

The server can’t find the requested resource on the server.

The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.

403

A client sends a request to the server and receives a response with HTTP status code 403. The body of this response contains information about what is wrong with the request, including the reason for failure and an error message.

For example:

“`ruby class SignUpController < ApplicationController def create @user = User.create!(params[:user]) redirect_to users_url end end “`

400

The request could not be understood by the server due to malformed syntax.

This is a server error, meaning it’s an issue with your app. The response has a 400 status code and a “Bad Request” message in its body, like this:

“`   Bad Request  “`

500

The 500 Internal Server Error is a standard error code used in many scenarios. It’s a generic message indicating that the server encountered an error and doesn’t provide additional information.

The response body of this status code usually contains more information about the type of error that occurred on the server. For example:

“`{ “message”: “An unexpected condition was encountered”, “error”: { “code”: 500, “title”: “InternalServerError” } }“`

410

The server is unwilling to process the request due to a limitation. The server has timed out, waiting for a response.

407

The request entity is too large. The request message body is too large.

408

The most common example is if you are trying to access a resource that requires authentication. For example, let’s say your user is trying to access their account on the API, but they don’t have any credentials. The client will receive a 408 error and should prompt the user for their credentials before making another request.

429

429 is a permanent HTTP status code. It indicates that the server does not want to complete the request because it’s too large. This could be due to several reasons, such as:

  • The request body is too large (the POST data, for example)
  • The request header is too large (for example, if you send a huge cookie value)

For example, freecurrencyapi.com will return a 429 error status code when you have reached your monthly quota limit.

These are common API error codes, along with their corresponding use cases.

API error codes are used by APIs to communicate errors to their users. There are many standardized API error codes, and they’re often defined in the OpenAPI Specification (OAS) — a file that describes the structure of an API. For example, if you attempt to make a call that doesn’t exist or is invalid, you might receive a 404 error code. This means that the requested resource could not be found on this server. In addition to these everyday use cases for API errors, there are also less-common ones which we’ll explore below.

Conclusion

The examples above are just a few of the most common error codes you’ll find in the wild. There are many others that we didn’t have time to cover here. Developers need to understand how these work and when they’re appropriate to avoid bad experiences with their users.

The post 8 Common API Error Examples and Use Cases appeared first on TechStory.


0 comments:

Post a Comment