2

We are on .NET WebAPI framework and we are using swagger on top of web api for annotations and out of the box UI experience. So far, it just has been working great. However, when I return an error from WebAPI(Http 400) as following code:

return BadRequest("Some error");

However, when I do that, it seems like Swagger is not able to parse the error properly. It basically, show response 0. Which is wrong for sure because when I go to Chrome DevTools, I see that my request had a response of HTTP 400 as I expected.

How can I configure Swagger so that it is able to show the errors correctly? Or should I be throwing my errors a little bit differently to be able to see it in swagger?

Lost
  • 12,007
  • 32
  • 121
  • 193

2 Answers2

2

You can annotate your actions like this

[ProducesResponseType(typeof(ResponseObjectTypeHere), 200)]
[ProducesResponseType(typeof(ErrorResponseObjectTypeHere), 400)]

You can also add something like this in the xml documentation if you enable swagger to read and use xml documentation

/// <response code="200">Description</response>
/// <response code="400">Description of error</response>

I used those in the .net core web api project with swagger, should be the same for regular .net web api project as well.

AD.Net
  • 13,352
  • 2
  • 28
  • 47
  • I actually tried annotating the controller method with `[ProducesResponseType(typeof(BadRequestResult), 400)]`. However, I still do not see anything but `response type 0` message. when I throw `return BadRequest("");` there – Lost Dec 22 '17 at 16:12
2

You can simply send the exception details to the client by enabling one of ABP's configurations (SendAllExceptionsToClients) in ***.Web.Core Module, like this:

public override void PreInitialize()
{   
    if (_env.EnvironmentName.ToLower() == "development")
        Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
}

Then you get the exception details on the client. Recommended only during development.

Mehdi Daustany
  • 1,018
  • 4
  • 10
  • 23