fix: Make get and get! consistent with what they raise when no record found (#325)

This commit is contained in:
Darren Black 2022-05-19 11:37:04 +10:00 committed by GitHub
parent fd68d212ef
commit 35a19f800d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View file

@ -1075,7 +1075,7 @@ defmodule Ash.Api do
end
defp unwrap_or_raise!({:error, error}, stacktraces?, _) do
exception = Ash.Error.to_ash_error(error)
exception = Ash.Error.to_error_class(error)
exception =
if stacktraces? do

View file

@ -158,6 +158,15 @@ defmodule Ash.Test.Actions.ReadTest do
assert ^post = clear_meta(Api.get!(Post, post.id))
end
test "it raises when there is no matching record" do
res =
assert_raise Ash.Error.Invalid, fn ->
Api.get!(Post, Ash.UUID.generate())
end
assert [%Ash.Error.Query.NotFound{}] = res.errors
end
test "raises an error when the first argument is not a module", %{post: post} do
res = assert_raise Ash.Error.Invalid.NoSuchResource, fn -> Api.get("bogus", post.id, []) end
assert res.message =~ ~r/Ash.Test.Actions.ReadTest.Api.get\/3/