Skip to content

complete_error_without_stack_trace

Reports completeError(error) in a catch clause without the clause’s stack trace. Pass the stack trace as well.

Tier
advisory
Scope
Dart
Lowest preset
recommended
Fix
passes the catch clause’s stack trace

A call of a Completer’s completeError with the error alone, inside a catch clause: completer.completeError(error). The future then fails with a stack trace of the call itself, though the clause has the trace of where the error came from.

It does not report a call outside a catch clause, since no trace of the error is at hand there.

A call with another error than the caught one is reported as well: the clause’s trace is still where the failure came from.

Whoever awaits the future sees a trace that ends in the code that completed it, which says nothing of the failure. The trace was one name away.

Violating
try {
done.complete(await work());
} catch (error) {
done.completeError(error);
}
Corrected
try {
done.complete(await work());
} catch (error, stackTrace) {
done.completeError(error, stackTrace);
}

The fix passes the clause’s stack trace as the second argument. Where the clause does not name the trace, it names it stackTrace: catch (error) becomes catch (error, stackTrace). It is not offered where the clause names the trace _, where it catches nothing by name (on Error { }), or where the clause already uses the name stackTrace.

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.

Write why on the line directly above the suppression:

// The trace of this failure is of no use to the caller.
// ignore: assay/complete_error_without_stack_trace

// ignore_for_file: assay/complete_error_without_stack_trace silences it in a whole file, and // ignore: complete_error_without_stack_trace without the prefix works too. See Suppressions.