improvement: add not_found_error? option to interface builder and when calling

This commit is contained in:
Zach Daniel 2022-11-29 17:18:57 -05:00
parent 113b8740d4
commit 82d3ec3946
3 changed files with 25 additions and 3 deletions

View file

@ -121,6 +121,7 @@ spark_locals_without_parens = [
module: 1,
name: 1,
no_attributes?: 1,
not_found_error?: 1,
not_found_message: 1,
offset?: 1,
on: 1,

View file

@ -213,7 +213,12 @@ defmodule Ash.CodeInterface do
)
|> case do
{:ok, nil} ->
{:error, Ash.Error.Query.NotFound.exception(resource: query.resource)}
if unquote(interface.not_found_error?) == false ||
Keyword.get(opts, :not_found_error?) == false do
{:ok, nil}
else
{:error, Ash.Error.Query.NotFound.exception(resource: query.resource)}
end
{:ok, result} ->
{:ok, result}
@ -286,7 +291,12 @@ defmodule Ash.CodeInterface do
)
|> case do
nil ->
raise Ash.Error.Query.NotFound, resource: query.resource
if unquote(interface.not_found_error?) == false ||
Keyword.get(opts, :not_found_error?) == false do
nil
else
raise Ash.Error.Query.NotFound, resource: query.resource
end
result ->
result

View file

@ -2,7 +2,7 @@ defmodule Ash.Resource.Interface do
@moduledoc """
Represents a function in a resource's code interface
"""
defstruct [:name, :action, :args, :get?, :get_by, :get_by_identity]
defstruct [:name, :action, :args, :get?, :get_by, :get_by_identity, :not_found_error?]
@type t :: %__MODULE__{}
@ -80,6 +80,11 @@ defmodule Ash.Resource.Interface do
load: [
type: :any,
doc: "Adds a load statement to the query before passing it to the action."
],
not_found_error?: [
type: :boolean,
doc:
"Whether or not to return or raise a `NotFound` error or to return `nil` when a get? action/interface is called."
]
]
end
@ -106,6 +111,12 @@ defmodule Ash.Resource.Interface do
""",
links: []
],
not_found_error?: [
type: :boolean,
default: true,
doc:
"If the action or interface is configured with `get?: true`, this determines whether or not an error is raised or `nil` is returned."
],
get?: [
type: :boolean,
doc: """