public static IApplicationBuilder UseInterneuronExceptionHandler(this IApplicationBuilder app, Action <IntrneuronExceptionHandlerOptions> configureOptions = null) { var options = new IntrneuronExceptionHandlerOptions(); configureOptions?.Invoke(options);// it is set now return(app.UseExceptionHandler((appbuilder) => { HandleException(appbuilder, options); })); }
public Task HandleExceptionAsync(HttpContext context, IntrneuronExceptionHandlerOptions options) { context.Response.ContentType = "application/json"; context.Response.StatusCode = this.dbEx.ErrorCode; var errorData = ExceptionError.GetExceptionErrorAsJSON(dbEx.ErrorId, dbEx.ErrorResponseMessage); var responseTask = context.Response.WriteAsync(errorData); if (options != null) { options.OnExceptionHandlingComplete?.Invoke(this.dbEx, dbEx.ErrorId); } return(responseTask); }
public Task HandleExceptionAsync(HttpContext context, IntrneuronExceptionHandlerOptions options) { var errorId = Guid.NewGuid().ToString(); var errorResponseMessage = $"Error fetching data from database. Please check the error log with ID: {errorId} for more details"; context.Response.ContentType = "application/json"; context.Response.StatusCode = 500; var errorData = ExceptionError.GetExceptionErrorAsJSON(errorId, errorResponseMessage); var responseTask = context.Response.WriteAsync(errorData); if (options != null) { options.OnExceptionHandlingComplete?.Invoke(this.ex, errorId); } return(responseTask); }
private static void HandleException(IApplicationBuilder app, IntrneuronExceptionHandlerOptions options) { app.Run(async context => { var errorFeature = context.Features.Get <IExceptionHandlerFeature>(); if (errorFeature == null) { return; } var exception = errorFeature.Error; if (exception == null) { return; } IExceptionHandler exceptionHandler = InterneuronExceptionHandlerFactory.GetExceptionHandler(exception); ////To be refactored to DI later //if (exception is PostgresException pgException) //{ // var dbExcpn = new SynapseDBException(npgEx: pgException); // exceptionHandler = new SynapseDBExceptionHandler(dbExcpn); //} //else if (exception is SynapseDBException dBException) //{ // exceptionHandler = new SynapseDBExceptionHandler(dBException); //} //else if (exception is SynapseAPIBusinessException apiException) //{ // exceptionHandler = new SynapseAPIExceptionHandler(apiException); //} //else if (exception is Exception ex) //{ // exceptionHandler = new SynapseGenericExceptionHandler(ex); //} await Task.Run(() => exceptionHandler.HandleExceptionAsync(context, options)); }); }
public static IApplicationBuilder UseInterneuronExceptionHandler(this IApplicationBuilder app) { var options = new IntrneuronExceptionHandlerOptions(); return(app.UseExceptionHandler((excpnOptions) => HandleException(app, options))); }