How to return errors in web API 2?

How to return errors in web API 2?

Recently, after picking up Web API 2, for sending back general errors we now use the IHttpActionResult interface, specifically the built in classes for in the System.Web.Http.Results namespace such as NotFound, BadRequest when they fit, if they don’t we extend them, for example a NotFound result with a response message:

What’s the status code for an HTTP request?

Client errors, or HTTP status codes from 400 to 499, are the result of HTTP requests sent by a user client (i.e. a web browser or other HTTP client).

Why do I have a 404 error code on my Web server?

There are many situations that could cause a web server to respond to a request with a particular error code–we will cover common potential causes and solutions. Client errors, or HTTP status codes from 400 to 499, are the result of HTTP requests sent by a user client (i.e. a web browser or other HTTP client).

When to send error response in HTTP 200 envelope?

Sending an error response in a HTTP 200 envelope is misleading, and forces the client (api consumer) to parse the message, most likely in a non-standard, or proprietary way. This is also not efficient – you will force your clients to parse the HTTP payload every single time to understand the “real” response status.

How is convention annotated in Microsoft web API?

Capable of defining response types and naming requirements on actions. These methods are annotated with [ProducesResponseType] or [ProducesDefaultResponseType] attributes. For example: If more specific metadata attributes are absent, applying this convention to an assembly enforces that: The convention method applies to any action named Find.

When to use conventions in an API analyzer?

API analyzers also understand conventions. If your action is unconventional (for example, it returns a status code that isn’t documented by the applied convention), a warning encourages you to document the status code. Conventions don’t compose; each action may be associated with exactly one convention.

Where do I find the applied convention in OpenAPI?

Attributes from the applied convention are associated with an action and are included in the action’s OpenAPI documentation. API analyzers also understand conventions. If your action is unconventional (for example, it returns a status code that isn’t documented by the applied convention), a warning encourages you to document the status code.

Is it good practice to send errors back?

At the end of the day it’s an API sending back responses and not views, so I think it’s fine to send back a message with the exception and status code to the consumer. I currently haven’t needed to accumulate errors and send them back as most exceptions are usually due to incorrect parameters or calls etc.

When to return to client with a JSON error?

1 200 – OK 2 400 – Bad Request (Client Error) – A json with error \\ more details should return to the client. 3 401 – Unauthorized 4 500 – Internal Server Error – A json with an error should return to the client only when there is no security risk by doing that.

Is the HTTP status code part of the REST API?

But today when someone asks me about HTTP Status codes, it is 99.9% refers to REST API web services development. I have lots of experience in both areas (Website development, REST API web services development) and it is sometimes hard to come to a conclusion about what and how use the errors in REST APIs.