From 92628c4033e59b18ee80d06d15495b0f3f3fe357 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 21 Mar 2020 21:03:48 +0100 Subject: Clean up HTTP listener exception handling --- .../HttpServer/HttpListenerHost.cs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'Emby.Server.Implementations/HttpServer') diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 655130fcf..49179a2da 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -527,22 +527,18 @@ namespace Emby.Server.Implementations.HttpServer } else { - await ErrorHandler(new FileNotFoundException(), httpReq, false).ConfigureAwait(false); + throw new FileNotFoundException(); } } - catch (Exception ex) when (ex is SocketException || ex is IOException || ex is OperationCanceledException) - { - await ErrorHandler(ex, httpReq, false).ConfigureAwait(false); - } - catch (SecurityException ex) - { - await ErrorHandler(ex, httpReq, false).ConfigureAwait(false); - } catch (Exception ex) { - var logException = !string.Equals(ex.GetType().Name, "SocketException", StringComparison.OrdinalIgnoreCase); - - await ErrorHandler(ex, httpReq, logException).ConfigureAwait(false); + bool ignoreStackTrace = + ex is SocketException || + ex is IOException || + ex is OperationCanceledException || + ex is SecurityException || + ex is FileNotFoundException; + await ErrorHandler(ex, httpReq, ignoreStackTrace).ConfigureAwait(false); } finally { -- cgit v1.2.3 From 842ec048284acfbe9711e87ea4fce10adfa890bb Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 21 Mar 2020 21:06:01 +0100 Subject: Do not handle exceptions manually when in development mode --- .../Emby.Server.Implementations.csproj | 1 + Emby.Server.Implementations/HttpServer/HttpListenerHost.cs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'Emby.Server.Implementations/HttpServer') diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csproj index f8560ca85..ae4f0bcf0 100644 --- a/Emby.Server.Implementations/Emby.Server.Implementations.csproj +++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj @@ -32,6 +32,7 @@ + diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 49179a2da..e8ea8d033 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -23,6 +23,7 @@ using MediaBrowser.Model.Services; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using ServiceStack.Text.Jsv; @@ -48,6 +49,8 @@ namespace Emby.Server.Implementations.HttpServer private readonly string _baseUrlPrefix; private readonly Dictionary _serviceOperationsMap = new Dictionary(); private readonly List _webSocketConnections = new List(); + private readonly IHostEnvironment _hostEnvironment; + private IWebSocketListener[] _webSocketListeners = Array.Empty(); private bool _disposed = false; @@ -60,7 +63,8 @@ namespace Emby.Server.Implementations.HttpServer IJsonSerializer jsonSerializer, IXmlSerializer xmlSerializer, IHttpListener socketListener, - ILocalizationManager localizationManager) + ILocalizationManager localizationManager, + IHostEnvironment hostEnvironment) { _appHost = applicationHost; _logger = logger; @@ -72,6 +76,7 @@ namespace Emby.Server.Implementations.HttpServer _xmlSerializer = xmlSerializer; _socketListener = socketListener; _socketListener.WebSocketConnected = OnWebSocketConnected; + _hostEnvironment = hostEnvironment; _funcParseFn = t => s => JsvReader.GetParseFn(t)(s); @@ -532,6 +537,13 @@ namespace Emby.Server.Implementations.HttpServer } catch (Exception ex) { + // Do not handle exceptions manually when in development mode + // The framework-defined development exception page will be returned instead + if (_hostEnvironment.IsDevelopment()) + { + throw; + } + bool ignoreStackTrace = ex is SocketException || ex is IOException || -- cgit v1.2.3 From de634203d812922bb04359a32e59189fb0110791 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Thu, 2 Apr 2020 14:31:56 -0400 Subject: Put Boolean operators at beginning of lines instead of the end --- Emby.Server.Implementations/HttpServer/HttpListenerHost.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Emby.Server.Implementations/HttpServer') diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 62735b7cf..72667a314 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -548,11 +548,11 @@ namespace Emby.Server.Implementations.HttpServer } bool ignoreStackTrace = - ex is SocketException || - ex is IOException || - ex is OperationCanceledException || - ex is SecurityException || - ex is FileNotFoundException; + ex is SocketException + || ex is IOException + || ex is OperationCanceledException + || ex is SecurityException + || ex is FileNotFoundException; await ErrorHandler(ex, httpReq, ignoreStackTrace).ConfigureAwait(false); } finally -- cgit v1.2.3 From 71d8e66d9fbca1d448d8d4ec7c1fe9be55134076 Mon Sep 17 00:00:00 2001 From: Vasily Date: Mon, 6 Apr 2020 14:42:41 +0300 Subject: Add logging of URL being processed when logging an error This might help diagnosing stuff like "Operation was cancelled" --- Emby.Server.Implementations/HttpServer/HttpListenerHost.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'Emby.Server.Implementations/HttpServer') diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 72667a314..c12862145 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.IO; using System.Linq; using System.Net.Sockets; @@ -239,19 +240,22 @@ namespace Emby.Server.Implementations.HttpServer } } - private async Task ErrorHandler(Exception ex, IRequest httpReq, bool logExceptionStackTrace) + private async Task ErrorHandler(Exception ex, IRequest httpReq, bool logExceptionStackTrace, string urlToLog) { + string urlSuffix = string.IsNullOrWhiteSpace(urlToLog) + ? string.Format(CultureInfo.InvariantCulture, "; URL being processed: {0}", urlToLog) + : ""; try { ex = GetActualException(ex); if (logExceptionStackTrace) { - _logger.LogError(ex, "Error processing request"); + _logger.LogError(ex, "Error processing request{Suffix}", urlSuffix); } else { - _logger.LogError("Error processing request: {Message}", ex.Message); + _logger.LogError("Error processing request: {Message}{Suffix}", ex.Message, urlSuffix); } var httpRes = httpReq.Response; @@ -271,7 +275,7 @@ namespace Emby.Server.Implementations.HttpServer } catch (Exception errorEx) { - _logger.LogError(errorEx, "Error this.ProcessRequest(context)(Exception while writing error to the response)"); + _logger.LogError(errorEx, "Error this.ProcessRequest(context)(Exception while writing error to the response){Suffix}", urlSuffix); } } @@ -553,7 +557,7 @@ namespace Emby.Server.Implementations.HttpServer || ex is OperationCanceledException || ex is SecurityException || ex is FileNotFoundException; - await ErrorHandler(ex, httpReq, ignoreStackTrace).ConfigureAwait(false); + await ErrorHandler(ex, httpReq, ignoreStackTrace, urlToLog).ConfigureAwait(false); } finally { -- cgit v1.2.3 From 61d9c9df5b4ad4b79679cccbb2a624447c824b67 Mon Sep 17 00:00:00 2001 From: Vasily Date: Sun, 12 Apr 2020 23:26:45 +0300 Subject: Addressing review feedback --- Emby.Server.Implementations/HttpServer/HttpListenerHost.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'Emby.Server.Implementations/HttpServer') diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index c12862145..dd60e7a18 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -242,9 +242,7 @@ namespace Emby.Server.Implementations.HttpServer private async Task ErrorHandler(Exception ex, IRequest httpReq, bool logExceptionStackTrace, string urlToLog) { - string urlSuffix = string.IsNullOrWhiteSpace(urlToLog) - ? string.Format(CultureInfo.InvariantCulture, "; URL being processed: {0}", urlToLog) - : ""; + string urlSuffix = string.Format(CultureInfo.InvariantCulture, "; URL being processed: {0}", urlToLog); try { ex = GetActualException(ex); @@ -460,7 +458,7 @@ namespace Emby.Server.Implementations.HttpServer var stopWatch = new Stopwatch(); stopWatch.Start(); var httpRes = httpReq.Response; - string urlToLog = null; + string urlToLog = GetUrlToLog(urlString); string remoteIp = httpReq.RemoteIp; try @@ -506,8 +504,6 @@ namespace Emby.Server.Implementations.HttpServer return; } - urlToLog = GetUrlToLog(urlString); - if (string.Equals(localPath, _baseUrlPrefix + "/", StringComparison.OrdinalIgnoreCase) || string.Equals(localPath, _baseUrlPrefix, StringComparison.OrdinalIgnoreCase) || string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase) -- cgit v1.2.3 From 30f439287266a8afd6f6e7bea01ed08eb86bf0d7 Mon Sep 17 00:00:00 2001 From: Vasily Date: Sun, 12 Apr 2020 23:35:41 +0300 Subject: Fix condition flipped by https://github.com/jellyfin/jellyfin/pull/2635 --- Emby.Server.Implementations/HttpServer/HttpListenerHost.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Emby.Server.Implementations/HttpServer') diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index dd60e7a18..229e5d199 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -553,7 +553,7 @@ namespace Emby.Server.Implementations.HttpServer || ex is OperationCanceledException || ex is SecurityException || ex is FileNotFoundException; - await ErrorHandler(ex, httpReq, ignoreStackTrace, urlToLog).ConfigureAwait(false); + await ErrorHandler(ex, httpReq, !ignoreStackTrace, urlToLog).ConfigureAwait(false); } finally { -- cgit v1.2.3 From 058c35e739d4d3193e236b106cacd7ebc3926705 Mon Sep 17 00:00:00 2001 From: Vasily Date: Sun, 12 Apr 2020 23:40:34 +0300 Subject: Fix log highlithing --- Emby.Server.Implementations/HttpServer/HttpListenerHost.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'Emby.Server.Implementations/HttpServer') diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 229e5d199..95df6fbc4 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Globalization; using System.IO; using System.Linq; using System.Net.Sockets; @@ -242,18 +241,17 @@ namespace Emby.Server.Implementations.HttpServer private async Task ErrorHandler(Exception ex, IRequest httpReq, bool logExceptionStackTrace, string urlToLog) { - string urlSuffix = string.Format(CultureInfo.InvariantCulture, "; URL being processed: {0}", urlToLog); try { ex = GetActualException(ex); if (logExceptionStackTrace) { - _logger.LogError(ex, "Error processing request{Suffix}", urlSuffix); + _logger.LogError(ex, "Error processing request; URL being processed: {Url}", urlToLog); } else { - _logger.LogError("Error processing request: {Message}{Suffix}", ex.Message, urlSuffix); + _logger.LogError("Error processing request: {Message}; URL being processed: {Url}", ex.Message, urlToLog); } var httpRes = httpReq.Response; @@ -273,7 +271,7 @@ namespace Emby.Server.Implementations.HttpServer } catch (Exception errorEx) { - _logger.LogError(errorEx, "Error this.ProcessRequest(context)(Exception while writing error to the response){Suffix}", urlSuffix); + _logger.LogError(errorEx, "Error this.ProcessRequest(context)(Exception while writing error to the response); URL being processed: {Url}", urlToLog); } } -- cgit v1.2.3 From 3bdb5e80a53fe8148048fc4424b1e92adc62ac3d Mon Sep 17 00:00:00 2001 From: Vasily Date: Mon, 13 Apr 2020 00:45:09 +0300 Subject: More consise error messages --- Emby.Server.Implementations/HttpServer/HttpListenerHost.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Emby.Server.Implementations/HttpServer') diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 95df6fbc4..5ae65a4e3 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -247,11 +247,11 @@ namespace Emby.Server.Implementations.HttpServer if (logExceptionStackTrace) { - _logger.LogError(ex, "Error processing request; URL being processed: {Url}", urlToLog); + _logger.LogError(ex, "Error processing request. URL: {Url}", urlToLog); } else { - _logger.LogError("Error processing request: {Message}; URL being processed: {Url}", ex.Message, urlToLog); + _logger.LogError("Error processing request: {Message}. URL: {Url}", ex.Message.TrimEnd('.'), urlToLog); } var httpRes = httpReq.Response; @@ -271,7 +271,7 @@ namespace Emby.Server.Implementations.HttpServer } catch (Exception errorEx) { - _logger.LogError(errorEx, "Error this.ProcessRequest(context)(Exception while writing error to the response); URL being processed: {Url}", urlToLog); + _logger.LogError(errorEx, "Error this.ProcessRequest(context)(Exception while writing error to the response). URL: {Url}", urlToLog); } } -- cgit v1.2.3