Link to home
Start Free TrialLog in
Avatar of ram27
ram27

asked on

application Specefic error codes in web api

Hello  

   i have asp.net WEB API. i want to return application specific error codes from asp.net web api based on business rules.
Apart from standard error codes: like 201,204,404,400, 500.
My response structure is :
{
    "error": {
        "errorCode": 0,
        "message": null,
        "details": null
    },
    "result": {
        "name": "3285",
        "type": "hgfhgh",
        "code": "00042"
       
       
    }
}

In case if no errors, result will be populated and error object will be null.   if there are any errors, result object will be null and error object will be   populated with error code,error description etc..

Apart from standard HTTP errors, i want to return application specific error codes with description based on the business rules.
How can i achieve this

Thanks,
Avatar of kaufmed
kaufmed
Flag of United States of America image

The structure of the response shouldn't be a surprise to your consumers. Either you create one structure that encompasses both the good data as well as the errors, and only one is populated at a time, or you can create one structure for each, but force your consumers to rely on HTTP status codes to determine what structure is in the response. What you don't want to do is have the same HTTP status code for two different response structures where your consumer has to test what kind of structure they received.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.