Enable error_context in assertions#15309
Open
dtwiers wants to merge 1 commit intoelixir-lang:mainfrom
Open
Conversation
dtwiers
commented
Apr 24, 2026
|
|
||
| """ | ||
| defmacro assert({:=, meta, [left, right]} = assertion) do | ||
| code = escape_quoted(:assert, meta, mark_as_generated(assertion)) |
Author
There was a problem hiding this comment.
Since assert/1 was a macro but assert/2 was a function, I needed to move things around a bit to enable diffing within assert/2, hence the decent size of this PR
Assisted-by: opencode:OpenAI/GPT-5.4
e8684d2 to
57c9f6c
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This enables 2 features within assertions:
assert foo == bar, diff: true, message: "this must be a string, and is intended to be the main error message"will give the error message as-is, but will also enable the same beautiful diffing we've enjoyed inassert/1.assert foo == bar, error_context: %{this: "could be any shape"}, which impliesdiff: true, also enables the diffing output, but adds an additionalcontext: <whatever you put here>, which I have found to be extremely handy if you want to assert something in a loop (you put the iterator value in theerror_contextand you know which item failed). You can still usemessage: "my main error message"with it too, if you want more detailsThoughts anyone? I asked about this as a proposal all the way back in September and finally got around to implementing it. It's not only my first PR into Elixir, it's my first PR into any major FOSS project.