Replace bare string throws with proper exception types#31
Merged
Harry-Chen merged 1 commit intonfcim:masterfrom Mar 13, 2026
Merged
Replace bare string throws with proper exception types#31Harry-Chen merged 1 commit intonfcim:masterfrom
Harry-Chen merged 1 commit intonfcim:masterfrom
Conversation
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
approved these changes
Mar 13, 2026
There was a problem hiding this comment.
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 runtimeifchecks throwingArgumentError/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)"); |
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
throw "error") is a Dart anti-pattern: they cannot be caught by type, breakischecks, and produce unhelpful stack tracesutilities.dartwith typed exceptions:ArgumentErrorfor invalid arguments (value overflow, bad byte length)RangeErrorfor out-of-range values (byte value range, stream bounds)FormatExceptionfor malformed input (hex strings, version strings)assert()calls (intToByteslength check,byteToHexStringrange check) with runtime checks so validation works in release buildsTest plan
on FormatException) now works correctly