Skip to content

Commit

Permalink
Improve exception message for DioException.badResponse (#1998)
Browse files Browse the repository at this point in the history
Since we receive a lot of issues around the topic of status code, I
think we should improve message of it. So I've made it more descriptinve
and actionable.

### 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)

<!-- Provide more context and info about the PR. -->
  • Loading branch information
ueman authored Oct 16, 2023
1 parent 2fbd0a5 commit a395cad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
## Unreleased

- Raise warning for `Map`s other than `Map<String, dynamic>` when encoding request data.
- Improve exception messages

## 5.3.3

Expand Down
37 changes: 35 additions & 2 deletions dio/lib/src/dio_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ class DioException implements Exception {
}) =>
DioException(
type: DioExceptionType.badResponse,
message: 'The request returned an '
'invalid status code of $statusCode.',
message: _badResponseExceptionMessage(statusCode),
requestOptions: requestOptions,
response: response,
error: null,
Expand Down Expand Up @@ -211,4 +210,38 @@ class DioException implements Exception {
}
return msg;
}

/// Because of [ValidateStatus] we need to consider all status codes when
/// creating a [DioException.badResponse].
static String _badResponseExceptionMessage(int statusCode) {
var message = '';
if (statusCode >= 100 && statusCode < 200) {
message =
'This is an informational response - the request was received, continuing processing';
} else if (statusCode >= 200 && statusCode < 300) {
message =
'The request was successfully received, understood, and accepted';
} else if (statusCode >= 300 && statusCode < 400) {
message =
'Redirection: further action needs to be taken in order to complete the request';
} else if (statusCode >= 400 && statusCode < 500) {
message =
'Client error - the request contains bad syntax or cannot be fulfilled';
} else if (statusCode >= 500 && statusCode < 600) {
message =
'Server error - the server failed to fulfil an apparently valid request';
} 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.';
}

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.';
}
}

0 comments on commit a395cad

Please sign in to comment.