Improve ErrorHandler and quieten logging

Closes #1301

(cherry picked from commit 5fb6b449507af0b238afe6b699d8f55cc5deb790)
This commit is contained in:
ta264
2021-11-09 21:35:20 +00:00
parent d1caed5c7d
commit 1835f15460
29 changed files with 50 additions and 74 deletions
+15 -16
View File
@@ -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);
}
}
}