dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.31k stars 1.59k forks source link

proposal: `error_completer_stack_trace` #59374

Open Hixie opened 10 months ago

Hixie commented 10 months ago

error_completer_stack_trace

I have no idea what the right name for this is so please don't use this one.

Description

Include a stack trace (possibly StackTrace.current) when completing a Completer with an error.

Details

When you call Completer.completeError and don't give a stack trace, Dart defaults to a stack trace that only includes internals of the Dart standard library. This makes debugging such errors very difficult. Callers should instead pass StackTrace.current so that completeError acts more like throw, providing a stack trace that corresponds to where the error occurred.

Kind

This makes debugging easier.

Bad Examples

// BAD
completer.completeError('foo');

Good Examples

// GOOD
// For an error being reported from here:
completer.completeError('foo', StackTrace.current);

// For an error being forwarded from elsewhere:
completer.completeError(result.exception, result.stackTrace);

Discussion

This is motivated by difficulties debugging code that used futures extensively but without knowing that stack traces should be included.

See https://github.com/dart-lang/sdk/issues/31412.

lrhn commented 10 months ago

False positives can occur when completing with an Error that's known to have a stackTrace set, intending to use that as the stack trace (which it will be). It shouldn't be hard to pass error.stackTrace explicitly instead.

(The default is actually not a stack trace containing only platform internals. The default is the empty stack trace.)