-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement error type identifier to mitigate obfuscated Flutter issue …
…titles (#2170) * try to mitigate runtime type not being obfuscated * fix imports * Remove prints * Update * Update * Update exception_type_identifier.dart * Add caching * Update * split up dart:io and dart:html exceptions * fix analyze * Update CHANGELOG * update * Add more tests * Update docs * Update options docs * remove print * remove CustomException * import with show * try fix test * Update CHANGELOG.md * Update CHANGELOG.md * Fix analyze * try fix test * Update CHANGELOG.md
- Loading branch information
Showing
18 changed files
with
508 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'package:http/http.dart' show ClientException; | ||
import 'dart:async' show TimeoutException, AsyncError, DeferredLoadException; | ||
import '../sentry.dart'; | ||
|
||
import 'dart_exception_type_identifier_io.dart' | ||
if (dart.library.html) 'dart_exception_type_identifier_web.dart'; | ||
|
||
class DartExceptionTypeIdentifier implements ExceptionTypeIdentifier { | ||
@override | ||
String? identifyType(dynamic throwable) { | ||
// dart:core | ||
if (throwable is ArgumentError) return 'ArgumentError'; | ||
if (throwable is AssertionError) return 'AssertionError'; | ||
if (throwable is ConcurrentModificationError) { | ||
return 'ConcurrentModificationError'; | ||
} | ||
if (throwable is FormatException) return 'FormatException'; | ||
if (throwable is IndexError) return 'IndexError'; | ||
if (throwable is NoSuchMethodError) return 'NoSuchMethodError'; | ||
if (throwable is OutOfMemoryError) return 'OutOfMemoryError'; | ||
if (throwable is RangeError) return 'RangeError'; | ||
if (throwable is StackOverflowError) return 'StackOverflowError'; | ||
if (throwable is StateError) return 'StateError'; | ||
if (throwable is TypeError) return 'TypeError'; | ||
if (throwable is UnimplementedError) return 'UnimplementedError'; | ||
if (throwable is UnsupportedError) return 'UnsupportedError'; | ||
// not adding Exception or Error because it's too generic | ||
|
||
// dart:async | ||
if (throwable is TimeoutException) return 'TimeoutException'; | ||
if (throwable is AsyncError) return 'FutureTimeout'; | ||
if (throwable is DeferredLoadException) return 'DeferredLoadException'; | ||
// not adding ParallelWaitError because it's not supported in dart 2.17.0 | ||
|
||
// dart http package | ||
if (throwable is ClientException) return 'ClientException'; | ||
|
||
// platform specific exceptions | ||
return identifyPlatformSpecificException(throwable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:meta/meta.dart'; | ||
|
||
@internal | ||
String? identifyPlatformSpecificException(dynamic throwable) { | ||
if (throwable is FileSystemException) return 'FileSystemException'; | ||
if (throwable is HttpException) return 'HttpException'; | ||
if (throwable is SocketException) return 'SocketException'; | ||
if (throwable is HandshakeException) return 'HandshakeException'; | ||
if (throwable is CertificateException) return 'CertificateException'; | ||
if (throwable is TlsException) return 'TlsException'; | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
@internal | ||
String? identifyPlatformSpecificException(dynamic throwable) { | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
/// An abstract class for identifying the type of Dart errors and exceptions. | ||
/// | ||
/// It's used in scenarios where error types need to be determined in obfuscated builds | ||
/// as [runtimeType] is not reliable in such cases. | ||
/// | ||
/// Implement this class to create custom error type identifiers for errors or exceptions. | ||
/// that we do not support out of the box. | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// class MyExceptionTypeIdentifier implements ExceptionTypeIdentifier { | ||
/// @override | ||
/// String? identifyType(dynamic throwable) { | ||
/// if (throwable is MyCustomError) return 'MyCustomError'; | ||
/// return null; | ||
/// } | ||
/// } | ||
/// ``` | ||
abstract class ExceptionTypeIdentifier { | ||
String? identifyType(dynamic throwable); | ||
} | ||
|
||
extension CacheableExceptionIdentifier on ExceptionTypeIdentifier { | ||
ExceptionTypeIdentifier withCache() => CachingExceptionTypeIdentifier(this); | ||
} | ||
|
||
@visibleForTesting | ||
class CachingExceptionTypeIdentifier implements ExceptionTypeIdentifier { | ||
@visibleForTesting | ||
ExceptionTypeIdentifier get identifier => _identifier; | ||
final ExceptionTypeIdentifier _identifier; | ||
|
||
final Map<Type, String?> _knownExceptionTypes = {}; | ||
|
||
CachingExceptionTypeIdentifier(this._identifier); | ||
|
||
@override | ||
String? identifyType(dynamic throwable) { | ||
final runtimeType = throwable.runtimeType; | ||
if (_knownExceptionTypes.containsKey(runtimeType)) { | ||
return _knownExceptionTypes[runtimeType]; | ||
} | ||
|
||
final identifiedType = _identifier.identifyType(throwable); | ||
|
||
if (identifiedType != null) { | ||
_knownExceptionTypes[runtimeType] = identifiedType; | ||
} | ||
|
||
return identifiedType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.