Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Details without Stack Trace

In my MVC WebApi service, when an exception is thrown, it is handled by a filter:

public class GlobalExceptionFilter : ExceptionFilterAttribute {
    public override void OnException(HttpActionExecutedContext context) {
        context.Response = context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, 
                                                               "Bad Request", 
                                                               context.Exception);
    } 
}

This HTTP response generated by this filter is dependent on config.IncludeErrorDetailPolicy configuration.

If I set config.IncludeErrorDetailPolicy to IncludeErrorDetailPolicy.Always, all the details are serialized into the HTTP response (Message, ExceptionMessage, ExceptionType, and StackTrace).

If I set config.IncludeErrorDetailPolicy to IncludeErrorDetailPolicy.Never, only the Message is included.

However, I want to include the Message, ExceptionMessage, and ExceptionType in the HTTP response but not the StackTrace; how do I exclude only the StackTrace? Or should I just concatenate the needed details into the Message field?

To add some context to my question, the client needs these exception details to handle special cases...but never the stack trace.

like image 396
Bill Heitstuman Avatar asked Dec 09 '14 05:12

Bill Heitstuman


People also ask

What does error stack trace mean?

Stack trace error is a generic term frequently associated with long error messages. The stack trace information identifies where in the program the error occurs and is helpful to programmers. For users, the long stack track information may not be very useful for troubleshooting web errors.

Does error contain stack trace in Java?

A Java stack trace is displayed when an error or exception occurs. The stack trace, also called a backtrace, consists of a collection of stack records, which store an application's movement during its execution.

What causes stack trace error?

Usually, a stack trace is shown when an Exception is not handled correctly in code. (An exception is what a runtime environment uses to tell you that there's an error in your code.) This may be one of the built-in Exception types, or a custom Exception created by a program or a library.

How do I disable stack trace?

To stop getting stack trace for messages that are issued by the server or storage agent, the stack trace for these messages must be disabled. Issue the MSGSTACKTRACE DISABLE < messageNumber > command to disable one or more messages. The < messageNumber > might be a space-delimited list of message numbers.


1 Answers

Thanks for pointing me in the right direction Leon. Your link inspired my solution below. It keeps the functionality of the CreateErrorResponse method and appends the ExceptionMessage and ExceptionType attributes.

public class GlobalExceptionFilter : ExceptionFilterAttribute {
    public override void OnException(HttpActionExecutedContext context) {
        context.Response = context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, 
                                                               "Bad Request", 
                                                               context.Exception);
            var httpError = (HttpError)((ObjectContent<HttpError>)context.Response.Content).Value;
            if (!httpError.ContainsKey("ExceptionType"))
                httpError.Add("ExceptionType", context.Exception.GetType().FullName);
            if (!httpError.ContainsKey("ExceptionMessage"))
                httpError.Add("ExceptionMessage", context.Exception.Message);
    } 
}
like image 156
Bill Heitstuman Avatar answered Oct 07 '22 16:10

Bill Heitstuman