internal static async Task HandleResponseWriteException(this Exception originalEx, IRequest request, IResponse response, string defaultContentType) { await HostContext.RaiseAndHandleException(request, response, request.OperationName, originalEx); if (!HostContext.Config.WriteErrorsToResponse) { throw originalEx; } var errorMessage = $"Error occured while Processing Request: [{originalEx.GetType().GetOperationName()}] {originalEx.Message}"; try { if (!response.IsClosed) { await response.WriteErrorToResponse( request, defaultContentType ?? request.ResponseContentType, request.OperationName, errorMessage, originalEx, (int)HttpStatusCode.InternalServerError); } } catch (Exception writeErrorEx) { //Exception in writing to response should not hide the original exception Log.Info("Failed to write error to response: {0}", writeErrorEx); throw originalEx; } }