From 8c832a064b6b521b8d7328bc8f8a8edd395fb612 Mon Sep 17 00:00:00 2001 From: Peter Leibiger Date: Mon, 20 Mar 2023 15:00:10 +0100 Subject: [PATCH] Adapt DioErrorExtractor to new DioStackTraceExtractor usage (#1344) --- CHANGELOG.md | 1 + dio/lib/src/dio_error_extractor.dart | 11 ++-- dio/test/dio_error_extractor_test.dart | 75 +++++++++++++------------- 3 files changed, 46 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03726917e1..96d6ce48a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/dio/lib/src/dio_error_extractor.dart b/dio/lib/src/dio_error_extractor.dart index a16284f9fd..ff8ae6b1e0 100644 --- a/dio/lib/src/dio_error_extractor.dart +++ b/dio/lib/src/dio_error_extractor.dart @@ -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 { @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, ); } } diff --git a/dio/test/dio_error_extractor_test.dart b/dio/test/dio_error_extractor_test.dart index de7203ae0e..918e9fd1bb 100644 --- a/dio/test/dio_error_extractor_test.dart +++ b/dio/test/dio_error_extractor_test.dart @@ -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); + }); }); }