c# - ASP.NET Core Web API exception handling -
i started using asp.net core new rest api project after using regular asp.net web api many years. don't see way handle exceptions in asp.net core web api. tried implement exception handling filter/attribute:
public class errorhandlingfilter : exceptionfilterattribute { public override void onexception(exceptioncontext context) { handleexceptionasync(context); context.exceptionhandled = true; } private static void handleexceptionasync(exceptioncontext context) { var exception = context.exception; if (exception mynotfoundexception) setexceptionresult(context, exception, httpstatuscode.notfound); else if (exception myunauthorizedexception) setexceptionresult(context, exception, httpstatuscode.unauthorized); else if (exception myexception) setexceptionresult(context, exception, httpstatuscode.badrequest); else setexceptionresult(context, exception, httpstatuscode.internalservererror); } private static void setexceptionresult( exceptioncontext context, exception exception, httpstatuscode code) { context.result = new jsonresult(new apiresponse(exception)) { statuscode = (int)code }; } }
and here startup filter registration:
services.addmvc(options => { options.filters.add(new authorizationfilter()); options.filters.add(new errorhandlingfilter()); });
the issue having when exception occurred in authorizationfilter
it's not being handled errorhandlingfilter
. expecting caught there worked old asp.net web api.
so how can catch application exceptions exceptions action filters?
exception handling middleware
after many experiments different exception handling approaches ended using middleware. worked best asp.net core web api application. handles application exceptions exceptions filters , have full control on exception handling , creating response json. here exception handling middleware:
public class errorhandlingmiddleware { private readonly requestdelegate next; public errorhandlingmiddleware(requestdelegate next) { this.next = next; } public async task invoke(httpcontext context /* other scoped dependencies */) { try { await next(context); } catch (exception ex) { await handleexceptionasync(context, ex); } } private static task handleexceptionasync(httpcontext context, exception exception) { var code = httpstatuscode.internalservererror; // 500 if unexpected if (exception mynotfoundexception) code = httpstatuscode.notfound; else if (exception myunauthorizedexception) code = httpstatuscode.unauthorized; else if (exception myexception) code = httpstatuscode.badrequest; var result = jsonconvert.serializeobject(new { error = exception.message }); context.response.contenttype = "application/json"; context.response.statuscode = (int)code; return context.response.writeasync(result); } }
register before mvc in startup
class:
app.usemiddleware(typeof(errorhandlingmiddleware)); app.usemvc();
here example of exception response:
{ "error": "authentication token not valid." }
you can add stack trace, exception type name, error codes or want it. flexible. hope it's starting point you!
Comments
Post a Comment