Skip to content

fix: check for impl for all classes of errors. #365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025
Merged
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
71 changes: 26 additions & 45 deletions lib/ash_json_api/error/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ defmodule AshJsonApi.Error do
[error]
end

def to_json_api_errors(domain, resource, %{class: :invalid} = error, type) do
def to_json_api_errors(_domain, _resource, %{class: :forbidden} = error, _type) do
[
%__MODULE__{
id: Ash.UUID.generate(),
status_code: class_to_status(error.class),
code: "forbidden",
title: "Forbidden",
detail: "forbidden"
}
]
end

def to_json_api_errors(domain, resource, error, type) do
if AshJsonApi.ToJsonApiError.impl_for(error) do
error
|> AshJsonApi.ToJsonApiError.to_json_api_error()
Expand All @@ -53,13 +65,21 @@ defmodule AshJsonApi.Error do
"`#{uuid}`: AshJsonApi.Error not implemented for error:\n\n#{Exception.format(:error, error, stacktrace)}"
)

code = if error.class == :forbidden, do: "forbidden", else: "something_went_wrong"
title = if error.class == :forbidden, do: "Forbidden", else: "SomethingWentWrong"

detail =
if error.class == :forbidden,
do: "forbidden",
else: "Something went wrong. Error id: #{uuid}"

if AshJsonApi.Domain.Info.show_raised_errors?(domain) do
[
%__MODULE__{
id: uuid,
status_code: class_to_status(error.class),
code: "something_went_wrong",
title: "SomethingWentWrong",
code: code,
title: title,
detail: """
Raised error: #{uuid}

Expand All @@ -72,54 +92,15 @@ defmodule AshJsonApi.Error do
%__MODULE__{
id: uuid,
status_code: class_to_status(error.class),
code: "something_went_wrong",
title: "SomethingWentWrong",
detail: "Something went wrong. Error id: #{uuid}"
code: code,
title: title,
detail: detail
}
]
end
end
end

def to_json_api_errors(_domain, _resource, %{class: :forbidden} = error, _type) do
[
%__MODULE__{
id: Ash.UUID.generate(),
status_code: class_to_status(error.class),
code: "forbidden",
title: "Forbidden",
detail: "forbidden"
}
]
end

def to_json_api_errors(_domain, _resource, error, _type) do
uuid = Ash.UUID.generate()

stacktrace =
case error do
%{stacktrace: %{stacktrace: v}} ->
v

_ ->
nil
end

Logger.warning(
"`#{uuid}`: AshJsonApi.Error not implemented for error:\n\n#{Exception.format(:error, error, stacktrace)}"
)

[
%__MODULE__{
id: uuid,
status_code: class_to_status(error.class),
code: "something_went_wrong",
title: "SomethingWentWrong",
detail: "Something went wrong. Error id: #{uuid}"
}
]
end

@doc "Turns an error class into an HTTP status code"
def class_to_status(:forbidden), do: 403
def class_to_status(:invalid), do: 400
Expand Down