mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
improvement: Add error message when page is added but pagination is not enabled (#511)
This commit is contained in:
parent
0de50e132b
commit
0e6763d8cc
3 changed files with 50 additions and 2 deletions
|
@ -46,7 +46,8 @@ defmodule Ash.Api do
|
|||
alias Ash.Error.Invalid.{
|
||||
NoPrimaryAction,
|
||||
NoSuchAction,
|
||||
NoSuchResource
|
||||
NoSuchResource,
|
||||
PageRequiresPagination
|
||||
}
|
||||
|
||||
alias Ash.Error.Query.NotFound
|
||||
|
@ -1003,7 +1004,8 @@ defmodule Ash.Api do
|
|||
query = Ash.Query.set_api(query, api)
|
||||
|
||||
with {:ok, opts} <- Spark.OptionsHelpers.validate(opts, @read_opts_schema),
|
||||
{:ok, action} <- get_action(query.resource, opts, :read, query.action) do
|
||||
{:ok, action} <- get_action(query.resource, opts, :read, query.action),
|
||||
{:ok, action} <- pagination_check(action, query.resource, opts) do
|
||||
Read.run(query, action, opts)
|
||||
else
|
||||
{:error, error} ->
|
||||
|
@ -1194,6 +1196,19 @@ defmodule Ash.Api do
|
|||
end
|
||||
end
|
||||
|
||||
defp pagination_check(action, resource, opts) do
|
||||
case Keyword.has_key?(opts, :page) && Map.get(action, :pagination) do
|
||||
true ->
|
||||
{:ok, action}
|
||||
|
||||
false ->
|
||||
{:error,
|
||||
Ash.Error.to_error_class(
|
||||
PageRequiresPagination.exception(resource: resource, action: action)
|
||||
)}
|
||||
end
|
||||
end
|
||||
|
||||
defp unwrap_or_raise!(first, second, destroy? \\ false)
|
||||
defp unwrap_or_raise!(:ok, _, _), do: :ok
|
||||
defp unwrap_or_raise!({:ok, result}, _, false), do: result
|
||||
|
|
24
lib/ash/error/invalid/page_requires_pagination.ex
Normal file
24
lib/ash/error/invalid/page_requires_pagination.ex
Normal file
|
@ -0,0 +1,24 @@
|
|||
defmodule Ash.Error.Invalid.PageRequiresPagination do
|
||||
@moduledoc "Used when page option is passed but pagination is not enabled."
|
||||
use Ash.Error.Exception
|
||||
|
||||
def_ash_error([:resource, :action], class: :invalid)
|
||||
|
||||
defimpl Ash.ErrorKind do
|
||||
def id(_), do: Ash.UUID.generate()
|
||||
|
||||
def code(_), do: "page_requires_pagination"
|
||||
|
||||
def message(%{resource: resource, action: action}) do
|
||||
"""
|
||||
Pagination is not enabled on resource #{inspect(resource)} for the action #{inspect(action)}. Check that you've
|
||||
enabled pagination in your action. For example:
|
||||
|
||||
|
||||
read #{action.name} do
|
||||
pagination offset?: true, keyset?: true, required?: false
|
||||
end
|
||||
"""
|
||||
end
|
||||
end
|
||||
end
|
|
@ -255,6 +255,15 @@ defmodule Ash.Test.Actions.ReadTest do
|
|||
assert res.message =~ ~r/Ash.Test.AnyApi.read!\/2/
|
||||
assert res.message =~ ~r/expected a keyword list, but instead got \[1\]/
|
||||
end
|
||||
|
||||
test "raises an error when page is sent but pagination is not enabled on a resource" do
|
||||
res =
|
||||
assert_raise Ash.Error.Invalid, fn ->
|
||||
Api.read!(Post)
|
||||
end
|
||||
|
||||
assert %Ash.Error.Invalid.PageRequiresPagination{resource: Post, action: _} = hd(res.errors)
|
||||
end
|
||||
end
|
||||
|
||||
describe "Api.read/2" do
|
||||
|
|
Loading…
Reference in a new issue