Unverified Commit 868c4d8c authored by Dan Field's avatar Dan Field Committed by GitHub

Fix issue with stack traces getting mangled (#59900)

parent 2a573a32
......@@ -389,6 +389,28 @@ class FlutterErrorDetails with Diagnosticable {
this.silent = false,
});
/// Creates a copy of the error details but with the given fields replaced
/// with new values.
FlutterErrorDetails copyWith({
DiagnosticsNode context,
dynamic exception,
InformationCollector informationCollector,
String library,
bool silent,
StackTrace stack,
IterableFilter<String> stackFilter,
}) {
return FlutterErrorDetails(
context: context ?? this.context,
exception: exception ?? this.exception,
informationCollector: informationCollector ?? this.informationCollector,
library: library ?? this.library,
silent: silent ?? this.silent,
stack: stack ?? this.stack,
stackFilter: stackFilter ?? this.stackFilter,
);
}
/// Transformers to transform [DiagnosticsNode] in [DiagnosticPropertiesBuilder]
/// into a more descriptive form.
///
......
......@@ -579,6 +579,10 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
// our main future completing.
assert(Zone.current == _parentZone);
if (_pendingExceptionDetails != null) {
assert(
_unmangle(_pendingExceptionDetails.stack) == _pendingExceptionDetails.stack,
'The test binding presented an unmangled stack trace to the framework.',
);
debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
reportTestException(_pendingExceptionDetails, testDescription);
_pendingExceptionDetails = null;
......@@ -611,6 +615,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
_oldExceptionHandler = FlutterError.onError;
int _exceptionCount = 0; // number of un-taken exceptions
FlutterError.onError = (FlutterErrorDetails details) {
details = details.copyWith(stack: _unmangle(details.stack));
if (_pendingExceptionDetails != null) {
debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the errors!
if (_exceptionCount == 0) {
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:stack_trace/stack_trace.dart' as stack_trace;
Future<void> main() async {
test('demangles stacks', () async {
// Test that the tester bindings unmangle stacks that come in as
// package:stack_trace types.
// Uses runTest directly so that the test does not get hung up waiting for
// the error reporter to be reset to the original one.
final Completer<FlutterErrorDetails> errorCompleter = Completer<FlutterErrorDetails>();
final TestExceptionReporter oldReporter = reportTestException;
reportTestException = (FlutterErrorDetails details, String testDescription) {
errorCompleter.complete(details);
reportTestException = oldReporter;
};
final AutomatedTestWidgetsFlutterBinding binding = AutomatedTestWidgetsFlutterBinding();
await binding.runTest(() async {
final Completer<String> completer = Completer<String>();
completer.future.then(
(String value) {},
onError: (dynamic error, StackTrace stack) {
assert(stack is stack_trace.Chain);
FlutterError.reportError(FlutterErrorDetails(
exception: error,
stack: stack,
));
}
);
completer.completeError(const CustomException());
}, null);
final FlutterErrorDetails details = await errorCompleter.future;
expect(details, isNotNull);
expect(details.exception, isA<CustomException>());
reportTestException = oldReporter;
});
}
class CustomException implements Exception {
const CustomException();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment