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

Add constructor for DioExceptionType.badCertificate #2174

Merged
merged 3 commits into from
Apr 19, 2024
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 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 the min Dart SDK version to 2.18.0.
- Add constructor for `DioExceptionType.badCertificate`.

## 5.4.3+1

Expand Down
4 changes: 1 addition & 3 deletions dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,9 @@ class IOHttpClientAdapter implements HttpClientAdapter {
port,
);
if (!isCertApproved) {
throw DioException(
throw DioException.badCertificate(
requestOptions: options,
type: DioExceptionType.badCertificate,
error: responseStream.certificate,
message: 'The certificate of the response is not approved.',
);
}
}
Expand Down
30 changes: 21 additions & 9 deletions dio/lib/src/dio_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum DioExceptionType {
/// It occurs when url is sent timeout.
sendTimeout,

///It occurs when receiving timeout.
/// It occurs when receiving timeout.
receiveTimeout,

/// Caused by an incorrect certificate as configured by [ValidateCertificate].
Expand Down Expand Up @@ -86,10 +86,10 @@ class DioException implements Exception {
}) =>
DioException(
type: DioExceptionType.badResponse,
message: _badResponseExceptionMessage(statusCode),
requestOptions: requestOptions,
response: response,
error: null,
message: _badResponseExceptionMessage(statusCode),
);

factory DioException.connectionTimeout({
Expand All @@ -99,14 +99,14 @@ class DioException implements Exception {
}) =>
DioException(
type: DioExceptionType.connectionTimeout,
requestOptions: requestOptions,
response: null,
error: error,
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,
);

factory DioException.sendTimeout({
Expand All @@ -115,14 +115,14 @@ class DioException implements Exception {
}) =>
DioException(
type: DioExceptionType.sendTimeout,
requestOptions: requestOptions,
response: null,
error: null,
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,
);

factory DioException.receiveTimeout({
Expand All @@ -132,14 +132,26 @@ class DioException implements Exception {
}) =>
DioException(
type: DioExceptionType.receiveTimeout,
requestOptions: requestOptions,
response: null,
error: error,
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.',
);

factory DioException.badCertificate({
required RequestOptions requestOptions,
Object? error,
}) =>
DioException(
type: DioExceptionType.badCertificate,
requestOptions: requestOptions,
response: null,
error: error,
message: 'The certificate of the response is not approved.',
);

factory DioException.requestCancelled({
Expand All @@ -149,11 +161,11 @@ class DioException implements Exception {
}) =>
DioException(
type: DioExceptionType.cancel,
message: 'The request was manually cancelled by the user.',
requestOptions: requestOptions,
response: null,
error: reason,
stackTrace: stackTrace,
message: 'The request was manually cancelled by the user.',
);

factory DioException.connectionError({
Expand Down
4 changes: 2 additions & 2 deletions dio/lib/src/interceptors/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ class LogInterceptor extends Interceptor {
/// Log printer; defaults print log to console.
/// In flutter, you'd better use debugPrint.
/// you can also write log in a file, for example:
///```dart
/// ```dart
/// final file=File("./log.txt");
/// final sink=file.openWrite();
/// dio.interceptors.add(LogInterceptor(logPrint: sink.writeln));
/// ...
/// await sink.close();
///```
/// ```
void Function(Object object) logPrint;

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class _ConnectionManager implements ConnectionManager {
uri.port,
);
if (!isCertApproved) {
// TODO(EVERYONE): Replace with DioException.badCertificate once upgrade dependencies Dio above 5.4.2.
throw DioException(
requestOptions: options,
type: DioExceptionType.badCertificate,
Expand Down
Loading