2

I run the Exceptionless project and we have a few customers using ServiceStack and I had some questions and also recommendations for your error handling. Currently you guys don't flow the exception to the asp.net core middleware or any of the diagnostics integrations and this is kind of an issue. It might be good to have a package that reports to these hooks. Instead you eat all the exceptions and call one of two handlers:

ServiceExceptionHandlers.Add((httpReq, request, exception) => {
var contextData = new ContextData();
contextData.MarkAsUnhandledError();
contextData.SetSubmissionMethod("ServiceExceptionHandlers");
exception.ToExceptionless(contextData).Submit(); // TODO: figure out how to get the http context here for extra metadata.

return null; //continue with default Error Handling
});

//Handle Unhandled Exceptions occurring outside of Services
//E.g. Exceptions during Request binding or in filters:
UncaughtExceptionHandlers.Add((req, res, operationName, ex) => {
var contextData = new ContextData();
contextData.MarkAsUnhandledError();
contextData.SetSubmissionMethod("UncaughtExceptionHandlers");
ex.ToExceptionless(contextData).SetProperty("Operation", operationName).Submit(); // TODO: figure out how to get the http context here for extra metadata.

// TODO: See if we have to do this,, rather just fallback and let them handle it.
res.Write("Error: {0}: {1}".Fmt(ex.GetType().Name, ex.Message));
res.EndRequest(skipHeaders: true);
});

You can see I don't want to interrupt any of the workflow I just want to log and continue on... Is there a good way to accomplish this?

Also, how can I get access to the http context (you pass in request, response and exception, but nothing else). I get that it's abstracted away and there isn't any context here, maybe there could be a bag of properties that I could look in to get a context? I want this so I can capture more metadata (request, user etc...)

Blake Niemyjski
  • 3,432
  • 3
  • 25
  • 41

1 Answers1

3

Please note your question seems like it's addressed directly to ServiceStack but it's posted on the public StackOverflow forum that should be addressed and open to anyone that's willing to help.

I don't want to interrupt any of the workflow I just want to log and continue on

If you don't want to interrupt the workflow return null in your ServiceExceptionHandlers and don't write to or terminate the response in your UncaughtExceptionHandlers, i.e:

//res.Write("Error: {0}: {1}".Fmt(ex.GetType().Name, ex.Message));
//res.EndRequest(skipHeaders: true);

how can I get access to the http context

You can get the underlying .NET Core Request with:

var origReq = req.OriginalRequest as HttpRequest;
var httpCtx = origReq.HttpContext;

Likewise with the Response:

var origRes = res.OriginalResponse as HttpResponse;
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thanks! Do you normalize the request or user info into a specific model that I can consume (without knowing the platform) – Blake Niemyjski May 25 '17 at 13:01
  • @BlakeNiemyjski The IRequest/IResponse which for HTTP Requests can also be casted to IHttpReques/IHttpResponse is the cross-platform abstraction covering all platforms. Casting from OriginalRequest/OriginalResponse is how you break out of that abstraction and access per-platform features. – mythz May 25 '17 at 13:07
  • Thanks, I really appreciate it. I want to create an exceptionless implementation for service stack as a bunch of our customers are using it but it's not very intuitive of an integration right now (for us). – Blake Niemyjski May 25 '17 at 18:59