Unverified Commit 0ad45f24 authored by Polina Cherkasova's avatar Polina Cherkasova Committed by GitHub

Update stack_frame.dart to parse unexpected error format to null. (#131786)

Fixes https://github.com/flutter/flutter/issues/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.
parent 9cda3092
......@@ -82,28 +82,38 @@ class StackFrame {
.toList();
}
static StackFrame? _parseWebFrame(String line) {
/// Parses a single [StackFrame] from a line of a [StackTrace].
///
/// Returns null if format is not as expected.
static StackFrame? _tryParseWebFrame(String line) {
if (kDebugMode) {
return _parseWebDebugFrame(line);
return _tryParseWebDebugFrame(line);
} else {
return _parseWebNonDebugFrame(line);
return _tryParseWebNonDebugFrame(line);
}
}
static StackFrame _parseWebDebugFrame(String line) {
/// Parses a single [StackFrame] from a line of a [StackTrace].
///
/// Returns null if format is not as expected.
static StackFrame? _tryParseWebDebugFrame(String line) {
// This RegExp is only partially correct for flutter run/test differences.
// https://github.com/flutter/flutter/issues/52685
final bool hasPackage = line.startsWith('package');
final RegExp parser = hasPackage
? RegExp(r'^(package.+) (\d+):(\d+)\s+(.+)$')
: RegExp(r'^(.+) (\d+):(\d+)\s+(.+)$');
Match? match = parser.firstMatch(line);
assert(match != null, 'Expected $line to match $parser.');
match = match!;
final Match? match = parser.firstMatch(line);
if (match == null) {
return null;
}
String package = '<unknown>';
String packageScheme = '<unknown>';
String packagePath = '<unknown>';
if (hasPackage) {
packageScheme = 'package';
final Uri packageUri = Uri.parse(match.group(1)!);
......@@ -132,7 +142,7 @@ class StackFrame {
// Parses `line` as a stack frame in profile and release Web builds. If not
// recognized as a stack frame, returns null.
static StackFrame? _parseWebNonDebugFrame(String line) {
static StackFrame? _tryParseWebNonDebugFrame(String line) {
final Match? match = _webNonDebugFramePattern.firstMatch(line);
if (match == null) {
// On the Web in non-debug builds the stack trace includes the exception
......@@ -169,6 +179,8 @@ class StackFrame {
}
/// Parses a single [StackFrame] from a single line of a [StackTrace].
///
/// Returns null if format is not as expected.
static StackFrame? fromStackTraceLine(String line) {
if (line == '<asynchronous suspension>') {
return asynchronousSuspension;
......@@ -185,7 +197,7 @@ class StackFrame {
// Web frames.
if (!line.startsWith('#')) {
return _parseWebFrame(line);
return _tryParseWebFrame(line);
}
final RegExp parser = RegExp(r'^#(\d+) +(.+) \((.+?):?(\d+){0,1}:?(\d+){0,1}\)$');
......
......@@ -99,6 +99,10 @@ void main() {
),
);
});
test('Parses to null for wrong format.', () {
expect(StackFrame.fromStackTraceLine('wrong stack trace format'), null);
});
}
const String stackString = '''
......
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