Skip to content

Commit

Permalink
Better messaging in APIv3 for Expression Validation Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Oct 6, 2023
1 parent 265cdda commit 36d0e61
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions Shoko.Server/API/v3/Controllers/FilterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,22 @@ [FromQuery] [Range(1, int.MaxValue)] int page = 1, [FromQuery] bool withConditio
[HttpPost]
public ActionResult<Filter> 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");
}
}

/// <summary>
Expand Down Expand Up @@ -222,16 +232,23 @@ public ActionResult<Filter> 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");
}
}

/// <summary>
Expand All @@ -248,11 +265,18 @@ public ActionResult<Filter> 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");
}
}

/// <summary>
Expand Down

0 comments on commit 36d0e61

Please sign in to comment.