From 27b1bdbf553fe04b2b12ec40db005963f18aca65 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 17 Apr 2013 15:53:31 -0400 Subject: [PATCH] added custom error codes --- .../HttpServer/HttpServer.cs | 324 +----------------- .../HttpServer/ServerLogger.cs | 230 +++++++++++++ ...MediaBrowser.Server.Implementations.csproj | 4 +- .../packages.config | 1 - 4 files changed, 242 insertions(+), 317 deletions(-) create mode 100644 MediaBrowser.Server.Implementations/HttpServer/ServerLogger.cs diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs index 3fff9b70eb..527de6ae4a 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs @@ -1,3 +1,4 @@ +using System.IO; using Funq; using MediaBrowser.Common; using MediaBrowser.Common.Extensions; @@ -16,7 +17,6 @@ using ServiceStack.WebHost.Endpoints.Support; using System; using System.Collections.Generic; using System.Globalization; -using System.IO; using System.Linq; using System.Net; using System.Net.WebSockets; @@ -108,7 +108,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer DefaultRedirectPath = defaultRedirectpath; _logger = logManager.GetLogger("HttpServer"); - ServiceStack.Logging.LogManager.LogFactory = new ServerLogFactory(logManager); + LogManager.LogFactory = new ServerLogFactory(logManager); EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath = null; EndpointHostConfig.Instance.MetadataRedirectPath = "metadata"; @@ -135,8 +135,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer { DefaultRedirectPath = DefaultRedirectPath, - // Tell SS to bubble exceptions up to here - WriteErrorsToResponse = false + MapExceptionToStatusCode = { + { typeof(InvalidOperationException), 422 }, + { typeof(ResourceNotFoundException), 404 }, + { typeof(FileNotFoundException), 404 }, + { typeof(DirectoryNotFoundException), 404 } + }, + + DebugMode = true }); container.Adapter = _containerAdapter; @@ -290,45 +296,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer RaiseReceiveWebRequest(context); - await Task.Run(() => - { - try - { - ProcessRequest(context); - } - catch (InvalidOperationException ex) - { - HandleException(context.Response, ex, 422); - } - catch (ResourceNotFoundException ex) - { - HandleException(context.Response, ex, 404); - } - catch (FileNotFoundException ex) - { - HandleException(context.Response, ex, 404); - } - catch (DirectoryNotFoundException ex) - { - HandleException(context.Response, ex, 404); - } - catch (UnauthorizedAccessException ex) - { - HandleException(context.Response, ex, 401); - } - catch (ArgumentException ex) - { - HandleException(context.Response, ex, 400); - } - catch (Exception ex) - { - HandleException(context.Response, ex, 500); - } - finally - { - context.Response.Close(); - } - }).ConfigureAwait(false); + await Task.Run(() => ProcessRequest(context)).ConfigureAwait(false); } /// @@ -375,52 +343,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer } } - /// - /// Appends the error message. - /// - /// The response. - /// The ex. - /// The status code. - private void HandleException(HttpListenerResponse response, Exception ex, int statusCode) - { - _logger.ErrorException("Error processing request", ex); - - // This could fail, but try to add the stack trace as the body content - try - { - //response.StatusCode = statusCode; - - response.Headers.Add("Status", statusCode.ToString(new CultureInfo("en-US"))); - - response.Headers.Remove("Age"); - response.Headers.Remove("Expires"); - response.Headers.Remove("Cache-Control"); - response.Headers.Remove("Etag"); - response.Headers.Remove("Last-Modified"); - - if (!string.IsNullOrEmpty(ex.Message)) - { - response.AddHeader("X-Application-Error-Code", ex.Message); - } - - var sb = new StringBuilder(); - sb.AppendLine("{"); - sb.AppendLine("\"ResponseStatus\":{"); - sb.AppendFormat(" \"ErrorCode\":{0},\n", ex.GetType().Name.EncodeJson()); - sb.AppendFormat(" \"Message\":{0},\n", ex.Message.EncodeJson()); - sb.AppendFormat(" \"StackTrace\":{0}\n", ex.StackTrace.EncodeJson()); - sb.AppendLine("}"); - sb.AppendLine("}"); - - var sbBytes = sb.ToString().ToUtf8Bytes(); - response.OutputStream.Write(sbBytes, 0, sbBytes.Length); - } - catch (Exception errorEx) - { - _logger.ErrorException("Error processing failed request", errorEx); - } - } - /// /// Overridable method that can be used to implement a custom hnandler /// @@ -615,228 +537,4 @@ namespace MediaBrowser.Server.Implementations.HttpServer // Leave this empty so SS doesn't try to dispose our objects } } - - /// - /// Class ServerLogFactory - /// - public class ServerLogFactory : ILogFactory - { - /// - /// The _log manager - /// - private readonly ILogManager _logManager; - - /// - /// Initializes a new instance of the class. - /// - /// The log manager. - public ServerLogFactory(ILogManager logManager) - { - _logManager = logManager; - } - - /// - /// Gets the logger. - /// - /// Name of the type. - /// ILog. - public ILog GetLogger(string typeName) - { - return new ServerLogger(_logManager.GetLogger(typeName)); - } - - /// - /// Gets the logger. - /// - /// The type. - /// ILog. - public ILog GetLogger(Type type) - { - return GetLogger(type.Name); - } - } - - /// - /// Class ServerLogger - /// - public class ServerLogger : ILog - { - /// - /// The _logger - /// - private readonly ILogger _logger; - - /// - /// Initializes a new instance of the class. - /// - /// The logger. - public ServerLogger(ILogger logger) - { - _logger = logger; - } - - /// - /// Logs a Debug message and exception. - /// - /// The message. - /// The exception. - public void Debug(object message, Exception exception) - { - _logger.ErrorException(GetMesssage(message), exception); - } - - /// - /// Logs a Debug message. - /// - /// The message. - public void Debug(object message) - { - _logger.Debug(GetMesssage(message)); - } - - /// - /// Logs a Debug format message. - /// - /// The format. - /// The args. - public void DebugFormat(string format, params object[] args) - { - _logger.Debug(format, args); - } - - /// - /// Logs a Error message and exception. - /// - /// The message. - /// The exception. - public void Error(object message, Exception exception) - { - _logger.ErrorException(GetMesssage(message), exception); - } - - /// - /// Logs a Error message. - /// - /// The message. - public void Error(object message) - { - _logger.Error(GetMesssage(message)); - } - - /// - /// Logs a Error format message. - /// - /// The format. - /// The args. - public void ErrorFormat(string format, params object[] args) - { - _logger.Error(format, args); - } - - /// - /// Logs a Fatal message and exception. - /// - /// The message. - /// The exception. - public void Fatal(object message, Exception exception) - { - _logger.FatalException(GetMesssage(message), exception); - } - - /// - /// Logs a Fatal message. - /// - /// The message. - public void Fatal(object message) - { - _logger.Fatal(GetMesssage(message)); - } - - /// - /// Logs a Error format message. - /// - /// The format. - /// The args. - public void FatalFormat(string format, params object[] args) - { - _logger.Fatal(format, args); - } - - /// - /// Logs an Info message and exception. - /// - /// The message. - /// The exception. - public void Info(object message, Exception exception) - { - _logger.ErrorException(GetMesssage(message), exception); - } - - /// - /// Logs an Info message and exception. - /// - /// The message. - public void Info(object message) - { - _logger.Info(GetMesssage(message)); - } - - /// - /// Logs an Info format message. - /// - /// The format. - /// The args. - public void InfoFormat(string format, params object[] args) - { - _logger.Info(format, args); - } - - /// - /// Gets or sets a value indicating whether this instance is debug enabled. - /// - /// true if this instance is debug enabled; otherwise, false. - public bool IsDebugEnabled - { - get { return true; } - } - - /// - /// Logs a Warning message and exception. - /// - /// The message. - /// The exception. - public void Warn(object message, Exception exception) - { - _logger.ErrorException(GetMesssage(message), exception); - } - - /// - /// Logs a Warning message. - /// - /// The message. - public void Warn(object message) - { - _logger.Warn(GetMesssage(message)); - } - - /// - /// Logs a Warning format message. - /// - /// The format. - /// The args. - public void WarnFormat(string format, params object[] args) - { - _logger.Warn(format, args); - } - - /// - /// Gets the messsage. - /// - /// The o. - /// System.String. - private string GetMesssage(object o) - { - return o == null ? string.Empty : o.ToString(); - } - } } \ No newline at end of file diff --git a/MediaBrowser.Server.Implementations/HttpServer/ServerLogger.cs b/MediaBrowser.Server.Implementations/HttpServer/ServerLogger.cs new file mode 100644 index 0000000000..836b8502a7 --- /dev/null +++ b/MediaBrowser.Server.Implementations/HttpServer/ServerLogger.cs @@ -0,0 +1,230 @@ +using MediaBrowser.Model.Logging; +using ServiceStack.Logging; +using System; + +namespace MediaBrowser.Server.Implementations.HttpServer +{ + /// + /// Class ServerLogFactory + /// + public class ServerLogFactory : ILogFactory + { + /// + /// The _log manager + /// + private readonly ILogManager _logManager; + + /// + /// Initializes a new instance of the class. + /// + /// The log manager. + public ServerLogFactory(ILogManager logManager) + { + _logManager = logManager; + } + + /// + /// Gets the logger. + /// + /// Name of the type. + /// ILog. + public ILog GetLogger(string typeName) + { + return new ServerLogger(_logManager.GetLogger(typeName)); + } + + /// + /// Gets the logger. + /// + /// The type. + /// ILog. + public ILog GetLogger(Type type) + { + return GetLogger(type.Name); + } + } + + /// + /// Class ServerLogger + /// + public class ServerLogger : ILog + { + /// + /// The _logger + /// + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// The logger. + public ServerLogger(ILogger logger) + { + _logger = logger; + } + + /// + /// Logs a Debug message and exception. + /// + /// The message. + /// The exception. + public void Debug(object message, Exception exception) + { + _logger.ErrorException(GetMesssage(message), exception); + } + + /// + /// Logs a Debug message. + /// + /// The message. + public void Debug(object message) + { + _logger.Debug(GetMesssage(message)); + } + + /// + /// Logs a Debug format message. + /// + /// The format. + /// The args. + public void DebugFormat(string format, params object[] args) + { + _logger.Debug(format, args); + } + + /// + /// Logs a Error message and exception. + /// + /// The message. + /// The exception. + public void Error(object message, Exception exception) + { + _logger.ErrorException(GetMesssage(message), exception); + } + + /// + /// Logs a Error message. + /// + /// The message. + public void Error(object message) + { + _logger.Error(GetMesssage(message)); + } + + /// + /// Logs a Error format message. + /// + /// The format. + /// The args. + public void ErrorFormat(string format, params object[] args) + { + _logger.Error(format, args); + } + + /// + /// Logs a Fatal message and exception. + /// + /// The message. + /// The exception. + public void Fatal(object message, Exception exception) + { + _logger.FatalException(GetMesssage(message), exception); + } + + /// + /// Logs a Fatal message. + /// + /// The message. + public void Fatal(object message) + { + _logger.Fatal(GetMesssage(message)); + } + + /// + /// Logs a Error format message. + /// + /// The format. + /// The args. + public void FatalFormat(string format, params object[] args) + { + _logger.Fatal(format, args); + } + + /// + /// Logs an Info message and exception. + /// + /// The message. + /// The exception. + public void Info(object message, Exception exception) + { + _logger.ErrorException(GetMesssage(message), exception); + } + + /// + /// Logs an Info message and exception. + /// + /// The message. + public void Info(object message) + { + _logger.Info(GetMesssage(message)); + } + + /// + /// Logs an Info format message. + /// + /// The format. + /// The args. + public void InfoFormat(string format, params object[] args) + { + _logger.Info(format, args); + } + + /// + /// Gets or sets a value indicating whether this instance is debug enabled. + /// + /// true if this instance is debug enabled; otherwise, false. + public bool IsDebugEnabled + { + get { return true; } + } + + /// + /// Logs a Warning message and exception. + /// + /// The message. + /// The exception. + public void Warn(object message, Exception exception) + { + _logger.ErrorException(GetMesssage(message), exception); + } + + /// + /// Logs a Warning message. + /// + /// The message. + public void Warn(object message) + { + _logger.Warn(GetMesssage(message)); + } + + /// + /// Logs a Warning format message. + /// + /// The format. + /// The args. + public void WarnFormat(string format, params object[] args) + { + _logger.Warn(format, args); + } + + /// + /// Gets the messsage. + /// + /// The o. + /// System.String. + private string GetMesssage(object o) + { + return o == null ? string.Empty : o.ToString(); + } + } +} diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index 9575269dc9..156ade1358 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -48,9 +48,6 @@ ..\packages\morelinq.1.0.15631-beta\lib\net35\MoreLinq.dll - - ..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll - False ..\packages\ServiceStack.3.9.43\lib\net35\ServiceStack.dll @@ -120,6 +117,7 @@ + diff --git a/MediaBrowser.Server.Implementations/packages.config b/MediaBrowser.Server.Implementations/packages.config index 88876adbc1..662daf967b 100644 --- a/MediaBrowser.Server.Implementations/packages.config +++ b/MediaBrowser.Server.Implementations/packages.config @@ -4,7 +4,6 @@ -