From 36d0e612fac3e92bec5c32e9dc7c372f666e5495 Mon Sep 17 00:00:00 2001 From: da3dsoul Date: Fri, 6 Oct 2023 13:27:22 -0400 Subject: [PATCH] Better messaging in APIv3 for Expression Validation Errors --- .../API/v3/Controllers/FilterController.cs | 62 +++++++++++++------ 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/Shoko.Server/API/v3/Controllers/FilterController.cs b/Shoko.Server/API/v3/Controllers/FilterController.cs index 50e8bda73..27866c3c0 100644 --- a/Shoko.Server/API/v3/Controllers/FilterController.cs +++ b/Shoko.Server/API/v3/Controllers/FilterController.cs @@ -82,12 +82,22 @@ [FromQuery] [Range(1, int.MaxValue)] int page = 1, [FromQuery] bool withConditio [HttpPost] public ActionResult AddNewFilter(Filter.Input.CreateOrUpdateFilterBody body) { - var filterPreset = new FilterPreset { FilterType = GroupFilterType.UserDefined }; - var filter = _factory.MergeWithExisting(body, filterPreset, ModelState); - if (!ModelState.IsValid) - return ValidationProblem(ModelState); + try + { + var filterPreset = new FilterPreset + { + FilterType = GroupFilterType.UserDefined + }; + var filter = _factory.MergeWithExisting(body, filterPreset, ModelState); + if (!ModelState.IsValid) + return ValidationProblem(ModelState); - return filter; + return filter; + } + catch (ArgumentException e) + { + return ValidationProblem(e.Message, "Expression"); + } } /// @@ -222,16 +232,23 @@ public ActionResult PatchFilter([FromRoute] int filterID, JsonPatchDocum if (filterPreset == null) return NotFound(FilterNotFound); - var body = _factory.CreateOrUpdateFilterBody(filterPreset); - document.ApplyTo(body, ModelState); - if (!ModelState.IsValid) - return ValidationProblem(ModelState); - - var filter = _factory.MergeWithExisting(body, filterPreset, ModelState); - if (!ModelState.IsValid) - return ValidationProblem(ModelState); - - return filter; + try + { + var body = _factory.CreateOrUpdateFilterBody(filterPreset); + document.ApplyTo(body, ModelState); + if (!ModelState.IsValid) + return ValidationProblem(ModelState); + + var filter = _factory.MergeWithExisting(body, filterPreset, ModelState); + if (!ModelState.IsValid) + return ValidationProblem(ModelState); + + return filter; + } + catch (ArgumentException e) + { + return ValidationProblem(e.Message, "Expression"); + } } /// @@ -248,11 +265,18 @@ public ActionResult PutFilter([FromRoute] int filterID, Filter.Input.Cre if (filterPreset == null) return NotFound(FilterNotFound); - var filter = _factory.MergeWithExisting(body, filterPreset, ModelState); - if (!ModelState.IsValid) - return ValidationProblem(ModelState); + try + { + var filter = _factory.MergeWithExisting(body, filterPreset, ModelState); + if (!ModelState.IsValid) + return ValidationProblem(ModelState); - return filter; + return filter; + } + catch (ArgumentException e) + { + return ValidationProblem(e.Message, "Expression"); + } } ///