5

An error is occurring when a client I'm using calls to Web Api. The problem is the stack trace I'm getting back is a bit unreadable, there are no line numbers so I am not sure where to look. This is it being returned as a Json response.

 "message": "An error has occurred.",
"exceptionMessage": "Object reference not set to an instance of an object.",
"exceptionType": "System.NullReferenceException",
"stackTrace": "   at WakeSocial.Gateway.WebApi.Host.Controllers.NotificationsController.<Response>d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}

Is there a way I can config web api to return the line number where the error occurred so I can debug the issue more appropriately?

The issue was covered here : Stack traces with async/await

I'm currently usng .Net 4.5 yet the errors still get outputted this way.

Community
  • 1
  • 1
Joel Dean
  • 2,444
  • 5
  • 32
  • 50
  • can you debug the api? you could easily look for an `NullReferenceException` if you could. – Amit Kumar Ghosh Aug 08 '15 at 19:21
  • 2
    @JoelDean: `WakeSocial.Gateway.WebApi.Host.Controllers.NotificationsController.d__7.MoveNext()` means that it's in the `NotificationsController.Response` action. Is the action really long and complex enough that you need a line number to track it down? – Stephen Cleary Aug 09 '15 at 01:38

1 Answers1

0

I am use an extension that stores stack trace information inside exceptions, for example, original code:

try{    
   await NestedAsyncOperation();
}catch(NullReferenceException e){
  Debug.WriteLine(e)
}

with readable stack trace:

try{    
   await NestedAsyncOperation().Trace();
}catch(Exception e){
   e.Catch((NullReferenceException ex) => {
     Debug.WriteLine(e);
   }).RethrowUnhandled();
}

and output will contains stack trace with line numbers. See more https://github.com/ilio/AsyncStackTrace/blob/master/UnitTests/AsyncStackTraceExtensionUnitTest.cs

IgorL
  • 1,169
  • 10
  • 19