Only show developer exception page for 500 server exceptions

Other response codes should be returned as normal
This commit is contained in:
Mark Monteiro 2020-04-13 15:26:49 -04:00
parent 9c7b3850f9
commit a8c3951c17

View File

@ -241,40 +241,38 @@ namespace Emby.Server.Implementations.HttpServer
} }
} }
private async Task ErrorHandler(Exception ex, IRequest httpReq, bool logExceptionStackTrace, string urlToLog) private async Task ErrorHandler(Exception ex, IRequest httpReq, int statusCode, string urlToLog)
{ {
try bool ignoreStackTrace =
ex is SocketException
|| ex is IOException
|| ex is OperationCanceledException
|| ex is SecurityException
|| ex is AuthenticationException
|| ex is FileNotFoundException;
if (ignoreStackTrace)
{ {
ex = GetActualException(ex); _logger.LogError("Error processing request: {Message}. URL: {Url}", ex.Message.TrimEnd('.'), urlToLog);
if (logExceptionStackTrace)
{
_logger.LogError(ex, "Error processing request. URL: {Url}", urlToLog);
}
else
{
_logger.LogError("Error processing request: {Message}. URL: {Url}", ex.Message.TrimEnd('.'), urlToLog);
}
var httpRes = httpReq.Response;
if (httpRes.HasStarted)
{
return;
}
var statusCode = GetStatusCode(ex);
httpRes.StatusCode = statusCode;
var errContent = NormalizeExceptionMessage(ex.Message);
httpRes.ContentType = "text/plain";
httpRes.ContentLength = errContent.Length;
await httpRes.WriteAsync(errContent).ConfigureAwait(false);
} }
catch (Exception errorEx) else
{ {
_logger.LogError(errorEx, "Error this.ProcessRequest(context)(Exception while writing error to the response). URL: {Url}", urlToLog); _logger.LogError(ex, "Error processing request. URL: {Url}", urlToLog);
} }
var httpRes = httpReq.Response;
if (httpRes.HasStarted)
{
return;
}
httpRes.StatusCode = statusCode;
var errContent = NormalizeExceptionMessage(ex.Message);
httpRes.ContentType = "text/plain";
httpRes.ContentLength = errContent.Length;
await httpRes.WriteAsync(errContent).ConfigureAwait(false);
} }
private string NormalizeExceptionMessage(string msg) private string NormalizeExceptionMessage(string msg)
@ -538,23 +536,32 @@ namespace Emby.Server.Implementations.HttpServer
throw new FileNotFoundException(); throw new FileNotFoundException();
} }
} }
catch (Exception ex) catch (Exception requestEx)
{ {
// Do not handle exceptions manually when in development mode try
// The framework-defined development exception page will be returned instead
if (_hostEnvironment.IsDevelopment())
{ {
throw; var requestInnerEx = GetActualException(requestEx);
} var statusCode = GetStatusCode(requestInnerEx);
bool ignoreStackTrace = // Do not handle 500 server exceptions manually when in development mode
ex is SocketException // The framework-defined development exception page will be returned instead
|| ex is IOException if (statusCode == 500 && _hostEnvironment.IsDevelopment())
|| ex is OperationCanceledException {
|| ex is SecurityException throw;
|| ex is AuthenticationException }
|| ex is FileNotFoundException;
await ErrorHandler(ex, httpReq, !ignoreStackTrace, urlToLog).ConfigureAwait(false); await ErrorHandler(requestInnerEx, httpReq, statusCode, urlToLog).ConfigureAwait(false);
}
catch (Exception handlerException)
{
var aggregateEx = new AggregateException("Error while handling request exception", requestEx, handlerException);
_logger.LogError(aggregateEx, "Error while handling exception in response to {Url}", urlToLog);
if (_hostEnvironment.IsDevelopment())
{
throw aggregateEx;
}
}
} }
finally finally
{ {