1

For handling all errors I use ErrorHandlingMiddleware. Anywhere where I want I throw exception (my implementation exception) where I define type (enum) and message of errors. My question is how can I generate this type of error in swagger, becaouse swagger know only about controller return type values. And swagger generate only response (200) bcs in methods is only return Ok();

[HttpPost("login")]
public async Task<IActionResult> Test(LoginRequest req)
{
    if (user.Banned)
    {
        throw new BadRequestHorseedoException(ErrorCode.BANNED, "User is banned");
    }

    return Ok();
}

1 Answer 1

3

You can use ProducesResponseType attribute on methods. Your code can look like:

[HttpPost("login")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Test(LoginRequest req)
Sign up to request clarification or add additional context in comments.

3 Comments

I know about it but I have own ErrorResponse Class for all responses. And here I can only use StatusCodes Enum.
I can handle like this: [ProducesResponseType(200)] [ProducesResponseType(typeof(ErrorResponse), 400)] [ProducesResponseType(typeof(ErrorResponse), 401)] [ProducesResponseType(typeof(ErrorResponse), 500)] But its so ugly :D
Yes, it's messy verbose. Maybe look at conventions learn.microsoft.com/en-us/aspnet/core/web-api/advanced/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.