diff --git a/src/Readarr.Api.V1/Author/AuthorController.cs b/src/Readarr.Api.V1/Author/AuthorController.cs index 9f958ccd7..55ca08bcd 100644 --- a/src/Readarr.Api.V1/Author/AuthorController.cs +++ b/src/Readarr.Api.V1/Author/AuthorController.cs @@ -95,7 +95,7 @@ namespace Readarr.Api.V1.Author PutValidator.RuleFor(s => s.Path).IsValidPath(); } - public override AuthorResource GetResourceById(int id) + protected override AuthorResource GetResourceById(int id) { var author = _authorService.GetAuthor(id); return GetAuthorResource(author); diff --git a/src/Readarr.Api.V1/BookFiles/BookFileController.cs b/src/Readarr.Api.V1/BookFiles/BookFileController.cs index d633fc0ae..24a877e8c 100644 --- a/src/Readarr.Api.V1/BookFiles/BookFileController.cs +++ b/src/Readarr.Api.V1/BookFiles/BookFileController.cs @@ -60,7 +60,7 @@ namespace Readarr.Api.V1.BookFiles } } - public override BookFileResource GetResourceById(int id) + protected override BookFileResource GetResourceById(int id) { var resource = MapToResource(_mediaFileService.Get(id)); resource.AudioTags = _metadataTagService.ReadTags((FileInfoBase)new FileInfo(resource.Path)); diff --git a/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs b/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs index ca2643137..f3fab4e93 100644 --- a/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs +++ b/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs @@ -33,7 +33,7 @@ namespace Readarr.Api.V1.Books _qualityUpgradableSpecification = qualityUpgradableSpecification; } - public override BookResource GetResourceById(int id) + protected override BookResource GetResourceById(int id) { var book = _bookService.GetBook(id); var resource = MapToResource(book, true); diff --git a/src/Readarr.Api.V1/Commands/CommandController.cs b/src/Readarr.Api.V1/Commands/CommandController.cs index 46ae8fe45..54244b7d0 100644 --- a/src/Readarr.Api.V1/Commands/CommandController.cs +++ b/src/Readarr.Api.V1/Commands/CommandController.cs @@ -41,7 +41,7 @@ namespace Readarr.Api.V1.Commands _pendingUpdates = new Dictionary(); } - public override CommandResource GetResourceById(int id) + protected override CommandResource GetResourceById(int id) { return _commandQueueManager.Get(id).ToResource(); } diff --git a/src/Readarr.Api.V1/Config/ConfigController.cs b/src/Readarr.Api.V1/Config/ConfigController.cs index ebdf666a8..6c60f2c9d 100644 --- a/src/Readarr.Api.V1/Config/ConfigController.cs +++ b/src/Readarr.Api.V1/Config/ConfigController.cs @@ -17,7 +17,7 @@ namespace Readarr.Api.V1.Config _configService = configService; } - public override TResource GetResourceById(int id) + protected override TResource GetResourceById(int id) { return GetConfig(); } diff --git a/src/Readarr.Api.V1/Config/DevelopmentConfigController.cs b/src/Readarr.Api.V1/Config/DevelopmentConfigController.cs index 3a9eaf8d2..1d3548fb2 100644 --- a/src/Readarr.Api.V1/Config/DevelopmentConfigController.cs +++ b/src/Readarr.Api.V1/Config/DevelopmentConfigController.cs @@ -26,7 +26,7 @@ namespace Prowlarr.Api.V1.Config SharedValidator.RuleFor(c => c.MetadataSource).IsValidUrl().When(c => !c.MetadataSource.IsNullOrWhiteSpace()); } - public override DevelopmentConfigResource GetResourceById(int id) + protected override DevelopmentConfigResource GetResourceById(int id) { return GetDevelopmentConfig(); } diff --git a/src/Readarr.Api.V1/Config/HostConfigController.cs b/src/Readarr.Api.V1/Config/HostConfigController.cs index ff23a3f7a..6f7df1600 100644 --- a/src/Readarr.Api.V1/Config/HostConfigController.cs +++ b/src/Readarr.Api.V1/Config/HostConfigController.cs @@ -78,7 +78,7 @@ namespace Readarr.Api.V1.Config return cert != null; } - public override HostConfigResource GetResourceById(int id) + protected override HostConfigResource GetResourceById(int id) { return GetHostConfig(); } diff --git a/src/Readarr.Api.V1/Config/NamingConfigController.cs b/src/Readarr.Api.V1/Config/NamingConfigController.cs index 2a2796c33..a762c040a 100644 --- a/src/Readarr.Api.V1/Config/NamingConfigController.cs +++ b/src/Readarr.Api.V1/Config/NamingConfigController.cs @@ -33,7 +33,7 @@ namespace Readarr.Api.V1.Config SharedValidator.RuleFor(c => c.AuthorFolderFormat).ValidAuthorFolderFormat(); } - public override NamingConfigResource GetResourceById(int id) + protected override NamingConfigResource GetResourceById(int id) { return GetNamingConfig(); } diff --git a/src/Readarr.Api.V1/CustomFilters/CustomFilterController.cs b/src/Readarr.Api.V1/CustomFilters/CustomFilterController.cs index fd64d5ff6..6c14728e6 100644 --- a/src/Readarr.Api.V1/CustomFilters/CustomFilterController.cs +++ b/src/Readarr.Api.V1/CustomFilters/CustomFilterController.cs @@ -17,7 +17,7 @@ namespace Readarr.Api.V1.CustomFilters _customFilterService = customFilterService; } - public override CustomFilterResource GetResourceById(int id) + protected override CustomFilterResource GetResourceById(int id) { return _customFilterService.Get(id).ToResource(); } diff --git a/src/Readarr.Api.V1/Health/HealthController.cs b/src/Readarr.Api.V1/Health/HealthController.cs index cac947fd4..f9dfa503e 100644 --- a/src/Readarr.Api.V1/Health/HealthController.cs +++ b/src/Readarr.Api.V1/Health/HealthController.cs @@ -22,7 +22,7 @@ namespace Readarr.Api.V1.Health _healthCheckService = healthCheckService; } - public override HealthResource GetResourceById(int id) + protected override HealthResource GetResourceById(int id) { throw new NotImplementedException(); } diff --git a/src/Readarr.Api.V1/ImportLists/ImportListExclusionController.cs b/src/Readarr.Api.V1/ImportLists/ImportListExclusionController.cs index 1b2e2bd10..43e72ee25 100644 --- a/src/Readarr.Api.V1/ImportLists/ImportListExclusionController.cs +++ b/src/Readarr.Api.V1/ImportLists/ImportListExclusionController.cs @@ -24,7 +24,7 @@ namespace Readarr.Api.V1.ImportLists SharedValidator.RuleFor(c => c.AuthorName).NotEmpty(); } - public override ImportListExclusionResource GetResourceById(int id) + protected override ImportListExclusionResource GetResourceById(int id) { return _importListExclusionService.Get(id).ToResource(); } diff --git a/src/Readarr.Api.V1/Indexers/ReleaseModuleBase.cs b/src/Readarr.Api.V1/Indexers/ReleaseModuleBase.cs index ea00b5446..b7341e8d6 100644 --- a/src/Readarr.Api.V1/Indexers/ReleaseModuleBase.cs +++ b/src/Readarr.Api.V1/Indexers/ReleaseModuleBase.cs @@ -7,7 +7,7 @@ namespace Readarr.Api.V1.Indexers { public abstract class ReleaseControllerBase : RestController { - public override ReleaseResource GetResourceById(int id) + protected override ReleaseResource GetResourceById(int id) { throw new NotImplementedException(); } diff --git a/src/Readarr.Api.V1/Languages/LanguageController.cs b/src/Readarr.Api.V1/Languages/LanguageController.cs index 9545b2c10..80089973f 100644 --- a/src/Readarr.Api.V1/Languages/LanguageController.cs +++ b/src/Readarr.Api.V1/Languages/LanguageController.cs @@ -10,7 +10,7 @@ namespace Readarr.Api.V1.Languages [V1ApiController] public class LanguageController : RestController { - public override LanguageResource GetResourceById(int id) + protected override LanguageResource GetResourceById(int id) { var language = (Language)id; diff --git a/src/Readarr.Api.V1/Profiles/Delay/DelayProfileController.cs b/src/Readarr.Api.V1/Profiles/Delay/DelayProfileController.cs index 373c986ab..8b400d570 100644 --- a/src/Readarr.Api.V1/Profiles/Delay/DelayProfileController.cs +++ b/src/Readarr.Api.V1/Profiles/Delay/DelayProfileController.cs @@ -61,7 +61,7 @@ namespace Readarr.Api.V1.Profiles.Delay return Accepted(model.Id); } - public override DelayProfileResource GetResourceById(int id) + protected override DelayProfileResource GetResourceById(int id) { return _delayProfileService.Get(id).ToResource(); } diff --git a/src/Readarr.Api.V1/Profiles/Metadata/MetadataProfileController.cs b/src/Readarr.Api.V1/Profiles/Metadata/MetadataProfileController.cs index aad55ecad..ab2c9f2ad 100644 --- a/src/Readarr.Api.V1/Profiles/Metadata/MetadataProfileController.cs +++ b/src/Readarr.Api.V1/Profiles/Metadata/MetadataProfileController.cs @@ -53,7 +53,7 @@ namespace Readarr.Api.V1.Profiles.Metadata return Accepted(model.Id); } - public override MetadataProfileResource GetResourceById(int id) + protected override MetadataProfileResource GetResourceById(int id) { return _profileService.Get(id).ToResource(); } diff --git a/src/Readarr.Api.V1/Profiles/Quality/QualityProfileController.cs b/src/Readarr.Api.V1/Profiles/Quality/QualityProfileController.cs index 21fbe20db..5851a071b 100644 --- a/src/Readarr.Api.V1/Profiles/Quality/QualityProfileController.cs +++ b/src/Readarr.Api.V1/Profiles/Quality/QualityProfileController.cs @@ -45,7 +45,7 @@ namespace Readarr.Api.V1.Profiles.Quality return Accepted(model.Id); } - public override QualityProfileResource GetResourceById(int id) + protected override QualityProfileResource GetResourceById(int id) { return _profileService.Get(id).ToResource(); } diff --git a/src/Readarr.Api.V1/Profiles/Release/ReleaseProfileController.cs b/src/Readarr.Api.V1/Profiles/Release/ReleaseProfileController.cs index c211d9763..573ca4f2a 100644 --- a/src/Readarr.Api.V1/Profiles/Release/ReleaseProfileController.cs +++ b/src/Readarr.Api.V1/Profiles/Release/ReleaseProfileController.cs @@ -41,7 +41,7 @@ namespace Readarr.Api.V1.Profiles.Release }); } - public override ReleaseProfileResource GetResourceById(int id) + protected override ReleaseProfileResource GetResourceById(int id) { return _releaseProfileService.Get(id).ToResource(); } diff --git a/src/Readarr.Api.V1/ProviderControllerBase.cs b/src/Readarr.Api.V1/ProviderControllerBase.cs index 786b2ced8..d2af243e2 100644 --- a/src/Readarr.Api.V1/ProviderControllerBase.cs +++ b/src/Readarr.Api.V1/ProviderControllerBase.cs @@ -32,7 +32,7 @@ namespace Readarr.Api.V1 PostValidator.RuleFor(c => c.Fields).NotNull(); } - public override TProviderResource GetResourceById(int id) + protected override TProviderResource GetResourceById(int id) { var definition = _providerFactory.Get(id); _providerFactory.SetProviderCharacteristics(definition); diff --git a/src/Readarr.Api.V1/Qualities/QualityDefinitionController.cs b/src/Readarr.Api.V1/Qualities/QualityDefinitionController.cs index 49f25f9e1..56e861031 100644 --- a/src/Readarr.Api.V1/Qualities/QualityDefinitionController.cs +++ b/src/Readarr.Api.V1/Qualities/QualityDefinitionController.cs @@ -26,7 +26,7 @@ namespace Readarr.Api.V1.Qualities return Accepted(model.Id); } - public override QualityDefinitionResource GetResourceById(int id) + protected override QualityDefinitionResource GetResourceById(int id) { return _qualityDefinitionService.GetById(id).ToResource(); } diff --git a/src/Readarr.Api.V1/Queue/QueueController.cs b/src/Readarr.Api.V1/Queue/QueueController.cs index 528ba146b..a6a1444e6 100644 --- a/src/Readarr.Api.V1/Queue/QueueController.cs +++ b/src/Readarr.Api.V1/Queue/QueueController.cs @@ -53,7 +53,7 @@ namespace Readarr.Api.V1.Queue _qualityComparer = new QualityModelComparer(qualityProfileService.GetDefaultProfile(string.Empty)); } - public override QueueResource GetResourceById(int id) + protected override QueueResource GetResourceById(int id) { throw new NotImplementedException(); } diff --git a/src/Readarr.Api.V1/Queue/QueueDetailsController.cs b/src/Readarr.Api.V1/Queue/QueueDetailsController.cs index 22789b29d..258bc065c 100644 --- a/src/Readarr.Api.V1/Queue/QueueDetailsController.cs +++ b/src/Readarr.Api.V1/Queue/QueueDetailsController.cs @@ -26,7 +26,7 @@ namespace Readarr.Api.V1.Queue _pendingReleaseService = pendingReleaseService; } - public override QueueResource GetResourceById(int id) + protected override QueueResource GetResourceById(int id) { throw new NotImplementedException(); } diff --git a/src/Readarr.Api.V1/Queue/QueueStatusController.cs b/src/Readarr.Api.V1/Queue/QueueStatusController.cs index 39d7b6a71..10ff444b1 100644 --- a/src/Readarr.Api.V1/Queue/QueueStatusController.cs +++ b/src/Readarr.Api.V1/Queue/QueueStatusController.cs @@ -30,7 +30,7 @@ namespace Readarr.Api.V1.Queue _broadcastDebounce = new Debouncer(BroadcastChange, TimeSpan.FromSeconds(5)); } - public override QueueStatusResource GetResourceById(int id) + protected override QueueStatusResource GetResourceById(int id) { throw new NotImplementedException(); } diff --git a/src/Readarr.Api.V1/RemotePathMappings/RemotePathMappingController.cs b/src/Readarr.Api.V1/RemotePathMappings/RemotePathMappingController.cs index 58aa7f06d..9289b50b8 100644 --- a/src/Readarr.Api.V1/RemotePathMappings/RemotePathMappingController.cs +++ b/src/Readarr.Api.V1/RemotePathMappings/RemotePathMappingController.cs @@ -34,7 +34,7 @@ namespace Readarr.Api.V1.RemotePathMappings .SetValidator(pathExistsValidator); } - public override RemotePathMappingResource GetResourceById(int id) + protected override RemotePathMappingResource GetResourceById(int id) { return _remotePathMappingService.Get(id).ToResource(); } diff --git a/src/Readarr.Api.V1/RootFolders/RootFolderController.cs b/src/Readarr.Api.V1/RootFolders/RootFolderController.cs index f81d73e06..ecf3b26c3 100644 --- a/src/Readarr.Api.V1/RootFolders/RootFolderController.cs +++ b/src/Readarr.Api.V1/RootFolders/RootFolderController.cs @@ -92,7 +92,7 @@ namespace Readarr.Api.V1.RootFolders return HttpUri.CombinePath(HttpRequestBuilder.BuildBaseUrl(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase), settings.Library); } - public override RootFolderResource GetResourceById(int id) + protected override RootFolderResource GetResourceById(int id) { return _rootFolderService.Get(id).ToResource(); } diff --git a/src/Readarr.Api.V1/System/Tasks/TaskController.cs b/src/Readarr.Api.V1/System/Tasks/TaskController.cs index 69b0b02e9..fdb22f19d 100644 --- a/src/Readarr.Api.V1/System/Tasks/TaskController.cs +++ b/src/Readarr.Api.V1/System/Tasks/TaskController.cs @@ -31,7 +31,7 @@ namespace Readarr.Api.V1.System.Tasks .ToList(); } - public override TaskResource GetResourceById(int id) + protected override TaskResource GetResourceById(int id) { var task = _taskManager.GetAll() .SingleOrDefault(t => t.Id == id); diff --git a/src/Readarr.Api.V1/Tags/TagController.cs b/src/Readarr.Api.V1/Tags/TagController.cs index 5a738cb03..5aba8de20 100644 --- a/src/Readarr.Api.V1/Tags/TagController.cs +++ b/src/Readarr.Api.V1/Tags/TagController.cs @@ -23,7 +23,7 @@ namespace Readarr.Api.V1.Tags _tagService = tagService; } - public override TagResource GetResourceById(int id) + protected override TagResource GetResourceById(int id) { return _tagService.GetTag(id).ToResource(); } diff --git a/src/Readarr.Api.V1/Tags/TagDetailsController.cs b/src/Readarr.Api.V1/Tags/TagDetailsController.cs index 2708186ca..a195b4255 100644 --- a/src/Readarr.Api.V1/Tags/TagDetailsController.cs +++ b/src/Readarr.Api.V1/Tags/TagDetailsController.cs @@ -16,7 +16,7 @@ namespace Readarr.Api.V1.Tags _tagService = tagService; } - public override TagDetailsResource GetResourceById(int id) + protected override TagDetailsResource GetResourceById(int id) { return _tagService.Details(id).ToResource(); } diff --git a/src/Readarr.Http/ErrorManagement/ReadarrErrorPipeline.cs b/src/Readarr.Http/ErrorManagement/ReadarrErrorPipeline.cs index ca2f92ad1..4911260e0 100644 --- a/src/Readarr.Http/ErrorManagement/ReadarrErrorPipeline.cs +++ b/src/Readarr.Http/ErrorManagement/ReadarrErrorPipeline.cs @@ -29,22 +29,17 @@ namespace Readarr.Http.ErrorManagement var exceptionHandlerPathFeature = context.Features.Get(); var exception = exceptionHandlerPathFeature?.Error; - _logger.Warn(exception); - var statusCode = HttpStatusCode.InternalServerError; var errorModel = new ErrorModel { - Message = exception.Message, - Description = exception.ToString() + Message = exception?.Message, + Description = exception?.ToString() }; if (exception is ApiException apiException) { _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); statusCode = apiException.StatusCode; } @@ -59,30 +54,14 @@ namespace Readarr.Http.ErrorManagement } else if (exception is NzbDroneClientException clientException) { - errorModel = new ErrorModel - { - Message = exception.Message, - Description = exception.ToString() - }; 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; } - 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; } else if (exception is SQLiteException sqLiteException) @@ -91,18 +70,16 @@ namespace Readarr.Http.ErrorManagement { if (sqLiteException.Message.Contains("constraint failed")) { - errorModel = new ErrorModel - { - Message = exception.Message, - }; statusCode = HttpStatusCode.Conflict; } } _logger.Error(sqLiteException, "[{0} {1}]", context.Request.Method, context.Request.Path); } - - _logger.Fatal(exception, "Request Failed. {0} {1}", context.Request.Method, context.Request.Path); + else + { + _logger.Fatal(exception, "Request Failed. {0} {1}", context.Request.Method, context.Request.Path); + } await errorModel.WriteToResponse(response, statusCode); } diff --git a/src/Readarr.Http/REST/RestController.cs b/src/Readarr.Http/REST/RestController.cs index 0752cfe86..eb4f2b414 100644 --- a/src/Readarr.Http/REST/RestController.cs +++ b/src/Readarr.Http/REST/RestController.cs @@ -39,7 +39,19 @@ namespace Readarr.Http.REST } [RestGetById] - public abstract TResource GetResourceById(int id); + public ActionResult GetResourceByIdWithErrorHandler(int id) + { + try + { + return GetResourceById(id); + } + catch (ModelNotFoundException) + { + return NotFound(); + } + } + + protected abstract TResource GetResourceById(int id); public override void OnActionExecuting(ActionExecutingContext context) { @@ -73,19 +85,6 @@ namespace Readarr.Http.REST 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) { if (resource == null) @@ -118,13 +117,13 @@ namespace Readarr.Http.REST protected ActionResult Accepted(int id) { var result = GetResourceById(id); - return AcceptedAtAction(nameof(GetResourceById), new { id = id }, result); + return AcceptedAtAction(nameof(GetResourceByIdWithErrorHandler), new { id = id }, result); } protected ActionResult Created(int id) { var result = GetResourceById(id); - return CreatedAtAction(nameof(GetResourceById), new { id = id }, result); + return CreatedAtAction(nameof(GetResourceByIdWithErrorHandler), new { id = id }, result); } } }