mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-21 22:04:31 -04:00
Improve ErrorHandler and quieten logging
Closes #1301 (cherry picked from commit 5fb6b449507af0b238afe6b699d8f55cc5deb790)
This commit is contained in:
@@ -39,7 +39,19 @@ namespace Readarr.Http.REST
|
||||
}
|
||||
|
||||
[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)
|
||||
{
|
||||
@@ -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<TResource> 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<TResource> Created(int id)
|
||||
{
|
||||
var result = GetResourceById(id);
|
||||
return CreatedAtAction(nameof(GetResourceById), new { id = id }, result);
|
||||
return CreatedAtAction(nameof(GetResourceByIdWithErrorHandler), new { id = id }, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user