From ec4a7c02d8533088dd899fca832681ad480d002e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Uek=C3=B6tter?= Date: Wed, 18 Oct 2023 08:48:58 +0200 Subject: [PATCH] Improve exception messages (#2000) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Further improve exception messages. ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [x] I have added the required tests to prove the fix/feature I'm adding - [x] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [x] I have updated the `CHANGELOG.md` in the corresponding package ### Additional context and info (if any) --------- Signed-off-by: Jonas Uekötter Co-authored-by: Alex Li --- dio/lib/src/dio_exception.dart | 55 +++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/dio/lib/src/dio_exception.dart b/dio/lib/src/dio_exception.dart index a9a54f85d..c7c7c9a5f 100644 --- a/dio/lib/src/dio_exception.dart +++ b/dio/lib/src/dio_exception.dart @@ -99,8 +99,11 @@ class DioException implements Exception { }) => DioException( type: DioExceptionType.connectionTimeout, - message: 'The request connection took ' - 'longer than $timeout. It was aborted.', + message: 'The request connection took longer than $timeout ' + 'and it was aborted. ' + 'To get rid of this exception, try raising the ' + 'RequestOptions.connectTimeout above the duration of $timeout or ' + 'improve the response time of the server.', requestOptions: requestOptions, response: null, error: error, @@ -112,8 +115,11 @@ class DioException implements Exception { }) => DioException( type: DioExceptionType.sendTimeout, - message: 'The request took ' - 'longer than $timeout to send data. It was aborted.', + message: 'The request took longer than $timeout to send data. ' + 'It was aborted. ' + 'To get rid of this exception, try raising the ' + 'RequestOptions.sendTimeout above the duration of $timeout or ' + 'improve the response time of the server.', requestOptions: requestOptions, response: null, error: null, @@ -126,8 +132,11 @@ class DioException implements Exception { }) => DioException( type: DioExceptionType.receiveTimeout, - message: 'The request took ' - 'longer than $timeout to receive data. It was aborted.', + message: 'The request took longer than $timeout to receive data. ' + 'It was aborted.' + 'To get rid of this exception, try raising the ' + 'RequestOptions.receiveTimeout above the duration of $timeout or ' + 'improve the response time of the server.', requestOptions: requestOptions, response: null, error: error, @@ -140,7 +149,7 @@ class DioException implements Exception { }) => DioException( type: DioExceptionType.cancel, - message: 'The request was cancelled.', + message: 'The request was manually cancelled by the user.', requestOptions: requestOptions, response: null, error: reason, @@ -154,7 +163,8 @@ class DioException implements Exception { }) => DioException( type: DioExceptionType.connectionError, - message: 'The connection errored: $reason', + message: 'The connection errored: $reason. ' + 'This indicates an error which most likely cannot be solved by the library.', requestOptions: requestOptions, response: null, error: error, @@ -214,7 +224,7 @@ class DioException implements Exception { /// Because of [ValidateStatus] we need to consider all status codes when /// creating a [DioException.badResponse]. static String _badResponseExceptionMessage(int statusCode) { - var message = ''; + final String message; if (statusCode >= 100 && statusCode < 200) { message = 'This is an informational response - the request was received, continuing processing'; @@ -233,15 +243,26 @@ class DioException implements Exception { } else { message = 'A response with a status code that is not within the range of inclusive 100 to exclusive 600' - 'is a non-standard response, possibly due to the server\'s software.'; + 'is a non-standard response, possibly due to the server\'s software'; } - return '' - 'This exception was thrown because the response has a status code of $statusCode ' - 'and RequestOptions.validateStatus was configured to throw for this status code.\n' - 'The status code of $statusCode has the following meaning: "$message"\n.' - 'Read more about status codes at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status\n' - 'In order to resolve this exception you typically have either to verify ' - 'and fix your request code or you have to fix the server code.'; + final buffer = StringBuffer(); + + buffer.writeln( + 'This exception was thrown because the response has a status code of $statusCode ' + 'and RequestOptions.validateStatus was configured to throw for this status code.', + ); + buffer.writeln( + 'The status code of $statusCode has the following meaning: "$message"', + ); + buffer.writeln( + 'Read more about status codes at https://developer.mozilla.org/en-US/docs/Web/HTTP/Status', + ); + buffer.writeln( + 'In order to resolve this exception you typically have either to verify ' + 'and fix your request code or you have to fix the server code.', + ); + + return buffer.toString(); } }