1

I have a ActionFilterAttribute like that

public class DataModelAttribute : ActionFilterAttribute
{   
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        ...
        throw filterContext.Exception;  //StackTrace is fine (pointing to my controller)
    }
}

I have a global error filter like that:

public class OncHandleErrorAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext context)
    {
        var ex = context.Exception; //Here StackTrace points to DataModelAttribute file
    }
}

So I lost my original StackTrace ... How can I preserve that ?

Paul
  • 12,359
  • 20
  • 64
  • 101
  • I solved this issue using this https://stackoverflow.com/questions/17806299/custom-errorhandling-action-filter-that-catches-only-certain-type-of-exceptions. Hope its helpful – Ogunleye Olawale Jun 27 '18 at 12:18

2 Answers2

1

I guess you cant just use throw instead of throw filterContext.Exception as suggested by MSDN.

So you should throw a new Exception and include the original exception as inner exception as suggested here.

Community
  • 1
  • 1
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
0

In your case it'd look something like this:

 public class HandleExceptionErrors : ActionFilterAttribute, IExceptionFilter
{

    private Type _exceptionType;
    public HandleExceptionErrors(Type exceptionType)
    {
        _exceptionType = exceptionType;
    }
    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.Exception.GetType() != _exceptionType) return;


        filterContext.ExceptionHandled = true;
        var data= filterContext.Exception +" , "+ filterContext.Exception.InnerException+" , "+filterContext.Exception.Message +" , "+filterContext.Exception.StackTrace;

    }
}
Ogunleye Olawale
  • 306
  • 3
  • 17