Skip to content

Document why UnexpectedNilError inherits from Exception#21

Merged
amomchilov merged 1 commit intomainfrom
Alex/document-exception-inheritance
Mar 4, 2026
Merged

Document why UnexpectedNilError inherits from Exception#21
amomchilov merged 1 commit intomainfrom
Alex/document-exception-inheritance

Conversation

@Morriar
Copy link
Contributor

@Morriar Morriar commented Mar 4, 2026

Prompted by a real-world case where a copy of UnexpectedNilError in another codebase changed the superclass to StandardError, which caused nil errors to be silently swallowed by common rescue StandardError clauses in tests — masking bugs that should have been immediately visible.

The existing comment explained the rationale but didn't warn against this specific footgun. This PR:

  • Fixes a small typo ("rescues clauses" → "rescue clauses")
  • Clarifies that rescue Exception can still catch it (intentionally harder to do accidentally)
  • Adds an explicit warning against changing the superclass when copying this class into another codebase

Related: #20

Comment on lines +34 to +36
# WARNING: do NOT change the superclass to `StandardError`.
# Doing so would allow common `rescue StandardError` or bare `rescue` clauses to silently swallow
# nil errors, masking bugs that should be immediately visible.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is particularly relevant to the gem.

A gem user can't change it unless they make their own copy, which isn't our problem

Suggested change
# WARNING: do NOT change the superclass to `StandardError`.
# Doing so would allow common `rescue StandardError` or bare `rescue` clauses to silently swallow
# nil errors, masking bugs that should be immediately visible.

@Morriar Morriar force-pushed the Alex/document-exception-inheritance branch from 0f89ee8 to a3ffa4b Compare March 4, 2026 19:18
@Morriar Morriar requested a review from amomchilov March 4, 2026 19:18
@amomchilov amomchilov merged commit da05823 into main Mar 4, 2026
1 check passed
@amomchilov amomchilov deleted the Alex/document-exception-inheritance branch March 4, 2026 21:11
@amomchilov amomchilov changed the title Document why UnexpectedNilError inherits from Exception and warn against changing it Document why UnexpectedNilError inherits from Exception Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants