Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Jul 16, 2024
1 parent 526102e commit 745a18f
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 29 deletions.
2 changes: 1 addition & 1 deletion dart/lib/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export 'src/sentry_baggage.dart';
export 'src/exception_cause_extractor.dart';
export 'src/exception_cause.dart';
export 'src/exception_stacktrace_extractor.dart';
export 'src/error_type_identifier.dart';
export 'src/exception_type_identifier.dart';
// URL
// ignore: invalid_export_of_internal_element
export 'src/utils/http_sanitizer.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import '../sentry.dart';

class DartErrorIdentifier implements ErrorTypeIdentifier {
class DartExceptionTypeIdentifier implements ExceptionTypeIdentifier {
@override
String? getTypeName(dynamic error) {
if (error is NoSuchMethodError) return 'NoSuchMethodError';
if (error is FormatException) return 'FormatException';
if (error is TypeError) return 'TypeError';
String? identifyType(dynamic error) {
if (error is ArgumentError) return 'ArgumentError';
if (error is StateError) return 'StateError';
if (error is UnsupportedError) return 'UnsupportedError';
if (error is UnimplementedError) return 'UnimplementedError';
if (error is AssertionError) return 'AssertionError';
if (error is ConcurrentModificationError)
return 'ConcurrentModificationError';

Check notice on line 9 in dart/lib/src/dart_exception_type_identifier.dart

View workflow job for this annotation

GitHub Actions / analyze / analyze

Statements in an if should be enclosed in a block.

Try wrapping the statement in a block. See https://dart.dev/lints/curly_braces_in_flow_control_structures to learn more about this problem.
if (error is FormatException) return 'FormatException';
if (error is IndexError) return 'IndexError';
if (error is NoSuchMethodError) return 'NoSuchMethodError';
if (error is OutOfMemoryError) return 'OutOfMemoryError';
if (error is RangeError) return 'RangeError';
if (error is StackOverflowError) return 'StackOverflowError';
if (error is StateError) return 'StateError';
if (error is TypeError) return 'TypeError';
if (error is UnimplementedError) return 'UnimplementedError';
if (error is UnsupportedError) return 'UnsupportedError';
// we purposefully don't include Exception or Error since it's too generic
return null;
}
}
3 changes: 0 additions & 3 deletions dart/lib/src/error_type_identifier.dart

This file was deleted.

25 changes: 25 additions & 0 deletions dart/lib/src/exception_type_identifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// An abstract class for identifying the type of Dart errors and exceptions.
///
/// This class provides a contract for implementing error type identification
/// in Dart. 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.
///
/// Add the implementation using [SentryOptions.addExceptionTypeIdentifier].
///
/// Example:
/// ```dart
/// class MyErrorTypeIdentifier implements ErrorTypeIdentifier {
/// @override
/// String? identifyType(dynamic error) {
/// if (error is MyCustomError) return 'MyCustomError';
/// return null;
/// }
/// }
/// ```
abstract class ExceptionTypeIdentifier {
String? identifyType(dynamic throwable);
}
4 changes: 2 additions & 2 deletions dart/lib/src/sentry_exception_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class SentryExceptionFactory {
final value = throwableString.replaceAll(stackTraceString, '').trim();

String errorTypeName = throwable.runtimeType.toString();
for (final errorTypeIdentifier in _options.errorTypeIdentifiers) {
final identifiedErrorType = errorTypeIdentifier.getTypeName(throwable);
for (final errorTypeIdentifier in _options.exceptionTypeIdentifiers) {
final identifiedErrorType = errorTypeIdentifier.identifyType(throwable);
if (identifiedErrorType != null) {
errorTypeName = identifiedErrorType;
break;
Expand Down
14 changes: 8 additions & 6 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:meta/meta.dart';
import '../sentry.dart';
import 'client_reports/client_report_recorder.dart';
import 'client_reports/noop_client_report_recorder.dart';
import 'dart_error_type_identifier.dart';
import 'dart_exception_type_identifier.dart';
import 'diagnostic_logger.dart';
import 'environment/environment_variables.dart';
import 'noop_client.dart';
Expand Down Expand Up @@ -437,14 +437,16 @@ class SentryOptions {
/// Settings this to `false` will set the `level` to [SentryLevel.error].
bool markAutomaticallyCollectedErrorsAsFatal = true;

final List<ErrorTypeIdentifier> _errorTypeIdentifiers = [
DartErrorIdentifier(),
final List<ExceptionTypeIdentifier> _exceptionTypeIdentifiers = [
DartExceptionTypeIdentifier(),
];

List<ErrorTypeIdentifier> get errorTypeIdentifiers => _errorTypeIdentifiers;
List<ExceptionTypeIdentifier> get exceptionTypeIdentifiers =>
_exceptionTypeIdentifiers;

void addErrorTypeIdentifier(ErrorTypeIdentifier errorTypeIdentifier) {
_errorTypeIdentifiers.add(errorTypeIdentifier);
void addExceptionTypeIdentifier(

Check warning on line 447 in dart/lib/src/sentry_options.dart

View check run for this annotation

Codecov / codecov/patch

dart/lib/src/sentry_options.dart#L447

Added line #L447 was not covered by tests
ExceptionTypeIdentifier exceptionTypeIdentifier) {
_exceptionTypeIdentifiers.insert(0, exceptionTypeIdentifier);

Check warning on line 449 in dart/lib/src/sentry_options.dart

View check run for this annotation

Codecov / codecov/patch

dart/lib/src/sentry_options.dart#L449

Added line #L449 was not covered by tests
}

/// The Spotlight configuration.
Expand Down
32 changes: 24 additions & 8 deletions flutter/lib/src/flutter_error_type_identifier.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import '../sentry_flutter.dart';

class FlutterErrorIdentifier implements ErrorTypeIdentifier {
class FlutterExceptionTypeIdentifier implements ExceptionTypeIdentifier {
@override

Check warning on line 10 in flutter/lib/src/flutter_error_type_identifier.dart

View check run for this annotation

Codecov / codecov/patch

flutter/lib/src/flutter_error_type_identifier.dart#L10

Added line #L10 was not covered by tests
String? getTypeName(dynamic error) {
if (error is FlutterError) return 'FlutterError';
if (error is PlatformException) return 'PlatformException';
if (error is MissingPluginException) return 'MissingPluginException';
if (error is AssertionError) return 'AssertionError';
if (error is NetworkImageLoadException) return 'NetworkImageLoadException';
if (error is TickerCanceled) return 'TickerCanceled';
String? identifyType(dynamic throwable) {
if (throwable is FlutterError) return 'FlutterError';
if (throwable is PlatformException) return 'PlatformException';
if (throwable is MissingPluginException) return 'MissingPluginException';
if (throwable is AssertionError) return 'AssertionError';
if (throwable is NetworkImageLoadException)

Check warning on line 16 in flutter/lib/src/flutter_error_type_identifier.dart

View check run for this annotation

Codecov / codecov/patch

flutter/lib/src/flutter_error_type_identifier.dart#L12-L16

Added lines #L12 - L16 were not covered by tests
return 'NetworkImageLoadException';

Check notice on line 17 in flutter/lib/src/flutter_error_type_identifier.dart

View workflow job for this annotation

GitHub Actions / analyze / analyze

Statements in an if should be enclosed in a block.

Try wrapping the statement in a block. See https://dart.dev/lints/curly_braces_in_flow_control_structures to learn more about this problem.
if (throwable is TickerCanceled) return 'TickerCanceled';

Check warning on line 18 in flutter/lib/src/flutter_error_type_identifier.dart

View check run for this annotation

Codecov / codecov/patch

flutter/lib/src/flutter_error_type_identifier.dart#L18

Added line #L18 was not covered by tests

// dart:io Exceptions
if (!kIsWeb) {
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';

Check warning on line 26 in flutter/lib/src/flutter_error_type_identifier.dart

View check run for this annotation

Codecov / codecov/patch

flutter/lib/src/flutter_error_type_identifier.dart#L22-L26

Added lines #L22 - L26 were not covered by tests
// not adding TlsException and IOException because it's too generic
}
return null;
}
}

bool isSubtype<S, T>() => <S>[] is List<T>;

Check warning on line 33 in flutter/lib/src/flutter_error_type_identifier.dart

View check run for this annotation

Codecov / codecov/patch

flutter/lib/src/flutter_error_type_identifier.dart#L33

Added line #L33 was not covered by tests
2 changes: 1 addition & 1 deletion flutter/lib/src/sentry_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mixin SentryFlutter {

options.addPerformanceCollector(SpanFrameMetricsCollector(options));

options.addErrorTypeIdentifier(FlutterErrorIdentifier());
options.addExceptionTypeIdentifier(FlutterExceptionTypeIdentifier());

_setSdk(options);
}
Expand Down

0 comments on commit 745a18f

Please sign in to comment.