src/fursvp.api/Filters/ApiExceptionFilter.cs
/// <summary>
/// Handle an Exception caught by MVC. Called by MVC when an otherwise uncaught exception is thrown.
/// </summary>
/// <param name="context">The Exception context.</param>
Handle an Exception caught by MVC. Called by MVC when an otherwise uncaught exception is thrown.
Handle an Exception caught by MVC . Called by MVC when an otherwise uncaught exception is thrown .
public override void OnException(ExceptionContext context)
        {
            if (context == null)
            {
                string message = "Exception filter was triggered with null context argument.";
                Logger?.LogError(message);
                EmailException(message);
            }
            else if (context.Exception is NotAuthorizedException authEx)
            {
                OnException(context, StatusCodes.Status401Unauthorized, authEx.GetType().Name, authEx.Message, authEx.SourceType.Name);
                Logger?.LogInformation(authEx, authEx.Message);
            }
            else if (context.Exception is ValidationException validationEx)
            {
                OnException(context, StatusCodes.Status403Forbidden, validationEx.GetType().Name, validationEx.Message);
                Logger?.LogInformation(validationEx, validationEx.Message);
            }
            else
            {
                var ex = context.Exception;
                string message = WebHostEnvironment.IsDevelopment()
                    ? ex.Message
                    : "An internal server error occurred. Sorry about that. The error has been logged.";
                OnException(context, StatusCodes.Status500InternalServerError, ex.GetType().Name, message);
                Logger?.LogError(ex, ex.Message);
                EmailException(context, ex);
            }
            base.OnException(context);
        }
public override void OnException ( ExceptionContext context ) { if ( context == null ) { string message = " " ; Logger ? . LogError ( message ) ; EmailException ( message ) ; } else if ( context . Exception is NotAuthorizedException authEx ) { OnException ( context , StatusCodes . Status401Unauthorized , authEx . GetType ( ) . Name , authEx . Message , authEx . SourceType . Name ) ; Logger ? . LogInformation ( authEx , authEx . Message ) ; } else if ( context . Exception is ValidationException validationEx ) { OnException ( context , StatusCodes . Status403Forbidden , validationEx . GetType ( ) . Name , validationEx . Message ) ; Logger ? . LogInformation ( validationEx , validationEx . Message ) ; } else { var ex = context . Exception ; string message = WebHostEnvironment . IsDevelopment ( ) ? ex . Message : " " ; OnException ( context , StatusCodes . Status500InternalServerError , ex . GetType ( ) . Name , message ) ; Logger ? . LogError ( ex , ex . Message ) ; EmailException ( context , ex ) ; } base . OnException ( context ) ; }
Handle an Exception caught by MVC.
Handle an Exception caught by MVC .
context
ExceptionContext
The Exception context.
The Exception context .