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