Raise Unit::ParseError instead of built-in SyntaxError#35
Merged
Conversation
Malformed unit expressions (unbalanced parentheses, dangling operators) previously raised Ruby's built-in SyntaxError. That is the wrong class for a bad argument value: SyntaxError is a ScriptError, so it is not a StandardError and slips through a plain `rescue`, and it conflates a malformed input string with a Ruby-level parse failure. Introduce Unit::ParseError < ArgumentError and raise it from the expression parser instead. A malformed unit string is a bad argument, so ArgumentError is the correct lineage: it is a StandardError (caught by a bare rescue) and is distinct from Unit::IncompatibleUnitError (a TypeError), keeping value errors and type errors separate.
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.
Summary
SyntaxError. That is the wrong class for a bad argument value:SyntaxErroris aScriptError, not aStandardError, so it slips through a plainrescueand is awkward to handle.Unit::ParseError < ArgumentErrorand raise it from the expression parser instead ofSyntaxError. A malformed unit string is a bad argument, soArgumentErroris the correct lineage:StandardError(caught by a barerescue), andUnit::IncompatibleUnitError(aTypeError), keeping value errors and type errors separate.parse_unitRDoc to document the new exception class.Test plan
bundle exec rake(runs the suite twice —spec:no_dslandspec:dsl) — green on both load pathsspec/error_spec.rbexamples assertUnit::ParseErroris anArgumentError/StandardError, is caught by a plainrescue, and is not aTypeError