Skip to content

Replace bare string throws with proper exception types#31

Merged
Harry-Chen merged 1 commit intonfcim:masterfrom
klaasdannen:fix/proper-exception-types
Mar 13, 2026
Merged

Replace bare string throws with proper exception types#31
Harry-Chen merged 1 commit intonfcim:masterfrom
klaasdannen:fix/proper-exception-types

Conversation

@klaasdannen
Copy link
Contributor

Summary

  • Throwing raw strings (throw "error") is a Dart anti-pattern: they cannot be caught by type, break is checks, and produce unhelpful stack traces
  • Replaced all bare string throws in utilities.dart with typed exceptions:
    • ArgumentError for invalid arguments (value overflow, bad byte length)
    • RangeError for out-of-range values (byte value range, stream bounds)
    • FormatException for malformed input (hex strings, version strings)
  • Also replaced assert() calls (intToBytes length check, byteToHexString range check) with runtime checks so validation works in release builds

Test plan

  • Existing tests pass (exception messages changed but types are now more specific)
  • Code that catches these errors by type (e.g., on FormatException) now works correctly

Throwing raw strings (e.g. throw "error message") is a Dart anti-pattern:
they cannot be caught by type, break is-checks, and produce unhelpful
stack traces. Replaced with appropriate typed exceptions:
- ArgumentError for invalid arguments (overflow, bad length)
- RangeError for out-of-range values (byte range, stream bounds)
- FormatException for malformed input (hex strings, version strings)

Also replaced remaining assert() calls with runtime checks so
validation works in release builds.
@Harry-Chen Harry-Chen requested a review from Copilot March 13, 2026 15:13
@Harry-Chen Harry-Chen merged commit dd281b8 into nfcim:master Mar 13, 2026
2 of 4 checks passed
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Replaces bare throw "string" and assert() calls with typed Dart exceptions (ArgumentError, RangeError, FormatException) so errors can be caught by type and work in release builds.

Changes:

  • Replaced assert() calls with runtime if checks throwing ArgumentError/RangeError
  • Replaced bare string throws with appropriate typed exceptions throughout utilities.dart
  • Improved error messages to be more concise and informative

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

int readInt(int number, {Endianness endianness = Endianness.Big}) {
if (number > 8) {
throw "Number of bytes converted to a int must be in [0,8), got $number";
throw ArgumentError.value(number, "number", "Must be in [0,8)");
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.

3 participants