Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/ash_json_api/error/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ defmodule AshJsonApi.Error do
Enum.flat_map(errors, &to_json_api_errors(domain, resource, &1, type))
end

def to_json_api_errors(
domain,
resource,
%Reactor.Error.Invalid.RunStepError{error: inner_error},
type
) do
inner_error
|> Ash.Error.to_error_class()
|> then(&to_json_api_errors(domain, resource, &1, type))
end

def to_json_api_errors(domain, resource, %__MODULE__{} = error, _type) do
apply_error_handler([error], domain, resource)
end
Expand Down
52 changes: 52 additions & 0 deletions test/acceptance/error_validation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,58 @@ defmodule Test.Acceptance.ErrorValidationTest do
assert is_binary(json_error.id)
end

test "RunStepError delegates to inner error with ToJsonApiError implementation" do
inner_error = Ash.Error.Changes.InvalidChanges.exception(message: "bad input")

run_step_error =
Reactor.Error.Invalid.RunStepError.exception(
error: inner_error,
step: %Reactor.Step{name: :test_step}
)

result = AshJsonApi.Error.to_json_api_errors(nil, nil, run_step_error, :create)

assert [json_error] = result
assert json_error.code == "invalid"
assert json_error.title == "Invalid"
assert json_error.detail == "bad input"
end

test "RunStepError delegates to inner Ash wrapper error" do
inner_error =
Ash.Error.Changes.InvalidChanges.exception(message: "wrapped error")
|> Ash.Error.to_error_class()

run_step_error =
Reactor.Error.Invalid.RunStepError.exception(
error: inner_error,
step: %Reactor.Step{name: :test_step}
)

result = AshJsonApi.Error.to_json_api_errors(nil, nil, run_step_error, :create)

assert [json_error] = result
assert json_error.code == "invalid"
assert json_error.detail == "wrapped error"
end

@tag capture_log: true
test "RunStepError falls back to generic error for unknown inner errors" do
inner_error = RuntimeError.exception("something unexpected")

run_step_error =
Reactor.Error.Invalid.RunStepError.exception(
error: inner_error,
step: %Reactor.Step{name: :test_step}
)

result = AshJsonApi.Error.to_json_api_errors(Domain, TestPost, run_step_error, :create)

assert [json_error] = result
assert json_error.status_code == 500
assert json_error.code == "something_went_wrong"
end

test "Binary error fallback uses UnknownError" do
# Test the binary error handler directly
domain = nil
Expand Down
Loading