internal static Task<bool> HandleResponseWriteException(this Exception originalEx, IRequest request, IResponse response, string defaultContentType)
{
HostContext.RaiseAndHandleUncaughtException(request, response, request.OperationName, originalEx);
if (!HostContext.Config.WriteErrorsToResponse)
return originalEx.AsTaskException<bool>();
var errorMessage = $"Error occured while Processing Request: [{originalEx.GetType().GetOperationName()}] {originalEx.Message}";
try
{
if (!response.IsClosed)
{
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);
return originalEx.AsTaskException<bool>();
}
return TypeConstants.TrueTask;
}