Search code examples
asp.net-web-apipostman

PostMan showing 500 Internal Server Error Instead of 401 Unauthorized?


RestException File

public class RestException: Exception
    {
        public RestException(HttpStatusCode code, object errors = null)
        {
            Code = code;
            Errors = errors;
        }
        public HttpStatusCode Code { get; }
        public object Errors { get; }
    }

Login Handler

   var user = await _userManager.FindByEmailAsync(request.Email);
            if(user == null){
                throw new RestException(HttpStatusCode.Unauthorized);
            }

            var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);

            if(result.Succeeded){
                return user;
            }

            throw new RestException(HttpStatusCode.Unauthorized);

User Controller

public class UserController : BaseController
{
    [HttpPost("login")]
    public async Task<ActionResult<AppUser>> Login(Login.Query query){
        return await Mediator.Send(query);
    }
}

Can anyone help me to get the correct error from API? I've been trying to checkout to get what's going on this system. PostMan shows the other's error as if any filed empty. I restart the dotnet and every time I get the same result. Is anyone help me out, it's really appreciated. Although, I'm a beginner here.


Solution

  • I solve this Error. It's getting from ErrorHandlingMiddleware that file i maded. I've throw command without any reason after checking the error response. That really not need to get actual error.