-
-
Notifications
You must be signed in to change notification settings - Fork 65
fix: handle binary error messages #395
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # SPDX-FileCopyrightText: 2019 ash_json_api contributors <https://github.com/ash-project/ash_json_api/graphs.contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshJsonApi.Error.ConflictingParams do | ||
| @moduledoc """ | ||
| Returned when path parameters and query parameters have conflicting names | ||
| """ | ||
|
|
||
| use Splode.Error, | ||
| class: :invalid, | ||
| fields: [:conflicting_keys, :detail] | ||
|
|
||
| def message(error) do | ||
| error.detail | ||
| end | ||
|
|
||
| def exception(opts) do | ||
| opts | ||
| |> Keyword.put_new(:detail, "conflict path and query params") | ||
| |> Keyword.drop([:conflicting_keys]) | ||
| |> super() | ||
| end | ||
|
|
||
| defimpl AshJsonApi.ToJsonApiError do | ||
| def to_json_api_error(error) do | ||
| %AshJsonApi.Error{ | ||
| id: Ash.UUID.generate(), | ||
| status_code: 400, | ||
| code: "invalid_query", | ||
| title: "InvalidQuery", | ||
| detail: error.detail, | ||
| meta: %{} | ||
| } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # SPDX-FileCopyrightText: 2019 ash_json_api contributors <https://github.com/ash-project/ash_json_api/graphs.contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshJsonApi.Error.InvalidFilter do | ||
| @moduledoc """ | ||
| Returned when a filter parameter is invalid or malformed | ||
| """ | ||
|
|
||
| use Splode.Error, | ||
| class: :invalid, | ||
| fields: [:filter, :detail, :source_parameter] | ||
|
|
||
| def message(error) do | ||
| error.detail | ||
| end | ||
|
|
||
| def exception(opts) do | ||
| filter = opts[:filter] | ||
|
|
||
| detail = | ||
| case filter do | ||
| nil -> "Invalid filter" | ||
| filter -> "Invalid filter: #{filter}" | ||
| end | ||
|
|
||
| opts | ||
| |> Keyword.put_new(:detail, detail) | ||
| |> Keyword.put_new(:source_parameter, "filter") | ||
| |> Keyword.drop([:filter]) | ||
| |> super() | ||
| end | ||
|
|
||
| defimpl AshJsonApi.ToJsonApiError do | ||
| def to_json_api_error(error) do | ||
| %AshJsonApi.Error{ | ||
| id: Ash.UUID.generate(), | ||
| status_code: 400, | ||
| code: "invalid_filter", | ||
| title: "InvalidFilter", | ||
| detail: error.detail, | ||
| source_parameter: error.source_parameter, | ||
| meta: %{} | ||
| } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # SPDX-FileCopyrightText: 2019 ash_json_api contributors <https://github.com/ash-project/ash_json_api/graphs.contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshJsonApi.Error.InvalidPathParam do | ||
| @moduledoc """ | ||
| Returned when a required path parameter is missing or invalid | ||
| """ | ||
|
|
||
| use Splode.Error, | ||
| class: :invalid, | ||
| fields: [:parameter, :url, :detail] | ||
|
|
||
| def message(error) do | ||
| error.detail | ||
| end | ||
|
|
||
| def exception(opts) do | ||
| parameter = opts[:parameter] | ||
| url = opts[:url] | ||
|
|
||
| detail = | ||
| case {parameter, url} do | ||
| {nil, nil} -> "Required path parameter not present" | ||
| {param, nil} -> "#{param} path parameter not present" | ||
| {nil, url} -> "Required path parameter not present in route: #{url}" | ||
| {param, url} -> "#{param} path parameter not present in route: #{url}" | ||
| end | ||
|
|
||
| opts | ||
| |> Keyword.put_new(:detail, detail) | ||
| |> Keyword.drop([:parameter, :url]) | ||
| |> super() | ||
| end | ||
|
|
||
| defimpl AshJsonApi.ToJsonApiError do | ||
| def to_json_api_error(error) do | ||
| %AshJsonApi.Error{ | ||
| id: Ash.UUID.generate(), | ||
| status_code: 400, | ||
| code: "invalid_path_param", | ||
| title: "InvalidPathParam", | ||
| detail: error.detail, | ||
| meta: %{} | ||
| } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # SPDX-FileCopyrightText: 2019 ash_json_api contributors <https://github.com/ash-project/ash_json_api/graphs.contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshJsonApi.Error.InvalidSort do | ||
| @moduledoc """ | ||
| Returned when a sort parameter is invalid or malformed | ||
| """ | ||
|
|
||
| use Splode.Error, | ||
| class: :invalid, | ||
| fields: [:sort, :field, :detail, :source_parameter] | ||
|
|
||
| def message(error) do | ||
| error.detail | ||
| end | ||
|
|
||
| def exception(opts) do | ||
| sort = opts[:sort] | ||
| field = opts[:field] | ||
|
|
||
| detail = | ||
| cond do | ||
| field -> "Invalid sort field: #{field}" | ||
| sort -> "Invalid sort: #{sort}" | ||
| true -> "Invalid sort parameter" | ||
| end | ||
|
|
||
| opts | ||
| |> Keyword.put_new(:detail, detail) | ||
| |> Keyword.put_new(:source_parameter, "sort") | ||
| |> Keyword.drop([:sort, :field]) | ||
| |> super() | ||
| end | ||
|
|
||
| defimpl AshJsonApi.ToJsonApiError do | ||
| def to_json_api_error(error) do | ||
| %AshJsonApi.Error{ | ||
| id: Ash.UUID.generate(), | ||
| status_code: 400, | ||
| code: "invalid_sort", | ||
| title: "InvalidSort", | ||
| detail: error.detail, | ||
| source_parameter: error.source_parameter, | ||
| meta: %{} | ||
| } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # SPDX-FileCopyrightText: 2019 ash_json_api contributors <https://github.com/ash-project/ash_json_api/graphs.contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshJsonApi.Error.MissingSchema do | ||
| @moduledoc """ | ||
| Returned when a required schema is not found for validation | ||
| """ | ||
|
|
||
| use Splode.Error, | ||
| class: :invalid, | ||
| fields: [:detail] | ||
|
|
||
| def message(error) do | ||
| error.detail | ||
| end | ||
|
|
||
| def exception(opts) do | ||
| opts | ||
| |> Keyword.put_new(:detail, "No schema found for validation") | ||
| |> super() | ||
| end | ||
|
|
||
| defimpl AshJsonApi.ToJsonApiError do | ||
| def to_json_api_error(error) do | ||
| %AshJsonApi.Error{ | ||
| id: Ash.UUID.generate(), | ||
| status_code: 400, | ||
| code: "missing_schema", | ||
| title: "MissingSchema", | ||
| detail: error.detail, | ||
| meta: %{} | ||
| } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # SPDX-FileCopyrightText: 2019 ash_json_api contributors <https://github.com/ash-project/ash_json_api/graphs.contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshJsonApi.Error.UnknownError do | ||
| @moduledoc """ | ||
| Returned when an unexpected error occurs that doesn't fit other categories | ||
| """ | ||
|
|
||
| use Splode.Error, | ||
| class: :unknown, | ||
| fields: [:detail] | ||
|
|
||
| def message(error) do | ||
| error.detail | ||
| end | ||
|
|
||
| def exception(opts) do | ||
| message = opts[:message] || "An unknown error occurred" | ||
|
|
||
| opts | ||
| |> Keyword.put_new(:detail, message) | ||
| |> Keyword.drop([:message]) | ||
| |> super() | ||
| end | ||
|
|
||
| defimpl AshJsonApi.ToJsonApiError do | ||
| def to_json_api_error(error) do | ||
| %AshJsonApi.Error{ | ||
| id: Ash.UUID.generate(), | ||
| status_code: 500, | ||
| code: "unknown_error", | ||
| title: "UnknownError", | ||
| detail: error.detail, | ||
| meta: %{} | ||
| } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally speaking we should expect to be handling specific errors here that can implement a protocol. What are you doing that is ending up passing a string here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh if its something internal, then we should have those return
InvalidQueryerrors instead of strings. Could you make that change instead? 🙇There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, added a bunch of new errors and tests for those errors, plus a fallback for if anything ever hits the binary handler again (just in case)