mirror of
https://github.com/Radarr/Radarr.git
synced 2026-04-18 21:35:51 -04:00
New: Backend changes for new UI
This commit is contained in:
35
src/Radarr.Http/ErrorManagement/ErrorHandler.cs
Normal file
35
src/Radarr.Http/ErrorManagement/ErrorHandler.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Nancy;
|
||||
using Nancy.ErrorHandling;
|
||||
using Radarr.Http.Extensions;
|
||||
|
||||
namespace Radarr.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(statusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/Radarr.Http/ErrorManagement/ErrorModel.cs
Normal file
21
src/Radarr.Http/ErrorManagement/ErrorModel.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Radarr.Http.Exceptions;
|
||||
|
||||
namespace Radarr.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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
79
src/Radarr.Http/ErrorManagement/RadarrErrorPipeline.cs
Normal file
79
src/Radarr.Http/ErrorManagement/RadarrErrorPipeline.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Data.SQLite;
|
||||
using FluentValidation;
|
||||
using Nancy;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using Radarr.Http.Exceptions;
|
||||
using Radarr.Http.Extensions;
|
||||
using HttpStatusCode = Nancy.HttpStatusCode;
|
||||
|
||||
namespace Radarr.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");
|
||||
|
||||
var apiException = exception as ApiException;
|
||||
|
||||
if (apiException != null)
|
||||
{
|
||||
_logger.Warn(apiException, "API Error");
|
||||
return apiException.ToErrorResponse();
|
||||
}
|
||||
|
||||
var validationException = exception as ValidationException;
|
||||
|
||||
if (validationException != null)
|
||||
{
|
||||
_logger.Warn("Invalid request {0}", validationException.Message);
|
||||
|
||||
return validationException.Errors.AsResponse(HttpStatusCode.BadRequest);
|
||||
}
|
||||
|
||||
var clientException = exception as NzbDroneClientException;
|
||||
|
||||
if (clientException != null)
|
||||
{
|
||||
return new ErrorModel
|
||||
{
|
||||
Message = exception.Message,
|
||||
Description = exception.ToString()
|
||||
}.AsResponse((HttpStatusCode)clientException.StatusCode);
|
||||
}
|
||||
|
||||
var sqLiteException = exception as SQLiteException;
|
||||
|
||||
if (sqLiteException != null)
|
||||
{
|
||||
if (context.Request.Method == "PUT" || context.Request.Method == "POST")
|
||||
{
|
||||
if (sqLiteException.Message.Contains("constraint failed"))
|
||||
return new ErrorModel
|
||||
{
|
||||
Message = exception.Message,
|
||||
}.AsResponse(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(HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user