Example #1
0
        public void OnException(ExceptionContext context)
        {
            if (context.ExceptionHandled)
            {
                return;
            }

            var result = context.Exception switch
            {
                EntityNotFoundException _ => context.CreateErrorResult(ApiErrorCode.EntityNotFound),
                InvalidStatusChangeException ex => context.CreateErrorResult(ApiErrorCode.InvalidStatusChange, additionalDetails: new { ex.AllowedStatus }),
                _ => null
            };

            if (result != null)
            {
                result.DeclaredType = typeof(ProblemDetails);
                result.ContentTypes.Add("application/problem+json");
                context.Result = result;
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                context.ExceptionHandled = true;
            }
        }
    }