fix: support passing a forbidden error to policies per the docs

fixes #1443
This commit is contained in:
Zach Daniel 2024-09-07 07:50:47 -04:00
parent 0dde9f0fc1
commit 4f31109bd9
2 changed files with 19 additions and 0 deletions

View file

@ -70,6 +70,12 @@ defmodule Ash.Error.Forbidden.Policy do
- `:help_text?`: Defaults to true. Displays help text at the top of the policy breakdown.
"""
def report(%Ash.Error.Forbidden{errors: errors}) do
errors
|> Enum.filter(&(&1.__struct__ == __MODULE__))
|> Enum.map_join("\n\n", &report/1)
end
def report(error, opts \\ []) do
error
|> get_errors()

View file

@ -190,6 +190,19 @@ defmodule Ash.Test.Policy.SimpleTest do
end
end
test "breakdowns can be shown for all policy errors in a forbidden error" do
res =
assert_raise Ash.Error.Forbidden,
~r/No policy conditions applied to this request/,
fn ->
ResourceWithAPolicyThatDoesntApply
|> Ash.read!()
end
assert Ash.Error.Forbidden.Policy.report(res) =~
~r/No policy conditions applied to this request/
end
test "an impossible create policy shows the correct error message" do
assert_raise Ash.Error.Forbidden, ~r/Cannot use a filter to authorize a create/, fn ->
ResourceWithAnImpossibleCreatePolicy