Blake Niemyjski
Blake Niemyjski

Reputation: 3577

Third party exception handling hooks for ServiceStack Asp.NET Core?

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...)

Upvotes: 2

Views: 182

Answers (1)

mythz
mythz

Reputation: 143319

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;

Upvotes: 3

Related Questions