diff --git a/DrinkRateAPI/Exceptions/ExceptionHandlingMiddleware.cs b/DrinkRateAPI/Exceptions/ExceptionHandlingMiddleware.cs index 82fb158..ba240fa 100644 --- a/DrinkRateAPI/Exceptions/ExceptionHandlingMiddleware.cs +++ b/DrinkRateAPI/Exceptions/ExceptionHandlingMiddleware.cs @@ -30,10 +30,9 @@ public class ExceptionHandlingMiddleware private async Task HandleExceptionAsync(HttpContext context, Exception exception) { - _logger.LogError(exception, "An unexpected error occurred."); + _logger.LogError(exception, "An error occurred."); - - var response = exception switch + var defaultResponse = exception switch { BadRequestException _ => new ExceptionResponse(StatusCodes.Status400BadRequest, "Application exception occurred."), NotFoundException _ => new ExceptionResponse(StatusCodes.Status404NotFound, "The request key not found."), @@ -44,7 +43,13 @@ public class ExceptionHandlingMiddleware UnavailableForLagalReasonsException _ => new ExceptionResponse(StatusCodes.Status451UnavailableForLegalReasons, "Unavailable for legal reasons."), _ => new ExceptionResponse(StatusCodes.Status500InternalServerError, "Internal server error. Please retry later.") }; - + + var description = !string.IsNullOrWhiteSpace(exception.Message) + ? exception.Message + : defaultResponse.Description; + + var response = defaultResponse with { Description = description }; + context.Response.ContentType = "application/json"; context.Response.StatusCode = response.StatusCode; await context.Response.WriteAsJsonAsync(response);