thrown_object_without_to_string
Reports a thrown object whose class has no toString of its own, so every report of it reads “Instance of ‘PaymentFailed’”. Give the class a toString.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- none
What it reports
Section titled “What it reports”An object created in a throw whose class has no toString of its own: throw PaymentFailed(code). The violation covers the creation.
Whatever reports the error shows it by its toString: the zone’s handler of uncaught errors, a log, a failing test. Implementing Exception or extending Error gives no text either.
Only an object created in the throw itself is reported, since its class is then known. The class must be neither abstract nor sealed, the constructor not a factory, and neither the class nor any class or mixin above it may declare a toString. The SDK’s own exceptions and errors have one.
The code and the message the exception carries are lost in every report of it, the one place they were meant for.
Example
Section titled “Example”class PaymentFailed implements Exception { PaymentFailed(this.code);
final int code;}
throw PaymentFailed(code);class PaymentFailed implements Exception { PaymentFailed(this.code);
final int code;
@override String toString() => 'PaymentFailed: code $code';}
throw PaymentFailed(code);Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// Only the type of this exception is ever tested.// ignore: assay/thrown_object_without_to_stringthrow Stop();// ignore_for_file: assay/thrown_object_without_to_string silences it in a whole file, and // ignore: thrown_object_without_to_string without the prefix works too. See Suppressions.