Skip to content

Commit 1051c58

Browse files
authored
improvement: handle Reactor.Error.Invalid.RunStepError in error conversion (#424)
1 parent ecfcc4e commit 1051c58

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

lib/ash_json_api/error/error.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ defmodule AshJsonApi.Error do
3131
Enum.flat_map(errors, &to_json_api_errors(domain, resource, &1, type))
3232
end
3333

34+
def to_json_api_errors(
35+
domain,
36+
resource,
37+
%Reactor.Error.Invalid.RunStepError{error: inner_error},
38+
type
39+
) do
40+
inner_error
41+
|> Ash.Error.to_error_class()
42+
|> then(&to_json_api_errors(domain, resource, &1, type))
43+
end
44+
3445
def to_json_api_errors(domain, resource, %__MODULE__{} = error, _type) do
3546
apply_error_handler([error], domain, resource)
3647
end

test/acceptance/error_validation_test.exs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,58 @@ defmodule Test.Acceptance.ErrorValidationTest do
191191
assert is_binary(json_error.id)
192192
end
193193

194+
test "RunStepError delegates to inner error with ToJsonApiError implementation" do
195+
inner_error = Ash.Error.Changes.InvalidChanges.exception(message: "bad input")
196+
197+
run_step_error =
198+
Reactor.Error.Invalid.RunStepError.exception(
199+
error: inner_error,
200+
step: %Reactor.Step{name: :test_step}
201+
)
202+
203+
result = AshJsonApi.Error.to_json_api_errors(nil, nil, run_step_error, :create)
204+
205+
assert [json_error] = result
206+
assert json_error.code == "invalid"
207+
assert json_error.title == "Invalid"
208+
assert json_error.detail == "bad input"
209+
end
210+
211+
test "RunStepError delegates to inner Ash wrapper error" do
212+
inner_error =
213+
Ash.Error.Changes.InvalidChanges.exception(message: "wrapped error")
214+
|> Ash.Error.to_error_class()
215+
216+
run_step_error =
217+
Reactor.Error.Invalid.RunStepError.exception(
218+
error: inner_error,
219+
step: %Reactor.Step{name: :test_step}
220+
)
221+
222+
result = AshJsonApi.Error.to_json_api_errors(nil, nil, run_step_error, :create)
223+
224+
assert [json_error] = result
225+
assert json_error.code == "invalid"
226+
assert json_error.detail == "wrapped error"
227+
end
228+
229+
@tag capture_log: true
230+
test "RunStepError falls back to generic error for unknown inner errors" do
231+
inner_error = RuntimeError.exception("something unexpected")
232+
233+
run_step_error =
234+
Reactor.Error.Invalid.RunStepError.exception(
235+
error: inner_error,
236+
step: %Reactor.Step{name: :test_step}
237+
)
238+
239+
result = AshJsonApi.Error.to_json_api_errors(Domain, TestPost, run_step_error, :create)
240+
241+
assert [json_error] = result
242+
assert json_error.status_code == 500
243+
assert json_error.code == "something_went_wrong"
244+
end
245+
194246
test "Binary error fallback uses UnknownError" do
195247
# Test the binary error handler directly
196248
domain = nil

0 commit comments

Comments
 (0)