Thursday, December 28, 2017

Swagger does not show real error message

Leave a Comment

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?

1 Answers

Answers 1

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment