-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
exception.ex
53 lines (45 loc) · 1.43 KB
/
exception.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
defmodule Ash.Error.Exception do
@moduledoc "Tooling for creating an Ash exception"
defmacro __using__(_) do
quote do
import Ash.Error.Exception, only: [def_ash_error: 1, def_ash_error: 2]
end
end
defmacro def_ash_error(fields, opts \\ []) do
quote do
defexception unquote(fields) ++
[
:changeset,
:query,
vars: [],
path: [],
stacktrace: [],
class: unquote(opts)[:class]
]
@impl Exception
def message(%{message: message, vars: vars} = exception) do
string = message || ""
string =
Enum.reduce(vars, string, fn {key, value}, acc ->
if String.contains?(acc, "%{#{key}}") do
String.replace(acc, "%{#{key}}", to_string(value))
else
acc
end
end)
Ash.ErrorKind.message(%{exception | message: string})
end
def message(exception), do: Ash.ErrorKind.message(exception)
def exception(opts) do
case Process.info(self(), :current_stacktrace) do
{:current_stacktrace, [_, _ | stacktrace]} ->
super(
Keyword.put_new(opts, :stacktrace, %Ash.Error.Stacktrace{stacktrace: stacktrace})
)
_ ->
super(opts)
end
end
end
end
end