public async Task Invoke(HttpContext context) { var responseStarted = false; try { context.Response.OnSendingHeaders(state => responseStarted = true, null); await _next(context); } catch (Exception ex) { _logger.WriteError("An unhandled exception has occurred: " + ex.Message, ex); // We can't do anything if the response has already started, just abort. if (responseStarted) { _logger.WriteWarning("The response has already started, the error handler will not be executed."); throw; } PathString originalPath = context.Request.Path; if (_options.ErrorHandlingPath.HasValue) { context.Request.Path = _options.ErrorHandlingPath; } try { var errorHandlerFeature = new ErrorHandlerFeature() { Error = ex, }; context.SetFeature <IErrorHandlerFeature>(errorHandlerFeature); context.Response.StatusCode = 500; context.Response.Headers.Clear(); // TODO: Try clearing any buffered data. The buffering feature/middleware has not been designed yet. await _options.ErrorHandler(context); // TODO: Optional re-throw? We'll re-throw the original exception by default if the error handler throws. return; } catch (Exception ex2) { // Suppress secondary exceptions, re-throw the original. _logger.WriteError("An exception was thrown attempting to execute the error handler.", ex2); } finally { context.Request.Path = originalPath; } throw; // Re-throw the original if we couldn't handle it } }
public async Task Invoke(HttpContext context) { var responseStarted = false; try { context.Response.OnSendingHeaders(state => responseStarted = true, null); await _next(context); } catch (Exception ex) { _logger.LogError("An unhandled exception has occurred: " + ex.Message, ex); // We can't do anything if the response has already started, just abort. if (responseStarted) { _logger.LogWarning("The response has already started, the error handler will not be executed."); throw; } PathString originalPath = context.Request.Path; if (_options.ErrorHandlingPath.HasValue) { context.Request.Path = _options.ErrorHandlingPath; } try { var errorHandlerFeature = new ErrorHandlerFeature() { Error = ex, }; context.SetFeature<IErrorHandlerFeature>(errorHandlerFeature); context.Response.StatusCode = 500; context.Response.Headers.Clear(); // TODO: Try clearing any buffered data. The buffering feature/middleware has not been designed yet. await _options.ErrorHandler(context); // TODO: Optional re-throw? We'll re-throw the original exception by default if the error handler throws. return; } catch (Exception ex2) { // Suppress secondary exceptions, re-throw the original. _logger.LogError("An exception was thrown attempting to execute the error handler.", ex2); } finally { context.Request.Path = originalPath; } throw; // Re-throw the original if we couldn't handle it } }