From 0ad45f2421134a103e797337d649cd347d61432f Mon Sep 17 00:00:00 2001 From: Polina Cherkasova Date: Wed, 2 Aug 2023 16:57:15 -0700 Subject: [PATCH] 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. --- .../lib/src/foundation/stack_frame.dart | 30 +++++++++++++------ .../test/foundation/stack_frame_test.dart | 4 +++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/foundation/stack_frame.dart b/packages/flutter/lib/src/foundation/stack_frame.dart index 5e22e1e82a..fc126821fe 100644 --- a/packages/flutter/lib/src/foundation/stack_frame.dart +++ b/packages/flutter/lib/src/foundation/stack_frame.dart @@ -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 = ''; String packageScheme = ''; String packagePath = ''; + 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 == '') { 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}\)$'); diff --git a/packages/flutter/test/foundation/stack_frame_test.dart b/packages/flutter/test/foundation/stack_frame_test.dart index 63138c7352..e3052188cb 100644 --- a/packages/flutter/test/foundation/stack_frame_test.dart +++ b/packages/flutter/test/foundation/stack_frame_test.dart @@ -99,6 +99,10 @@ void main() { ), ); }); + + test('Parses to null for wrong format.', () { + expect(StackFrame.fromStackTraceLine('wrong stack trace format'), null); + }); } const String stackString = '''