Only show developer exception page for 500 server exceptions

Other response codes should be returned as normal
pull/2861/head
Mark Monteiro 5 years ago
parent 9c7b3850f9
commit a8c3951c17

@ -241,19 +241,23 @@ 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 = GetActualException(ex); || ex is IOException
|| ex is OperationCanceledException
|| ex is SecurityException
|| ex is AuthenticationException
|| ex is FileNotFoundException;
if (logExceptionStackTrace) if (ignoreStackTrace)
{ {
_logger.LogError(ex, "Error processing request. URL: {Url}", urlToLog); _logger.LogError("Error processing request: {Message}. URL: {Url}", ex.Message.TrimEnd('.'), urlToLog);
} }
else else
{ {
_logger.LogError("Error processing request: {Message}. URL: {Url}", ex.Message.TrimEnd('.'), urlToLog); _logger.LogError(ex, "Error processing request. URL: {Url}", urlToLog);
} }
var httpRes = httpReq.Response; var httpRes = httpReq.Response;
@ -263,7 +267,6 @@ namespace Emby.Server.Implementations.HttpServer
return; return;
} }
var statusCode = GetStatusCode(ex);
httpRes.StatusCode = statusCode; httpRes.StatusCode = statusCode;
var errContent = NormalizeExceptionMessage(ex.Message); var errContent = NormalizeExceptionMessage(ex.Message);
@ -271,11 +274,6 @@ namespace Emby.Server.Implementations.HttpServer
httpRes.ContentLength = errContent.Length; httpRes.ContentLength = errContent.Length;
await httpRes.WriteAsync(errContent).ConfigureAwait(false); await httpRes.WriteAsync(errContent).ConfigureAwait(false);
} }
catch (Exception errorEx)
{
_logger.LogError(errorEx, "Error this.ProcessRequest(context)(Exception while writing error to the response). URL: {Url}", urlToLog);
}
}
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
{
var requestInnerEx = GetActualException(requestEx);
var statusCode = GetStatusCode(requestInnerEx);
// Do not handle 500 server exceptions manually when in development mode
// The framework-defined development exception page will be returned instead // The framework-defined development exception page will be returned instead
if (_hostEnvironment.IsDevelopment()) if (statusCode == 500 && _hostEnvironment.IsDevelopment())
{ {
throw; throw;
} }
bool ignoreStackTrace = await ErrorHandler(requestInnerEx, httpReq, statusCode, urlToLog).ConfigureAwait(false);
ex is SocketException }
|| ex is IOException catch (Exception handlerException)
|| ex is OperationCanceledException {
|| ex is SecurityException var aggregateEx = new AggregateException("Error while handling request exception", requestEx, handlerException);
|| ex is AuthenticationException _logger.LogError(aggregateEx, "Error while handling exception in response to {Url}", urlToLog);
|| ex is FileNotFoundException;
await ErrorHandler(ex, httpReq, !ignoreStackTrace, urlToLog).ConfigureAwait(false); if (_hostEnvironment.IsDevelopment())
{
throw aggregateEx;
}
}
} }
finally finally
{ {

Loading…
Cancel
Save