-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[Breaking change request] Make TypeError
not extend AssertionError
#40317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
TypeError
not extend AssertionError
TypeError
not extend AssertionError
cc @Hixie @matanlurey @dgrove for review and approval. |
Seems fine |
@lrhn - when we unfork, developers will start seeing If so, shall we combine both your bullets here: dart-lang/language#787 (comment) into this breaking change? Otherwise, LGTM. |
There's logic in Flutter (at runtime in release builds) that specifically looks for AssertionError in some cases, but I doubt it'll be affected by this, so LGTM. |
Approved |
@vsmenon Would it be easier to combine the two changes ( My guess is that the error situations are independent of each other, so there will be no work saved by combining them, it will just be a bigger chunk of work. There might be a few cases where a test is changed from |
@lrhn @franklinyow - I'm fine either way. LGTM. |
TypeError no longer implements AssertionError after dart-lang/sdk#40317.
TypeError no longer implements AssertionError after dart-lang/sdk#40317.
Boolean conversion now throws a TypeError, not CastError. See: * #40317 * #40763 Presubmit: https://critique.corp.google.com/#review/301195917 Change-Id: I76e8aa4a849eb519e47c80f1b4873032a05ad636 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139402 Reviewed-by: Nicholas Shahan <[email protected]> Commit-Queue: Mark Zhou <[email protected]>
Change:
We will make the platform
TypeError
class not extend/implement theAssertionError
class.That will also make it not inherit the
message
getter.Reason for Change:
It is a legacy hold-over from Dart 1 that type errors (thrown by failing type annotations) are assertion errors (thrown by failed assertions in debug mode), because in Dart 1 type annotations were only checked in debug mode.
When we added the message operand to
assert
statements, we also added amessage
field toAssertionError
. ThenTypeError
inherited that field, even though it had no need for it.We plan to change which operations throw
TypeError
andCastError
(most likely by combining the two and only have one error type), and that would then cause cast errors to also be assertion errors. So, before moving forward with any other error changes, we want to detachTypeError
fromAssertionError
.An alternative would be to simply deprecate
TypeError
and just useCastError
for everything, butTypeError
has the better name for being a general "not the type expected" error because Dart doesn't/won't have implicit casts.Expected Impact:
You should generally not be catching errors, and if you do, it should be for generic handling like logging. Your code should not depend on which particular
Error
is thrown.That doesn't hold for test code, where you very often do want to check that the error that occurs is the one that you expect.
If such code conflates
TypeError
andAssertionError
by only expecting anAssertionError
for code which triggers an assert in debug (assertions enabled) mode, but a type error in production mode when a wrong value gets past the disabled assertion, then that test will start failing when aTypeError
occurs.The Dart SDK itself has tests like this.
However, the impact is expected to almost exclusively affect tests, not production code, which gives the affected developers time to fix the tests.
Mitigating the Impact:
If your code starts failing because you expect an
AssertionError
and get aTypeError
, then start expecting aTypeError
.Look at all places where you have
try { ... } on AssertionError { ... }
(remembering that catching a specificError
is discouraged in general, so there hopefully won't be many cases of this), andconsider whether it needs to handle
TypeError
instead or as well.Look at places where your tests do
expect(..., throwsAssertionError)
and consider whether it should catchTypeError
instead or as well.If you have code which uses the
message
field ofTypeError
, then that will start failing, and it should stop doing so now. The message was a fixed text in the VM implementation, so it wasn't particularly useful.The text was updated successfully, but these errors were encountered: