Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt DioErrorExtractor to new DioStackTraceExtractor usage #1344

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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