khellang / Middleware

Various ASP.NET Core middleware
MIT License
811 stars 112 forks source link

How to configure to display a message (detail) on the productive environment without details of the exception (exceptionDetails)? #163

Closed YuryYatskov closed 2 years ago

YuryYatskov commented 2 years ago

I need a short message on a productive environment.

throw new DuplicateWaitObjectException(nameof(id), "This is duplicate.");`
{
    "type": "https://httpstatuses.com/400",
    "title": "Bad Request",
    "status": 400,
    "detail": "This is duplicate (Parameter 'id')"
}

When i use

options.IncludeExceptionDetails = (ctx, ex) => false;`

I have without detail

{
    "type": "https://httpstatuses.com/400",
    "title": "Bad Request",
    "status": 400,
}

When true, I have with exceptionDetails

{
    "type": "https://httpstatuses.com/400",
    "title": "Bad Request",
    "status": 400,
    "detail": "This is duplicate (Parameter 'id')"
    "exceptionDetails": [
        {
            "message": "This is duplicate (Parameter 'id')",
            "type": "System.DuplicateWaitObjectException",
            "raw": "System.DuplicateWaitObjectException: This is duplicate (Parameter 'id')\r\n   at ..."
       }]
}

I need a option for see only detail

options.IncludeExceptionMessage = (ctx, ex) => true;
YuryYatskov commented 2 years ago

I found a solution, I can use map

static ProblemDetails Mapping<TException>(TException ex, int sc) where TException : Exception =>
                new ExceptionProblemDetails(ex, sc) {
                    Detail = ex.Message
                };

and

options.Map<DuplicateWaitObjectException>(ex =>  Mapping(ex, StatusCodes.Status400BadRequest));
HegrJan commented 2 years ago

Hello, it seems this is rather common problem. In #105 I've found ExceptionProblemDetails is deprecated. I also found a solution using Map and StatusCodeProblemDetails.Create. However, I think it would be nice to have it configurable via Options (Something like IncludeExceptionMessage) and use the perfectly readable MapToStatusCode.