From e6a8c275e6b69357d9c85ea5af0f8b2e16a875c8 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Wed, 18 Sep 2024 21:58:08 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E2=9C=A8=20Introduce=20`DioExceptionLogBui?= =?UTF-8?q?lder`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/dio_exception.dart | 29 +++++++++++++++++++++++++---- dio/lib/src/utils.dart | 4 ++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/dio/lib/src/dio_exception.dart b/dio/lib/src/dio_exception.dart index 69aac43d0..c480107b9 100644 --- a/dio/lib/src/dio_exception.dart +++ b/dio/lib/src/dio_exception.dart @@ -1,5 +1,6 @@ import 'options.dart'; import 'response.dart'; +import 'utils.dart' show warningLog; /// Deprecated in favor of [DioExceptionType] and will be removed in future major versions. @Deprecated('Use DioExceptionType instead. This will be removed in 6.0.0') @@ -205,6 +206,9 @@ class DioException implements Exception { /// The error message that throws a [DioException]. final String? message; + /// Users can customize the logging content when a [DioException] was thrown. + static DioExceptionLogBuilder logBuilder = defaultDioExceptionLogBuilder; + /// Generate a new [DioException] by combining given values and original values. DioException copyWith({ RequestOptions? requestOptions, @@ -226,11 +230,12 @@ class DioException implements Exception { @override String toString() { - String msg = 'DioException [${type.toPrettyDescription()}]: $message'; - if (error != null) { - msg += '\nError: $error'; + try { + return logBuilder(this); + } catch (e, s) { + warningLog(e, s); + return defaultDioExceptionLogBuilder(this); } - return msg; } /// Because of [ValidateStatus] we need to consider all status codes when @@ -278,3 +283,19 @@ class DioException implements Exception { return buffer.toString(); } } + +/// The log builder's signature. +typedef DioExceptionLogBuilder = String Function(DioException e); + +/// The default implementation of logging the [DioException] as text content. +String defaultDioExceptionLogBuilder(DioException e) { + final buffer = StringBuffer( + 'DioException [${e.type.toPrettyDescription()}]: ' + '${e.message}', + ); + if (e.error != null) { + buffer.writeln(); + buffer.write('Error: ${e.error}'); + } + return buffer.toString(); +} diff --git a/dio/lib/src/utils.dart b/dio/lib/src/utils.dart index 06d5aa94e..1a2e1d801 100644 --- a/dio/lib/src/utils.dart +++ b/dio/lib/src/utils.dart @@ -154,10 +154,10 @@ Map caseInsensitiveKeyMap([Map? value]) { } // TODO(Alex): Provide a configurable property on the Dio class once https://github.com/cfug/dio/discussions/1982 has made some progress. -void warningLog(String message, StackTrace stackTrace) { +void warningLog(Object message, StackTrace stackTrace) { if (!kReleaseMode) { dev.log( - message, + message.toString(), level: 900, name: '🔔 Dio', stackTrace: stackTrace, From 796266bc72a398cece4d385c006b181366968b5a Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 16 Dec 2024 20:47:33 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E2=9C=85=20Adds=20the=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/test/exception_test.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/dio/test/exception_test.dart b/dio/test/exception_test.dart index bfc0f95d7..63920a841 100644 --- a/dio/test/exception_test.dart +++ b/dio/test/exception_test.dart @@ -52,4 +52,16 @@ void main() { }, testOn: '!browser', ); + + test('DioExceptionLogBuilder', () { + final exception = DioException( + requestOptions: RequestOptions(path: 'just/a/test', method: 'POST'), + ); + DioException.logBuilder = (e) => 'Hey, Dio throws an exception: ' + '${exception.requestOptions.path}, ${exception.requestOptions.method}'; + expect( + exception.toString(), + equals('Hey, Dio throws an exception: just/a/test, POST'), + ); + }); } From ee79933e46bf41855c9bb13527aaedbe619f31f4 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 16 Dec 2024 20:49:11 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=F0=9F=93=9D=20CHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index 4243c1626..6363317d9 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -8,6 +8,7 @@ See the [Migration Guide][] for the complete breaking changes list.** - Update comments and strings with `MultipartFile`. - Removes redundant warnings when composing request options on Web. - Fixes boundary inconsistency in `FormData.clone()`. +- Enables configuring the logging details of `DioException` globally. ## 5.7.0 From 287141733d157a69395f520f2e8aab4bfca445ed Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 28 Jan 2025 17:39:08 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=F0=9F=9A=9A=20Rename?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/dio_exception.dart | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/dio/lib/src/dio_exception.dart b/dio/lib/src/dio_exception.dart index c480107b9..3a72893f7 100644 --- a/dio/lib/src/dio_exception.dart +++ b/dio/lib/src/dio_exception.dart @@ -206,8 +206,9 @@ class DioException implements Exception { /// The error message that throws a [DioException]. final String? message; - /// Users can customize the logging content when a [DioException] was thrown. - static DioExceptionLogBuilder logBuilder = defaultDioExceptionLogBuilder; + /// Users can customize the content of [toString] when thrown. + static DioExceptionReadableStringBuilder readableStringBuilder = + defaultDioExceptionReadableStringBuilder; /// Generate a new [DioException] by combining given values and original values. DioException copyWith({ @@ -231,10 +232,10 @@ class DioException implements Exception { @override String toString() { try { - return logBuilder(this); + return readableStringBuilder(this); } catch (e, s) { warningLog(e, s); - return defaultDioExceptionLogBuilder(this); + return defaultDioExceptionReadableStringBuilder(this); } } @@ -284,11 +285,12 @@ class DioException implements Exception { } } -/// The log builder's signature. -typedef DioExceptionLogBuilder = String Function(DioException e); +/// The readable string builder's signature of +/// [DioException.readableStringBuilder]. +typedef DioExceptionReadableStringBuilder = String Function(DioException e); -/// The default implementation of logging the [DioException] as text content. -String defaultDioExceptionLogBuilder(DioException e) { +/// The default implementation of building a readable string of [DioException]. +String defaultDioExceptionReadableStringBuilder(DioException e) { final buffer = StringBuffer( 'DioException [${e.type.toPrettyDescription()}]: ' '${e.message}', From 1f41008f1f08d38a6130a78825a2679bd6977a44 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 28 Jan 2025 17:39:21 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E2=9C=85=20Try=20to=20increase=20test=20co?= =?UTF-8?q?verage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/test/exception_test.dart | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/dio/test/exception_test.dart b/dio/test/exception_test.dart index 63920a841..6fabf0538 100644 --- a/dio/test/exception_test.dart +++ b/dio/test/exception_test.dart @@ -53,15 +53,31 @@ void main() { testOn: '!browser', ); - test('DioExceptionLogBuilder', () { + test('DioExceptionReadableStringBuilder', () { + final requestOptions = RequestOptions(path: 'just/a/test', method: 'POST'); final exception = DioException( - requestOptions: RequestOptions(path: 'just/a/test', method: 'POST'), + requestOptions: requestOptions, + response: Response(requestOptions: requestOptions), + error: 'test', + message: 'test message', + stackTrace: StackTrace.current, ); - DioException.logBuilder = (e) => 'Hey, Dio throws an exception: ' - '${exception.requestOptions.path}, ${exception.requestOptions.method}'; + DioException.readableStringBuilder = (e) => 'Hey, Dio throws an exception: ' + '${e.requestOptions.path}, ' + '${e.requestOptions.method}, ' + '${e.type}, ' + '${e.error}, ' + '${e.stackTrace}, ' + '${e.message}'; expect( exception.toString(), - equals('Hey, Dio throws an exception: just/a/test, POST'), + 'Hey, Dio throws an exception: ' + 'just/a/test, ' + 'POST, ' + 'DioExceptionType.unknown, ' + 'test, ' + '${exception.stackTrace}, ' + 'test message', ); }); } From 110848dbc716d0516cacaedc192fa0e8476510b8 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 28 Jan 2025 17:44:20 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E2=9C=A8=20Adds=20local=20override=20abili?= =?UTF-8?q?ty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/CHANGELOG.md | 2 +- dio/lib/src/dio_exception.dart | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index 14574e8c1..3eafb3580 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -10,7 +10,7 @@ See the [Migration Guide][] for the complete breaking changes list.** - Fixes boundary inconsistency in `FormData.clone()`. - Support `FileAccessMode` in `Dio.download` and `Dio.downloadUri` to change download file opening mode - Fix `ListParam` equality by using the `DeepCollectionEquality`. -- Enables configuring the logging details of `DioException` globally. +- Enables configuring the logging details of `DioException` globally and locally. ## 5.7.0 diff --git a/dio/lib/src/dio_exception.dart b/dio/lib/src/dio_exception.dart index 3a72893f7..6dcdc375c 100644 --- a/dio/lib/src/dio_exception.dart +++ b/dio/lib/src/dio_exception.dart @@ -210,6 +210,10 @@ class DioException implements Exception { static DioExceptionReadableStringBuilder readableStringBuilder = defaultDioExceptionReadableStringBuilder; + /// Each exception can be override with a customized builder or fallback to + /// the default [DioException.readableStringBuilder]. + DioExceptionReadableStringBuilder? stringBuilder; + /// Generate a new [DioException] by combining given values and original values. DioException copyWith({ RequestOptions? requestOptions, @@ -232,7 +236,7 @@ class DioException implements Exception { @override String toString() { try { - return readableStringBuilder(this); + return stringBuilder?.call(this) ?? readableStringBuilder(this); } catch (e, s) { warningLog(e, s); return defaultDioExceptionReadableStringBuilder(this); From 880c46e1d882ff4bfaf52e40511dfc4260da3406 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 28 Jan 2025 17:45:18 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E2=9C=85=20Add=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/test/exception_test.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dio/test/exception_test.dart b/dio/test/exception_test.dart index 6fabf0538..740ceb5dc 100644 --- a/dio/test/exception_test.dart +++ b/dio/test/exception_test.dart @@ -79,5 +79,7 @@ void main() { '${exception.stackTrace}, ' 'test message', ); + exception.stringBuilder = (e) => 'Locally override: ${e.message}'; + expect(exception.toString(), 'Locally override: test message'); }); } From 92419aa62217242aa40a9798bf0e946b87cd3788 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 28 Jan 2025 18:56:09 +0800 Subject: [PATCH 8/8] Update coverage_comment.yml Signed-off-by: Alex Li --- .github/workflows/coverage_comment.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/coverage_comment.yml b/.github/workflows/coverage_comment.yml index 90bbf4e5b..e2aab9c3f 100644 --- a/.github/workflows/coverage_comment.yml +++ b/.github/workflows/coverage_comment.yml @@ -13,7 +13,8 @@ on: jobs: download_coverage: runs-on: ubuntu-latest - if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' + # if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' + if: github.event.workflow_run.event == 'pull_request' steps: - name: Download artifact id: download-artifact