Improve ErrorHandler and quieten logging

[common]
This commit is contained in:
ta264 2021-10-24 21:35:22 +01:00 committed by Qstick
parent bb4c1d6181
commit 5fb6b44950
33 changed files with 54 additions and 78 deletions

View File

@ -35,7 +35,7 @@ namespace Radarr.Api.V3.Calendar
_configService = configService; _configService = configService;
} }
public override MovieResource GetResourceById(int id) protected override MovieResource GetResourceById(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -41,7 +41,7 @@ namespace Radarr.Api.V3.Commands
PostValidator.RuleFor(c => c.Name).NotBlank(); PostValidator.RuleFor(c => c.Name).NotBlank();
} }
public override CommandResource GetResourceById(int id) protected override CommandResource GetResourceById(int id)
{ {
return _commandQueueManager.Get(id).ToResource(); return _commandQueueManager.Get(id).ToResource();
} }

View File

@ -17,7 +17,7 @@ namespace Radarr.Api.V3.Config
_configService = configService; _configService = configService;
} }
public override TResource GetResourceById(int id) protected override TResource GetResourceById(int id)
{ {
return GetConfig(); return GetConfig();
} }

View File

@ -78,7 +78,7 @@ namespace Radarr.Api.V3.Config
return cert != null; return cert != null;
} }
public override HostConfigResource GetResourceById(int id) protected override HostConfigResource GetResourceById(int id)
{ {
return GetHostConfig(); return GetHostConfig();
} }

View File

@ -33,7 +33,7 @@ namespace Radarr.Api.V3.Config
SharedValidator.RuleFor(c => c.MovieFolderFormat).ValidMovieFolderFormat(); SharedValidator.RuleFor(c => c.MovieFolderFormat).ValidMovieFolderFormat();
} }
public override NamingConfigResource GetResourceById(int id) protected override NamingConfigResource GetResourceById(int id)
{ {
return GetNamingConfig(); return GetNamingConfig();
} }

View File

@ -16,7 +16,7 @@ namespace Radarr.Api.V3.Credits
_creditService = creditService; _creditService = creditService;
} }
public override CreditResource GetResourceById(int id) protected override CreditResource GetResourceById(int id)
{ {
return _creditService.GetById(id).ToResource(); return _creditService.GetById(id).ToResource();
} }

View File

@ -17,7 +17,7 @@ namespace Radarr.Api.V3.CustomFilters
_customFilterService = customFilterService; _customFilterService = customFilterService;
} }
public override CustomFilterResource GetResourceById(int id) protected override CustomFilterResource GetResourceById(int id)
{ {
return _customFilterService.Get(id).ToResource(); return _customFilterService.Get(id).ToResource();
} }

View File

@ -27,7 +27,7 @@ namespace Radarr.Api.V3.CustomFormats
SharedValidator.RuleFor(c => c.Specifications).NotEmpty(); SharedValidator.RuleFor(c => c.Specifications).NotEmpty();
} }
public override CustomFormatResource GetResourceById(int id) protected override CustomFormatResource GetResourceById(int id)
{ {
return _formatService.GetById(id).ToResource(); return _formatService.GetById(id).ToResource();
} }

View File

@ -22,7 +22,7 @@ namespace Radarr.Api.V3.Health
_healthCheckService = healthCheckService; _healthCheckService = healthCheckService;
} }
public override HealthResource GetResourceById(int id) protected override HealthResource GetResourceById(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -28,7 +28,7 @@ namespace Radarr.Api.V3.ImportLists
return _exclusionService.GetAllExclusions().ToResource(); return _exclusionService.GetAllExclusions().ToResource();
} }
public override ImportExclusionsResource GetResourceById(int id) protected override ImportExclusionsResource GetResourceById(int id)
{ {
return _exclusionService.GetById(id).ToResource(); return _exclusionService.GetById(id).ToResource();
} }

View File

@ -15,7 +15,7 @@ namespace Radarr.Api.V3.Indexers
_qualityProfile = qualityProfileService.GetDefaultProfile(string.Empty); _qualityProfile = qualityProfileService.GetDefaultProfile(string.Empty);
} }
public override ReleaseResource GetResourceById(int id) protected override ReleaseResource GetResourceById(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -47,7 +47,7 @@ namespace Radarr.Api.V3.MovieFiles
_qualityUpgradableSpecification = qualityUpgradableSpecification; _qualityUpgradableSpecification = qualityUpgradableSpecification;
} }
public override MovieFileResource GetResourceById(int id) protected override MovieFileResource GetResourceById(int id)
{ {
var movieFile = _mediaFileService.GetMovie(id); var movieFile = _mediaFileService.GetMovie(id);
var movie = _movieService.GetMovie(movieFile.MovieId); var movie = _movieService.GetMovie(movieFile.MovieId);

View File

@ -17,7 +17,7 @@ namespace Radarr.Api.V3.Movies
_altTitleService = altTitleService; _altTitleService = altTitleService;
} }
public override AlternativeTitleResource GetResourceById(int id) protected override AlternativeTitleResource GetResourceById(int id)
{ {
return _altTitleService.GetById(id).ToResource(); return _altTitleService.GetById(id).ToResource();
} }

View File

@ -14,7 +14,7 @@ namespace Radarr.Api.V3.Movies
_yearCache = cacheManager.GetCache<int>(GetType(), "altYears"); _yearCache = cacheManager.GetCache<int>(GetType(), "altYears");
} }
public override AlternativeYearResource GetResourceById(int id) protected override AlternativeYearResource GetResourceById(int id)
{ {
return new AlternativeYearResource return new AlternativeYearResource
{ {

View File

@ -150,7 +150,7 @@ namespace Radarr.Api.V3.Movies
return moviesResources; return moviesResources;
} }
public override MovieResource GetResourceById(int id) protected override MovieResource GetResourceById(int id)
{ {
var movie = _moviesService.GetMovie(id); var movie = _moviesService.GetMovie(id);
return MapToResource(movie); return MapToResource(movie);

View File

@ -17,7 +17,7 @@ namespace Radarr.Api.V3.Movies
_addMovieService = addMovieService; _addMovieService = addMovieService;
} }
public override MovieResource GetResourceById(int id) protected override MovieResource GetResourceById(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -35,7 +35,7 @@ namespace Radarr.Api.V3.Movies
_configService = configService; _configService = configService;
} }
public override MovieResource GetResourceById(int id) protected override MovieResource GetResourceById(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -61,7 +61,7 @@ namespace Radarr.Api.V3.Profiles.Delay
return Accepted(model.Id); return Accepted(model.Id);
} }
public override DelayProfileResource GetResourceById(int id) protected override DelayProfileResource GetResourceById(int id)
{ {
return _delayProfileService.Get(id).ToResource(); return _delayProfileService.Get(id).ToResource();
} }

View File

@ -10,7 +10,7 @@ namespace Radarr.Api.V3.Profiles.Languages
[V3ApiController] [V3ApiController]
public class LanguageController : RestController<LanguageResource> public class LanguageController : RestController<LanguageResource>
{ {
public override LanguageResource GetResourceById(int id) protected override LanguageResource GetResourceById(int id)
{ {
var language = (Language)id; var language = (Language)id;

View File

@ -68,7 +68,7 @@ namespace Radarr.Api.V3.Profiles.Quality
return Accepted(model.Id); return Accepted(model.Id);
} }
public override QualityProfileResource GetResourceById(int id) protected override QualityProfileResource GetResourceById(int id)
{ {
return _profileService.Get(id).ToResource(); return _profileService.Get(id).ToResource();
} }

View File

@ -33,7 +33,7 @@ namespace Radarr.Api.V3
PostValidator.RuleFor(c => c.Fields).NotNull(); PostValidator.RuleFor(c => c.Fields).NotNull();
} }
public override TProviderResource GetResourceById(int id) protected override TProviderResource GetResourceById(int id)
{ {
var definition = _providerFactory.Get(id); var definition = _providerFactory.Get(id);
_providerFactory.SetProviderCharacteristics(definition); _providerFactory.SetProviderCharacteristics(definition);

View File

@ -26,7 +26,7 @@ namespace Radarr.Api.V3.Qualities
return Accepted(model.Id); return Accepted(model.Id);
} }
public override QualityDefinitionResource GetResourceById(int id) protected override QualityDefinitionResource GetResourceById(int id)
{ {
return _qualityDefinitionService.GetById(id).ToResource(); return _qualityDefinitionService.GetById(id).ToResource();
} }

View File

@ -54,7 +54,7 @@ namespace Radarr.Api.V3.Queue
_qualityComparer = new QualityModelComparer(qualityProfileService.GetDefaultProfile(string.Empty)); _qualityComparer = new QualityModelComparer(qualityProfileService.GetDefaultProfile(string.Empty));
} }
public override QueueResource GetResourceById(int id) protected override QueueResource GetResourceById(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -26,7 +26,7 @@ namespace Radarr.Api.V3.Queue
_pendingReleaseService = pendingReleaseService; _pendingReleaseService = pendingReleaseService;
} }
public override QueueResource GetResourceById(int id) protected override QueueResource GetResourceById(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -30,7 +30,7 @@ namespace Radarr.Api.V3.Queue
_broadcastDebounce = new Debouncer(BroadcastChange, TimeSpan.FromSeconds(5)); _broadcastDebounce = new Debouncer(BroadcastChange, TimeSpan.FromSeconds(5));
} }
public override QueueStatusResource GetResourceById(int id) protected override QueueStatusResource GetResourceById(int id)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -34,7 +34,7 @@ namespace Radarr.Api.V3.RemotePathMappings
.SetValidator(pathExistsValidator); .SetValidator(pathExistsValidator);
} }
public override RemotePathMappingResource GetResourceById(int id) protected override RemotePathMappingResource GetResourceById(int id)
{ {
return _remotePathMappingService.Get(id).ToResource(); return _remotePathMappingService.Get(id).ToResource();
} }

View File

@ -27,7 +27,7 @@ namespace Radarr.Api.V3.Restrictions
}); });
} }
public override RestrictionResource GetResourceById(int id) protected override RestrictionResource GetResourceById(int id)
{ {
return _restrictionService.Get(id).ToResource(); return _restrictionService.Get(id).ToResource();
} }

View File

@ -41,7 +41,7 @@ namespace Radarr.Api.V3.RootFolders
.SetValidator(folderWritableValidator); .SetValidator(folderWritableValidator);
} }
public override RootFolderResource GetResourceById(int id) protected override RootFolderResource GetResourceById(int id)
{ {
var timeout = Request?.GetBooleanQueryParameter("timeout", true) ?? true; var timeout = Request?.GetBooleanQueryParameter("timeout", true) ?? true;

View File

@ -31,7 +31,7 @@ namespace Radarr.Api.V3.System.Tasks
.ToList(); .ToList();
} }
public override TaskResource GetResourceById(int id) protected override TaskResource GetResourceById(int id)
{ {
var task = _taskManager.GetAll() var task = _taskManager.GetAll()
.SingleOrDefault(t => t.Id == id); .SingleOrDefault(t => t.Id == id);

View File

@ -22,7 +22,7 @@ namespace Radarr.Api.V3.Tags
_tagService = tagService; _tagService = tagService;
} }
public override TagResource GetResourceById(int id) protected override TagResource GetResourceById(int id)
{ {
return _tagService.GetTag(id).ToResource(); return _tagService.GetTag(id).ToResource();
} }

View File

@ -16,7 +16,7 @@ namespace Radarr.Api.V3.Tags
_tagService = tagService; _tagService = tagService;
} }
public override TagDetailsResource GetResourceById(int id) protected override TagDetailsResource GetResourceById(int id)
{ {
return _tagService.Details(id).ToResource(); return _tagService.Details(id).ToResource();
} }

View File

@ -29,22 +29,17 @@ namespace Radarr.Http.ErrorManagement
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>(); var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
var exception = exceptionHandlerPathFeature?.Error; var exception = exceptionHandlerPathFeature?.Error;
_logger.Warn(exception);
var statusCode = HttpStatusCode.InternalServerError; var statusCode = HttpStatusCode.InternalServerError;
var errorModel = new ErrorModel var errorModel = new ErrorModel
{ {
Message = exception.Message, Message = exception?.Message,
Description = exception.ToString() Description = exception?.ToString()
}; };
if (exception is ApiException apiException) if (exception is ApiException apiException)
{ {
_logger.Warn(apiException, "API Error:\n{0}", apiException.Message); _logger.Warn(apiException, "API Error:\n{0}", apiException.Message);
/* var body = RequestStream.FromStream(context.Request.Body).AsString();
_logger.Trace("Request body:\n{0}", body);*/
errorModel = new ErrorModel(apiException); errorModel = new ErrorModel(apiException);
statusCode = apiException.StatusCode; statusCode = apiException.StatusCode;
} }
@ -59,30 +54,14 @@ namespace Radarr.Http.ErrorManagement
} }
else if (exception is NzbDroneClientException clientException) else if (exception is NzbDroneClientException clientException)
{ {
errorModel = new ErrorModel
{
Message = exception.Message,
Description = exception.ToString()
};
statusCode = clientException.StatusCode; statusCode = clientException.StatusCode;
} }
else if (exception is ModelNotFoundException notFoundException) else if (exception is ModelNotFoundException)
{ {
errorModel = new ErrorModel
{
Message = exception.Message,
Description = exception.ToString()
};
statusCode = HttpStatusCode.NotFound; statusCode = HttpStatusCode.NotFound;
} }
else if (exception is ModelConflictException conflictException) else if (exception is ModelConflictException)
{ {
_logger.Error(exception, "DB error");
errorModel = new ErrorModel
{
Message = exception.Message,
Description = exception.ToString()
};
statusCode = HttpStatusCode.Conflict; statusCode = HttpStatusCode.Conflict;
} }
else if (exception is SQLiteException sqLiteException) else if (exception is SQLiteException sqLiteException)
@ -91,18 +70,16 @@ namespace Radarr.Http.ErrorManagement
{ {
if (sqLiteException.Message.Contains("constraint failed")) if (sqLiteException.Message.Contains("constraint failed"))
{ {
errorModel = new ErrorModel
{
Message = exception.Message,
};
statusCode = HttpStatusCode.Conflict; statusCode = HttpStatusCode.Conflict;
} }
} }
_logger.Error(sqLiteException, "[{0} {1}]", context.Request.Method, context.Request.Path); _logger.Error(sqLiteException, "[{0} {1}]", context.Request.Method, context.Request.Path);
} }
else
_logger.Fatal(exception, "Request Failed. {0} {1}", context.Request.Method, context.Request.Path); {
_logger.Fatal(exception, "Request Failed. {0} {1}", context.Request.Method, context.Request.Path);
}
await errorModel.WriteToResponse(response, statusCode); await errorModel.WriteToResponse(response, statusCode);
} }

View File

@ -39,7 +39,19 @@ namespace Radarr.Http.REST
} }
[RestGetById] [RestGetById]
public abstract TResource GetResourceById(int id); public ActionResult<TResource> GetResourceByIdWithErrorHandler(int id)
{
try
{
return GetResourceById(id);
}
catch (ModelNotFoundException)
{
return NotFound();
}
}
protected abstract TResource GetResourceById(int id);
public override void OnActionExecuting(ActionExecutingContext context) public override void OnActionExecuting(ActionExecutingContext context)
{ {
@ -73,19 +85,6 @@ namespace Radarr.Http.REST
base.OnActionExecuting(context); base.OnActionExecuting(context);
} }
public override void OnActionExecuted(ActionExecutedContext context)
{
var descriptor = context.ActionDescriptor as ControllerActionDescriptor;
var attributes = descriptor.MethodInfo.CustomAttributes;
if (context.Exception?.GetType() == typeof(ModelNotFoundException) &&
attributes.Any(x => x.AttributeType == typeof(RestGetByIdAttribute)))
{
context.Result = new NotFoundResult();
}
}
protected void ValidateResource(TResource resource, bool skipValidate = false, bool skipSharedValidate = false) protected void ValidateResource(TResource resource, bool skipValidate = false, bool skipSharedValidate = false)
{ {
if (resource == null) if (resource == null)
@ -118,13 +117,13 @@ namespace Radarr.Http.REST
protected ActionResult<TResource> Accepted(int id) protected ActionResult<TResource> Accepted(int id)
{ {
var result = GetResourceById(id); var result = GetResourceById(id);
return AcceptedAtAction(nameof(GetResourceById), new { id = id }, result); return AcceptedAtAction(nameof(GetResourceByIdWithErrorHandler), new { id = id }, result);
} }
protected ActionResult<TResource> Created(int id) protected ActionResult<TResource> Created(int id)
{ {
var result = GetResourceById(id); var result = GetResourceById(id);
return CreatedAtAction(nameof(GetResourceById), new { id = id }, result); return CreatedAtAction(nameof(GetResourceByIdWithErrorHandler), new { id = id }, result);
} }
} }
} }