mirror of
https://github.com/Readarr/Readarr.git
synced 2026-03-29 18:14:28 -04:00
New: Lidarr to Readarr
This commit is contained in:
41
src/Readarr.Http/ErrorManagement/ErrorHandler.cs
Normal file
41
src/Readarr.Http/ErrorManagement/ErrorHandler.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Nancy;
|
||||
using Nancy.ErrorHandling;
|
||||
using Readarr.Http.Extensions;
|
||||
|
||||
namespace Readarr.Http.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 (statusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.Response.ContentType == "text/html" || context.Response.ContentType == "text/plain")
|
||||
{
|
||||
context.Response = new ErrorModel
|
||||
{
|
||||
Message = statusCode.ToString()
|
||||
}.AsResponse(context, statusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/Readarr.Http/ErrorManagement/ErrorModel.cs
Normal file
21
src/Readarr.Http/ErrorManagement/ErrorModel.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Readarr.Http.Exceptions;
|
||||
|
||||
namespace Readarr.Http.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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
92
src/Readarr.Http/ErrorManagement/ReadarrErrorPipeline.cs
Normal file
92
src/Readarr.Http/ErrorManagement/ReadarrErrorPipeline.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Data.SQLite;
|
||||
using FluentValidation;
|
||||
using Nancy;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using Readarr.Http.Exceptions;
|
||||
using Readarr.Http.Extensions;
|
||||
using HttpStatusCode = Nancy.HttpStatusCode;
|
||||
|
||||
namespace Readarr.Http.ErrorManagement
|
||||
{
|
||||
public class ReadarrErrorPipeline
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public ReadarrErrorPipeline(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Response HandleException(NancyContext context, Exception exception)
|
||||
{
|
||||
_logger.Trace("Handling Exception");
|
||||
|
||||
if (exception is ApiException apiException)
|
||||
{
|
||||
_logger.Warn(apiException, "API Error");
|
||||
return apiException.ToErrorResponse(context);
|
||||
}
|
||||
|
||||
if (exception is ValidationException validationException)
|
||||
{
|
||||
_logger.Warn("Invalid request {0}", validationException.Message);
|
||||
|
||||
return validationException.Errors.AsResponse(context, HttpStatusCode.BadRequest);
|
||||
}
|
||||
|
||||
if (exception is NzbDroneClientException clientException)
|
||||
{
|
||||
return new ErrorModel
|
||||
{
|
||||
Message = exception.Message,
|
||||
Description = exception.ToString()
|
||||
}.AsResponse(context, (HttpStatusCode)clientException.StatusCode);
|
||||
}
|
||||
|
||||
if (exception is ModelNotFoundException notFoundException)
|
||||
{
|
||||
return new ErrorModel
|
||||
{
|
||||
Message = exception.Message,
|
||||
Description = exception.ToString()
|
||||
}.AsResponse(context, HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
if (exception is ModelConflictException conflictException)
|
||||
{
|
||||
return new ErrorModel
|
||||
{
|
||||
Message = exception.Message,
|
||||
Description = exception.ToString()
|
||||
}.AsResponse(context, HttpStatusCode.Conflict);
|
||||
}
|
||||
|
||||
if (exception is SQLiteException sqLiteException)
|
||||
{
|
||||
if (context.Request.Method == "PUT" || context.Request.Method == "POST")
|
||||
{
|
||||
if (sqLiteException.Message.Contains("constraint failed"))
|
||||
{
|
||||
return new ErrorModel
|
||||
{
|
||||
Message = exception.Message,
|
||||
}.AsResponse(context, HttpStatusCode.Conflict);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.Error(sqLiteException, "[{0} {1}]", context.Request.Method, context.Request.Path);
|
||||
}
|
||||
|
||||
_logger.Fatal(exception, "Request Failed. {0} {1}", context.Request.Method, context.Request.Path);
|
||||
|
||||
return new ErrorModel
|
||||
{
|
||||
Message = exception.Message,
|
||||
Description = exception.ToString()
|
||||
}.AsResponse(context, HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user