Skip to main content

prefer-correct-error-name

added in: 1.7.0
⚙️🛠

Ensures a consistent name for the error parameter in catch, then, catchError, listen and handleError.

⚙️ Config

Set ignored-names (default is none) to ignore specific names.

Set allowed-names (default is [error, exception]) to configure the allowed name for the error parameter.

dart_code_metrics:
...
rules:
...
- prefer-correct-error-name:
allowed-names:
- err

Example

❌ Bad:

void function() async {
final future = Future.delayed(Duration(microseconds: 100), () => 6);

future.then((value) => print(value), onError: (Object? e) => print(e)) // LINT

future.catchError((e) => print(e)); // LINT

final stream = createStream();

stream.handleError((err) => print(err)); // LINT
stream.listen((event) {}, onError: (err) {}); // LINT

try {
await future;
// LINT
} catch (err) {}
}

✅ Good:

void function() async {
final future = Future.delayed(Duration(microseconds: 100), () => 6);

future.then((value) => print(value));
future.then((value) => print(value), onError: (error) => print(error));

future.catchError((error) => print(error));

final stream = createStream();

stream.handleError((error) => print(error));

stream.listen((event) {});
stream.listen((event) {}, onError: (error) {});

try {
await future;
} catch (error) {}
}