Skip to content

Commit

Permalink
Adapt DioErrorExtractor to new DioStackTraceExtractor usage (#1344)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhnroyal authored Mar 20, 2023
1 parent 40680d3 commit 8c832a0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Fixes

- `DioErrorExtractor` no longer extracts `DioError.stackTrace` which is done via `DioStackTraceExtractor` instead ([#1344](https://github.com/getsentry/sentry-dart/pull/1344))
- LoadImageListIntegration won't throw bad state if there is no exceptions in the event ([#1347](https://github.com/getsentry/sentry-dart/pull/1347))

### Dependencies
Expand Down
11 changes: 7 additions & 4 deletions dio/lib/src/dio_error_extractor.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import 'package:dio/dio.dart';
import 'package:sentry/sentry.dart';

/// Extracts the inner exception and stacktrace from [DioError]
/// Extracts the inner cause and stacktrace from [DioError]
class DioErrorExtractor extends ExceptionCauseExtractor<DioError> {
@override
ExceptionCause? cause(DioError error) {
if (error.stackTrace == null) {
final cause = error.error;
if (cause == null) {
return null;
}
return ExceptionCause(
error.error ?? 'DioError inner stacktrace',
error.stackTrace,
cause,
// A custom [ExceptionStackTraceExtractor] can be
// used to extract the inner stacktrace in other cases
cause is Error ? cause.stackTrace : null,
);
}
}
75 changes: 38 additions & 37 deletions dio/test/dio_error_extractor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,55 @@ import 'package:test/test.dart';
void main() {
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

test('$DioErrorExtractor extracts error and stacktrace', () {
final sut = fixture.getSut();
final exception = Exception('foo bar');
final stacktrace = StackTrace.current;
group(DioErrorExtractor, () {
setUp(() {
fixture = Fixture();
});

final dioError = DioError(
error: exception,
requestOptions: RequestOptions(path: '/foo/bar'),
stackTrace: stacktrace,
);
test('extracts error and stacktrace', () {
final sut = fixture.getSut();
late Error error;
try {
throw ArgumentError('foo bar');
} on ArgumentError catch (e) {
error = e;
}
final dioError = DioError(
error: error,
requestOptions: RequestOptions(path: '/foo/bar'),
);

final cause = sut.cause(dioError);
final cause = sut.cause(dioError);

expect(cause?.exception, exception);
expect(cause?.stackTrace, stacktrace);
});
expect(cause?.exception, error);
expect(cause?.stackTrace, error.stackTrace);
});

test('$DioErrorExtractor extracts stacktrace only', () {
final sut = fixture.getSut();
final stacktrace = StackTrace.current;
test('extracts exception', () {
final sut = fixture.getSut();

final dioError = DioError(
requestOptions: RequestOptions(path: '/foo/bar'),
stackTrace: stacktrace,
);
final dioError = DioError(
error: 'Some error',
requestOptions: RequestOptions(path: '/foo/bar'),
);

final cause = sut.cause(dioError);
final cause = sut.cause(dioError);

expect(cause?.exception, 'DioError inner stacktrace');
expect(cause?.stackTrace, stacktrace);
});
expect(cause?.exception, 'Some error');
expect(cause?.stackTrace, isNull);
});

test('$DioErrorExtractor extracts nothing with missing stacktrace', () {
final sut = fixture.getSut();
final exception = Exception('foo bar');
test('extracts nothing with missing cause', () {
final sut = fixture.getSut();

final dioError = DioError(
error: exception,
requestOptions: RequestOptions(path: '/foo/bar'),
);
final dioError = DioError(
requestOptions: RequestOptions(path: '/foo/bar'),
);

final cause = sut.cause(dioError);
final cause = sut.cause(dioError);

expect(cause, isNull);
expect(cause, isNull);
});
});
}

Expand Down

0 comments on commit 8c832a0

Please sign in to comment.