Skip to content

Commit a60e4d1

Browse files
authored
Relands Fix FlutterError.onError in debug mode (flutter#55500)
1 parent 1ab3878 commit a60e4d1

File tree

4 files changed

+123
-11
lines changed

4 files changed

+123
-11
lines changed

packages/flutter/lib/src/foundation/assertions.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ class FlutterError extends Error with DiagnosticableTreeMixin implements Asserti
773773

774774
/// Called whenever the Flutter framework catches an error.
775775
///
776-
/// The default behavior is to call [dumpErrorToConsole].
776+
/// The default behavior is to call [presentError].
777777
///
778778
/// You can set this to your own function to override this default behavior.
779779
/// For example, you could report all errors to your server.
@@ -783,7 +783,18 @@ class FlutterError extends Error with DiagnosticableTreeMixin implements Asserti
783783
///
784784
/// Set this to null to silently catch and ignore errors. This is not
785785
/// recommended.
786-
static FlutterExceptionHandler onError = dumpErrorToConsole;
786+
static FlutterExceptionHandler onError = (FlutterErrorDetails details) => presentError(details);
787+
788+
/// Called whenever the Flutter framework wants to present an error to the
789+
/// users.
790+
///
791+
/// The default behavior is to call [dumpErrorToConsole].
792+
///
793+
/// Plugins can override how an error is to be presented to the user. For
794+
/// example, the structured errors service extension sets its own method when
795+
/// the extension is enabled. If you want to change how Flutter responds to an
796+
/// error, use [onError] instead.
797+
static FlutterExceptionHandler presentError = dumpErrorToConsole;
787798

788799
static int _errorCount = 0;
789800

packages/flutter/lib/src/widgets/widget_inspector.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,13 +959,13 @@ mixin WidgetInspectorService {
959959
SchedulerBinding.instance.addPersistentFrameCallback(_onFrameStart);
960960

961961
final FlutterExceptionHandler structuredExceptionHandler = _reportError;
962-
final FlutterExceptionHandler defaultExceptionHandler = FlutterError.onError;
962+
final FlutterExceptionHandler defaultExceptionHandler = FlutterError.presentError;
963963

964964
_registerBoolServiceExtension(
965965
name: 'structuredErrors',
966-
getter: () async => FlutterError.onError == structuredExceptionHandler,
966+
getter: () async => FlutterError.presentError == structuredExceptionHandler,
967967
setter: (bool value) {
968-
FlutterError.onError = value ? structuredExceptionHandler : defaultExceptionHandler;
968+
FlutterError.presentError = value ? structuredExceptionHandler : defaultExceptionHandler;
969969
return Future<void>.value();
970970
},
971971
);
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:convert';
7+
8+
import 'package:flutter/foundation.dart';
9+
import 'package:flutter/rendering.dart';
10+
import 'package:flutter/widgets.dart';
11+
import 'package:flutter_test/flutter_test.dart';
12+
13+
void main() {
14+
StructureErrorTestWidgetInspectorService.runTests();
15+
}
16+
17+
typedef InspectorServiceExtensionCallback = FutureOr<Map<String, Object>> Function(Map<String, String> parameters);
18+
19+
class StructureErrorTestWidgetInspectorService extends Object with WidgetInspectorService {
20+
final Map<String, InspectorServiceExtensionCallback> extensions = <String, InspectorServiceExtensionCallback>{};
21+
22+
final Map<String, List<Map<Object, Object>>> eventsDispatched = <String, List<Map<Object, Object>>>{};
23+
24+
@override
25+
void registerServiceExtension({
26+
@required String name,
27+
@required FutureOr<Map<String, Object>> callback(Map<String, String> parameters),
28+
}) {
29+
assert(!extensions.containsKey(name));
30+
extensions[name] = callback;
31+
}
32+
33+
@override
34+
void postEvent(String eventKind, Map<Object, Object> eventData) {
35+
getEventsDispatched(eventKind).add(eventData);
36+
}
37+
38+
List<Map<Object, Object>> getEventsDispatched(String eventKind) {
39+
return eventsDispatched.putIfAbsent(eventKind, () => <Map<Object, Object>>[]);
40+
}
41+
42+
Iterable<Map<Object, Object>> getServiceExtensionStateChangedEvents(String extensionName) {
43+
return getEventsDispatched('Flutter.ServiceExtensionStateChanged')
44+
.where((Map<Object, Object> event) => event['extension'] == extensionName);
45+
}
46+
47+
Future<String> testBoolExtension(String name, Map<String, String> arguments) async {
48+
expect(extensions, contains(name));
49+
// Encode and decode to JSON to match behavior using a real service
50+
// extension where only JSON is allowed.
51+
return json.decode(json.encode(await extensions[name](arguments)))['enabled'] as String;
52+
}
53+
54+
55+
static void runTests() {
56+
final StructureErrorTestWidgetInspectorService service = StructureErrorTestWidgetInspectorService();
57+
WidgetInspectorService.instance = service;
58+
59+
test('ext.flutter.inspector.structuredErrors still report error to original on error', () async {
60+
final FlutterExceptionHandler oldHandler = FlutterError.onError;
61+
62+
FlutterErrorDetails actualError;
63+
// Creates a spy onError. This spy needs to be set before widgets binding
64+
// initializes.
65+
FlutterError.onError = (FlutterErrorDetails details) {
66+
actualError = details;
67+
};
68+
69+
WidgetsFlutterBinding.ensureInitialized();
70+
try {
71+
// Enables structured errors.
72+
expect(await service.testBoolExtension(
73+
'structuredErrors', <String, String>{'enabled': 'true'}),
74+
equals('true'));
75+
76+
// Creates an error.
77+
final FlutterErrorDetails expectedError = FlutterErrorDetailsForRendering(
78+
library: 'rendering library',
79+
context: ErrorDescription('during layout'),
80+
exception: StackTrace.current,
81+
);
82+
FlutterError.reportError(expectedError);
83+
84+
// Validates the spy still received an error.
85+
expect(actualError, expectedError);
86+
} finally {
87+
FlutterError.onError = oldHandler;
88+
}
89+
});
90+
}
91+
}

packages/flutter/test/widgets/widget_inspector_test.dart

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,11 +2276,11 @@ class TestWidgetInspectorService extends Object with WidgetInspectorService {
22762276
);
22772277
}, skip: isBrowser);
22782278

2279-
testWidgets('ext.flutter.inspector.structuredErrors', (WidgetTester tester) async {
2279+
test('ext.flutter.inspector.structuredErrors', () async {
22802280
List<Map<Object, Object>> flutterErrorEvents = service.getEventsDispatched('Flutter.Error');
22812281
expect(flutterErrorEvents, isEmpty);
22822282

2283-
final FlutterExceptionHandler oldHandler = FlutterError.onError;
2283+
final FlutterExceptionHandler oldHandler = FlutterError.presentError;
22842284

22852285
try {
22862286
// Enable structured errors.
@@ -2320,9 +2320,19 @@ class TestWidgetInspectorService extends Object with WidgetInspectorService {
23202320
error = flutterErrorEvents.last;
23212321
expect(error['errorsSinceReload'], 1);
23222322

2323-
// Reload the app.
2324-
tester.binding.reassembleApplication();
2325-
await tester.pump();
2323+
// Reloads the app.
2324+
final FlutterExceptionHandler oldHandler = FlutterError.onError;
2325+
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
2326+
// We need the runTest to setup the fake async in the test binding.
2327+
await binding.runTest(() async {
2328+
binding.reassembleApplication();
2329+
await binding.pump();
2330+
}, () { });
2331+
// The run test overrides the flutter error handler, so we should
2332+
// restore it back for the structure error to continue working.
2333+
FlutterError.onError = oldHandler;
2334+
// Cleans up the fake async so it does not bleed into next test.
2335+
binding.postTest();
23262336

23272337
// Send another error.
23282338
FlutterError.reportError(FlutterErrorDetailsForRendering(
@@ -2337,7 +2347,7 @@ class TestWidgetInspectorService extends Object with WidgetInspectorService {
23372347
error = flutterErrorEvents.last;
23382348
expect(error['errorsSinceReload'], 0);
23392349
} finally {
2340-
FlutterError.onError = oldHandler;
2350+
FlutterError.presentError = oldHandler;
23412351
}
23422352
});
23432353

0 commit comments

Comments
 (0)