2013-12-02 00:02:32 +00:00
|
|
|
using System.Data.SQLite;
|
2021-10-21 20:04:19 +00:00
|
|
|
using System.Net;
|
|
|
|
using System.Threading.Tasks;
|
2013-04-20 22:14:41 +00:00
|
|
|
using FluentValidation;
|
2021-10-21 20:04:19 +00:00
|
|
|
using Microsoft.AspNetCore.Diagnostics;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NLog;
|
2021-10-21 20:04:19 +00:00
|
|
|
using NzbDrone.Common.Serializer;
|
2019-07-01 01:50:01 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-09-16 18:14:09 +00:00
|
|
|
using NzbDrone.Core.Exceptions;
|
2018-11-23 07:03:32 +00:00
|
|
|
using Radarr.Http.Exceptions;
|
2013-01-19 04:46:43 +00:00
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
namespace Radarr.Http.ErrorManagement
|
2013-01-19 04:46:43 +00:00
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
public class RadarrErrorPipeline
|
2013-01-19 04:46:43 +00:00
|
|
|
{
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
public RadarrErrorPipeline(Logger logger)
|
2013-01-19 04:46:43 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
public async Task HandleException(HttpContext context)
|
2013-01-19 04:46:43 +00:00
|
|
|
{
|
2014-07-27 18:56:04 +00:00
|
|
|
_logger.Trace("Handling Exception");
|
2013-09-19 07:16:59 +00:00
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
var response = context.Response;
|
|
|
|
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
|
|
|
|
var exception = exceptionHandlerPathFeature?.Error;
|
|
|
|
|
|
|
|
var statusCode = HttpStatusCode.InternalServerError;
|
|
|
|
var errorModel = new ErrorModel
|
|
|
|
{
|
2021-10-24 20:35:22 +00:00
|
|
|
Message = exception?.Message,
|
|
|
|
Description = exception?.ToString()
|
2021-10-21 20:04:19 +00:00
|
|
|
};
|
|
|
|
|
2019-07-01 01:50:01 +00:00
|
|
|
if (exception is ApiException apiException)
|
2013-01-19 04:46:43 +00:00
|
|
|
{
|
2021-02-04 21:33:20 +00:00
|
|
|
_logger.Warn(apiException, "API Error:\n{0}", apiException.Message);
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
errorModel = new ErrorModel(apiException);
|
|
|
|
statusCode = apiException.StatusCode;
|
|
|
|
}
|
|
|
|
else if (exception is ValidationException validationException)
|
2013-04-20 22:14:41 +00:00
|
|
|
{
|
|
|
|
_logger.Warn("Invalid request {0}", validationException.Message);
|
2013-04-29 00:39:17 +00:00
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
response.StatusCode = (int)HttpStatusCode.BadRequest;
|
|
|
|
response.ContentType = "application/json";
|
|
|
|
await response.WriteAsync(STJson.ToJson(validationException.Errors));
|
|
|
|
return;
|
2013-04-20 22:14:41 +00:00
|
|
|
}
|
2021-10-21 20:04:19 +00:00
|
|
|
else if (exception is NzbDroneClientException clientException)
|
2013-09-09 06:40:24 +00:00
|
|
|
{
|
2021-10-21 20:04:19 +00:00
|
|
|
statusCode = clientException.StatusCode;
|
2013-09-09 06:40:24 +00:00
|
|
|
}
|
2021-10-24 20:35:22 +00:00
|
|
|
else if (exception is ModelNotFoundException)
|
2019-07-01 01:50:01 +00:00
|
|
|
{
|
2021-10-21 20:04:19 +00:00
|
|
|
statusCode = HttpStatusCode.NotFound;
|
2019-07-01 01:50:01 +00:00
|
|
|
}
|
2021-10-24 20:35:22 +00:00
|
|
|
else if (exception is ModelConflictException)
|
2019-07-01 01:50:01 +00:00
|
|
|
{
|
2021-10-21 20:04:19 +00:00
|
|
|
statusCode = HttpStatusCode.Conflict;
|
2019-07-01 01:50:01 +00:00
|
|
|
}
|
2021-10-21 20:04:19 +00:00
|
|
|
else if (exception is SQLiteException sqLiteException)
|
2014-08-02 06:09:59 +00:00
|
|
|
{
|
|
|
|
if (context.Request.Method == "PUT" || context.Request.Method == "POST")
|
2013-12-02 00:02:32 +00:00
|
|
|
{
|
|
|
|
if (sqLiteException.Message.Contains("constraint failed"))
|
2019-12-22 22:08:53 +00:00
|
|
|
{
|
2021-10-21 20:04:19 +00:00
|
|
|
statusCode = HttpStatusCode.Conflict;
|
2019-12-22 22:08:53 +00:00
|
|
|
}
|
2013-12-02 00:02:32 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
_logger.Error(sqLiteException, "[{0} {1}]", context.Request.Method, context.Request.Path);
|
2014-08-02 06:09:59 +00:00
|
|
|
}
|
2021-10-24 20:35:22 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.Fatal(exception, "Request Failed. {0} {1}", context.Request.Method, context.Request.Path);
|
|
|
|
}
|
2013-01-28 18:06:54 +00:00
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
await errorModel.WriteToResponse(response, statusCode);
|
2013-01-19 04:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-28 21:43:55 +00:00
|
|
|
}
|