mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2026-04-27 23:16:58 -04:00
Initial Push
This commit is contained in:
@@ -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 Prowlarr.Http.Exceptions;
|
||||
using Prowlarr.Http.Extensions;
|
||||
using HttpStatusCode = Nancy.HttpStatusCode;
|
||||
|
||||
namespace Prowlarr.Http.ErrorManagement
|
||||
{
|
||||
public class RadarrErrorPipeline
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public RadarrErrorPipeline(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