Requiring data preserves the stackTrace (#93592)

This commit is contained in:
Remi Rousselet 2021-12-08 00:24:04 +01:00 committed by GitHub
parent af9cf734e8
commit e05ddcbd9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -249,7 +249,7 @@ class AsyncSnapshot<T> {
if (hasData)
return data!;
if (hasError)
throw error!; // ignore: only_throw_errors, since we're just propagating an existing error
Error.throwWithStackTrace(error!, stackTrace!);
throw StateError('Snapshot has neither data nor error');
}

View File

@ -12,6 +12,20 @@ void main() {
return Text(snapshot.toString(), textDirection: TextDirection.ltr);
}
group('AsyncSnapshot', () {
test('requiring data preserves the stackTrace', () {
final StackTrace originalStackTrace = StackTrace.current;
try {
AsyncSnapshot<String>.withError(
ConnectionState.done,
Error(),
originalStackTrace,
).requireData;
fail('requireData did not throw');
} catch (error, stackTrace) {
expect(stackTrace, originalStackTrace);
}
});
test('requiring data succeeds if data is present', () {
expect(
const AsyncSnapshot<String>.withData(ConnectionState.done, 'hello').requireData,