Skip to main content

prefer-correct-error-name

added in: 1.7.0
⚙️🛠
Pro+

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

⚙️ Config

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

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

analysis_options.yaml
dart_code_metrics:
rules:
- prefer-correct-error-name:
allowed-names:
- error

Example

❌ Bad:

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

future.then((value) => print(value), onError: (Object? e) => print(e)) // LINT: Parameter name does not match any of the configured ones: error

future.catchError((e) => print(e)); // LINT: Parameter name does not match any of the configured ones: error

final stream = createStream();

stream.handleError((err) => print(err)); // LINT: Parameter name does not match any of the configured ones: error
stream.listen((event) {}, onError: (err) {}); // LINT: Parameter name does not match any of the configured ones: error

try {
await future;
// LINT: Parameter name does not match any of the configured ones: error
} 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)); // Correct, renamed to '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) {}
}

Example with "ignored-names"

Config
analysis_options.yaml
dart_code_metrics:
rules:
- prefer-correct-error-name:
ignored-names:
- e

✅ Good:

void function() async {
try {
await future;
} catch (e) {} // Correct, 'e' is ignored
}