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