-
Notifications
You must be signed in to change notification settings - Fork 0
Reraise unexpected spectra crashes instead of masking them #43
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
4 commits
Select commit
Hold shift + click to select a range
97cb211
Reraise unexpected spectra crashes instead of masking them
claude 8c94954
Split handle_erlang_error into two clauses instead of rewriting matches
claude e9b9ccc
Add schema-path and unrecognized-ErlangError crash coverage
claude 679a45d
Bump CI matrix to Elixir 1.20 stable on OTP 29
claude 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
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,78 @@ | ||
| defmodule SpectralUnexpectedCrashTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| @moduledoc """ | ||
| `Spectral.encode/decode/schema` rescue `error in ErlangError` to translate a few | ||
| known spectra configuration errors (module/type not found, unsupported type) into | ||
| `ArgumentError`. That rescue clause also binds every other BEAM error that Elixir | ||
| normalizes into its own exception struct (e.g. `%KeyError{}`), not just literal | ||
| `%ErlangError{}` structs. `handle_erlang_error/4` used to only pattern-match on | ||
| `%ErlangError{}`, so those other exceptions crashed inside Spectral's own handler | ||
| with a misleading `FunctionClauseError`, discarding the original exception and | ||
| stacktrace. It must instead reraise the original exception with its original | ||
| stacktrace. | ||
| """ | ||
|
|
||
| test "encode reraises an unexpected crash unchanged, not as a FunctionClauseError" do | ||
| error = | ||
| assert_raise KeyError, fn -> | ||
| Spectral.encode("data", CrashingCodec, :t) | ||
| end | ||
|
|
||
| assert %KeyError{key: :missing_key} = error | ||
|
|
||
| stacktrace = | ||
| try do | ||
| Spectral.encode("data", CrashingCodec, :t) | ||
| rescue | ||
| _ -> __STACKTRACE__ | ||
| end | ||
|
|
||
| assert Enum.any?(stacktrace, fn {mod, _fun, _arity, _location} -> mod == CrashingCodec end) | ||
| end | ||
|
|
||
| test "decode reraises an unexpected crash unchanged, not as a FunctionClauseError" do | ||
| error = | ||
| assert_raise KeyError, fn -> | ||
| Spectral.decode("data", CrashingCodec, :t) | ||
| end | ||
|
|
||
| assert %KeyError{key: :missing_key} = error | ||
| end | ||
|
|
||
| test "schema/2 reraises an unexpected crash unchanged, not as a FunctionClauseError" do | ||
| error = | ||
| assert_raise KeyError, fn -> | ||
| Spectral.schema(CrashingCodec, :t) | ||
| end | ||
|
|
||
| assert %KeyError{key: :missing_key} = error | ||
| end | ||
|
|
||
| test "schema/4 reraises an unexpected crash unchanged, not as a FunctionClauseError" do | ||
| error = | ||
| assert_raise KeyError, fn -> | ||
| Spectral.schema(CrashingCodec, :t, :json_schema, []) | ||
| end | ||
|
|
||
| assert %KeyError{key: :missing_key} = error | ||
| end | ||
|
|
||
| test "an ErlangError with an unrecognized original is reraised unchanged, with its original stacktrace" do | ||
| error = | ||
| assert_raise ErlangError, fn -> | ||
| Spectral.encode("data", CrashingCodec, :t2) | ||
| end | ||
|
|
||
| assert %ErlangError{original: {:unexpected_codec_failure, :detail}} = error | ||
|
|
||
| stacktrace = | ||
| try do | ||
| Spectral.encode("data", CrashingCodec, :t2) | ||
| rescue | ||
| _ -> __STACKTRACE__ | ||
| end | ||
|
|
||
| assert Enum.any?(stacktrace, fn {mod, _fun, _arity, _location} -> mod == CrashingCodec 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,43 @@ | ||
| defmodule CrashingCodec do | ||
| @moduledoc """ | ||
| A codec whose callbacks crash instead of returning `:continue` or | ||
| `{:error, _}`. Used to verify that an unexpected crash from inside spectra | ||
| surfaces to the caller unchanged, rather than being turned into a | ||
| misleading error about Spectral itself. | ||
|
|
||
| `t` crashes with a normal Elixir exception (`%KeyError{}`) to exercise the | ||
| fallback for exceptions that are not `%ErlangError{}` at all. `t2` crashes | ||
| with a raw BEAM error Elixir has no specific exception for, producing a | ||
| genuine `%ErlangError{}` with an unrecognized `original`, to exercise the | ||
| other fallback (an `%ErlangError{}` that isn't one of the known | ||
| configuration errors). | ||
| """ | ||
|
|
||
| use Spectral.Codec | ||
|
|
||
| @type t :: String.t() | ||
| @type t2 :: String.t() | ||
|
|
||
| @impl Spectral.Codec | ||
| def encode(_format, _caller_type_info, {:type, :t, 0}, _target_type, _data, _config) do | ||
| raise KeyError, key: :missing_key, term: %{} | ||
| end | ||
|
|
||
| def encode(_format, _caller_type_info, {:type, :t2, 0}, _target_type, _data, _config) do | ||
| :erlang.error({:unexpected_codec_failure, :detail}) | ||
| end | ||
|
|
||
| def encode(_format, _caller_type_info, _type_ref, _target_type, _data, _config), do: :continue | ||
|
|
||
| @impl Spectral.Codec | ||
| def decode(_format, _caller_type_info, {:type, :t, 0}, _target_type, _input, _config) do | ||
| raise KeyError, key: :missing_key, term: %{} | ||
| end | ||
|
|
||
| def decode(_format, _caller_type_info, _type_ref, _target_type, _input, _config), do: :continue | ||
|
|
||
| @impl Spectral.Codec | ||
| def schema(_format, _caller_type_info, {:type, :t, 0}, _target_type, _config) do | ||
| raise KeyError, key: :missing_key, term: %{} | ||
| end | ||
| end |
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.
Uh oh!
There was an error while loading. Please reload this page.