Skip to content

Raise Unit::ParseError instead of built-in SyntaxError#35

Merged
nertzy merged 1 commit into
minad:masterfrom
nertzy:parse-error
Jun 23, 2026
Merged

Raise Unit::ParseError instead of built-in SyntaxError#35
nertzy merged 1 commit into
minad:masterfrom
nertzy:parse-error

Conversation

@nertzy

@nertzy nertzy commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • 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, not a StandardError, so it slips through a plain rescue and is awkward to handle.
    • It conflates a malformed input string with a Ruby-level source parse failure.
  • Introduce Unit::ParseError < ArgumentError and raise it from the expression parser instead of SyntaxError. A malformed unit string is a bad argument, so ArgumentError is the correct lineage:
    • it's a StandardError (caught by a bare rescue), and
    • it's distinct from Unit::IncompatibleUnitError (a TypeError), keeping value errors and type errors separate.
  • Updated the parse_unit RDoc to document the new exception class.

Test plan

  • bundle exec rake (runs the suite twice — spec:no_dsl and spec:dsl) — green on both load paths
  • New spec/error_spec.rb examples assert Unit::ParseError is an ArgumentError/StandardError, is caught by a plain rescue, and is not a TypeError

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.
@nertzy nertzy merged commit 5d27823 into minad:master Jun 23, 2026
4 checks passed
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.

1 participant