Skip to content

Commit 0ad45f2

Browse files
authored
Update stack_frame.dart to parse unexpected error format to null. (flutter#131786)
Fixes flutter#131627 Originally this code sometimes was returning null and sometimes was failing, when stack frame is in unexpected format. This PR updates for one of the code paths from failing to returning null.
1 parent 9cda309 commit 0ad45f2

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,38 @@ class StackFrame {
8282
.toList();
8383
}
8484

85-
static StackFrame? _parseWebFrame(String line) {
85+
/// Parses a single [StackFrame] from a line of a [StackTrace].
86+
///
87+
/// Returns null if format is not as expected.
88+
static StackFrame? _tryParseWebFrame(String line) {
8689
if (kDebugMode) {
87-
return _parseWebDebugFrame(line);
90+
return _tryParseWebDebugFrame(line);
8891
} else {
89-
return _parseWebNonDebugFrame(line);
92+
return _tryParseWebNonDebugFrame(line);
9093
}
9194
}
9295

93-
static StackFrame _parseWebDebugFrame(String line) {
96+
/// Parses a single [StackFrame] from a line of a [StackTrace].
97+
///
98+
/// Returns null if format is not as expected.
99+
static StackFrame? _tryParseWebDebugFrame(String line) {
94100
// This RegExp is only partially correct for flutter run/test differences.
95101
// https://github.com/flutter/flutter/issues/52685
96102
final bool hasPackage = line.startsWith('package');
97103
final RegExp parser = hasPackage
98104
? RegExp(r'^(package.+) (\d+):(\d+)\s+(.+)$')
99105
: RegExp(r'^(.+) (\d+):(\d+)\s+(.+)$');
100-
Match? match = parser.firstMatch(line);
101-
assert(match != null, 'Expected $line to match $parser.');
102-
match = match!;
106+
107+
final Match? match = parser.firstMatch(line);
108+
109+
if (match == null) {
110+
return null;
111+
}
103112

104113
String package = '<unknown>';
105114
String packageScheme = '<unknown>';
106115
String packagePath = '<unknown>';
116+
107117
if (hasPackage) {
108118
packageScheme = 'package';
109119
final Uri packageUri = Uri.parse(match.group(1)!);
@@ -132,7 +142,7 @@ class StackFrame {
132142

133143
// Parses `line` as a stack frame in profile and release Web builds. If not
134144
// recognized as a stack frame, returns null.
135-
static StackFrame? _parseWebNonDebugFrame(String line) {
145+
static StackFrame? _tryParseWebNonDebugFrame(String line) {
136146
final Match? match = _webNonDebugFramePattern.firstMatch(line);
137147
if (match == null) {
138148
// On the Web in non-debug builds the stack trace includes the exception
@@ -169,6 +179,8 @@ class StackFrame {
169179
}
170180

171181
/// Parses a single [StackFrame] from a single line of a [StackTrace].
182+
///
183+
/// Returns null if format is not as expected.
172184
static StackFrame? fromStackTraceLine(String line) {
173185
if (line == '<asynchronous suspension>') {
174186
return asynchronousSuspension;
@@ -185,7 +197,7 @@ class StackFrame {
185197

186198
// Web frames.
187199
if (!line.startsWith('#')) {
188-
return _parseWebFrame(line);
200+
return _tryParseWebFrame(line);
189201
}
190202

191203
final RegExp parser = RegExp(r'^#(\d+) +(.+) \((.+?):?(\d+){0,1}:?(\d+){0,1}\)$');

packages/flutter/test/foundation/stack_frame_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ void main() {
9999
),
100100
);
101101
});
102+
103+
test('Parses to null for wrong format.', () {
104+
expect(StackFrame.fromStackTraceLine('wrong stack trace format'), null);
105+
});
102106
}
103107

104108
const String stackString = '''

0 commit comments

Comments
 (0)