2013-01-19 05:46:43 +01:00
|
|
|
|
using System;
|
2013-04-21 00:14:41 +02:00
|
|
|
|
using FluentValidation;
|
2013-01-19 05:46:43 +01:00
|
|
|
|
using NLog;
|
|
|
|
|
using Nancy;
|
2013-02-23 21:35:26 +01:00
|
|
|
|
using NzbDrone.Api.Extensions;
|
2013-08-02 09:16:37 +02:00
|
|
|
|
using HttpStatusCode = Nancy.HttpStatusCode;
|
2013-01-19 05:46:43 +01:00
|
|
|
|
|
2013-02-19 02:13:42 +01:00
|
|
|
|
namespace NzbDrone.Api.ErrorManagement
|
2013-01-19 05:46:43 +01:00
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public class NzbDroneErrorPipeline
|
2013-01-19 05:46:43 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public NzbDroneErrorPipeline(Logger logger)
|
2013-01-19 05:46:43 +01:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response HandleException(NancyContext context, Exception exception)
|
|
|
|
|
{
|
2013-01-20 01:19:27 +01:00
|
|
|
|
var apiException = exception as ApiException;
|
2013-01-28 19:06:54 +01:00
|
|
|
|
|
2013-01-20 01:19:27 +01:00
|
|
|
|
if (apiException != null)
|
2013-01-19 05:46:43 +01:00
|
|
|
|
{
|
2013-01-20 01:19:27 +01:00
|
|
|
|
_logger.WarnException("API Error", apiException);
|
|
|
|
|
return apiException.ToErrorResponse();
|
2013-01-19 05:46:43 +01:00
|
|
|
|
}
|
2013-01-28 19:06:54 +01:00
|
|
|
|
|
2013-04-21 00:14:41 +02:00
|
|
|
|
var validationException = exception as ValidationException;
|
|
|
|
|
|
|
|
|
|
if (validationException != null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("Invalid request {0}", validationException.Message);
|
2013-04-29 02:39:17 +02:00
|
|
|
|
|
2013-04-21 00:14:41 +02:00
|
|
|
|
return validationException.Errors.AsResponse(HttpStatusCode.BadRequest);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-04 04:15:46 +02:00
|
|
|
|
_logger.FatalException("Request Failed", exception);
|
2013-01-28 19:06:54 +01:00
|
|
|
|
|
|
|
|
|
return new ErrorModel()
|
|
|
|
|
{
|
2013-04-29 02:39:17 +02:00
|
|
|
|
Message = exception.Message,
|
|
|
|
|
Description = exception.ToString()
|
2013-01-28 19:06:54 +01:00
|
|
|
|
}.AsResponse(HttpStatusCode.InternalServerError);
|
2013-01-19 05:46:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|