mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 14:03:29 -07:00
started to remove iisexpress.
This commit is contained in:
parent
40f3a8663d
commit
68128809c9
39 changed files with 383 additions and 820 deletions
41
NzbDrone.Api/ErrorManagement/ApiException.cs
Normal file
41
NzbDrone.Api/ErrorManagement/ApiException.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using Nancy;
|
||||
using Nancy.Responses;
|
||||
using Newtonsoft.Json;
|
||||
using NzbDrone.Api.Extentions;
|
||||
|
||||
namespace NzbDrone.Api.ErrorManagement
|
||||
{
|
||||
public abstract class ApiException : Exception
|
||||
{
|
||||
public object Content { get; private set; }
|
||||
|
||||
|
||||
public HttpStatusCode StatusCode { get; private set; }
|
||||
|
||||
protected ApiException(HttpStatusCode statusCode, object content = null)
|
||||
: base(GetMessage(statusCode, content))
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
Content = content;
|
||||
}
|
||||
|
||||
public JsonResponse<ErrorModel> ToErrorResponse()
|
||||
{
|
||||
return new ErrorModel(this).AsResponse(StatusCode);
|
||||
}
|
||||
|
||||
private static string GetMessage(HttpStatusCode statusCode, object content)
|
||||
{
|
||||
var result = statusCode.ToString();
|
||||
|
||||
if (content != null)
|
||||
{
|
||||
result = result + " :" + JsonConvert.SerializeObject(content);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
33
NzbDrone.Api/ErrorManagement/ErrorHandler.cs
Normal file
33
NzbDrone.Api/ErrorManagement/ErrorHandler.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
using System.Linq;
|
||||
using Nancy;
|
||||
using Nancy.ErrorHandling;
|
||||
using NzbDrone.Api.Extentions;
|
||||
|
||||
namespace NzbDrone.Api.ErrorManagement
|
||||
{
|
||||
public class ErrorHandler : IStatusCodeHandler
|
||||
{
|
||||
public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Handle(HttpStatusCode statusCode, NancyContext context)
|
||||
{
|
||||
if (statusCode == HttpStatusCode.SeeOther || statusCode == HttpStatusCode.OK)
|
||||
return;
|
||||
|
||||
if (statusCode == HttpStatusCode.Continue)
|
||||
{
|
||||
context.Response = new Response { StatusCode = statusCode };
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.Response.ContentType == "text/html" || context.Response.ContentType == "text/plain")
|
||||
context.Response = new ErrorModel
|
||||
{
|
||||
Message = statusCode.ToString()
|
||||
}.AsResponse(statusCode);
|
||||
}
|
||||
}
|
||||
}
|
21
NzbDrone.Api/ErrorManagement/ErrorModel.cs
Normal file
21
NzbDrone.Api/ErrorManagement/ErrorModel.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using System.Linq;
|
||||
|
||||
namespace NzbDrone.Api.ErrorManagement
|
||||
{
|
||||
public class ErrorModel
|
||||
{
|
||||
public string Message { get; set; }
|
||||
public string Description { get; set; }
|
||||
public object Content { get; set; }
|
||||
|
||||
public ErrorModel(ApiException exception)
|
||||
{
|
||||
Message = exception.Message;
|
||||
Content = exception.Content;
|
||||
}
|
||||
|
||||
public ErrorModel()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
38
NzbDrone.Api/ErrorManagement/ErrorPipeline.cs
Normal file
38
NzbDrone.Api/ErrorManagement/ErrorPipeline.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using Nancy;
|
||||
using NzbDrone.Api.Extentions;
|
||||
|
||||
namespace NzbDrone.Api.ErrorManagement
|
||||
{
|
||||
public class ErrorPipeline
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public ErrorPipeline(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Response HandleException(NancyContext context, Exception exception)
|
||||
{
|
||||
var apiException = exception as ApiException;
|
||||
|
||||
if (apiException != null)
|
||||
{
|
||||
_logger.WarnException("API Error", apiException);
|
||||
return apiException.ToErrorResponse();
|
||||
}
|
||||
|
||||
_logger.ErrorException("Unexpected error", exception);
|
||||
|
||||
|
||||
return new ErrorModel()
|
||||
{
|
||||
Message = exception.Message,
|
||||
Description = exception.ToString()
|
||||
}.AsResponse(HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue