Skip to content

Commit

Permalink
Add constructor for DioExceptionType.badCertificate (cfug#2174)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgraceb committed Apr 19, 2024
1 parent 6a77d56 commit 5e724de
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 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 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
1 change: 1 addition & 0 deletions plugins/http2_adapter/lib/src/connection_manager_imp.dart
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

0 comments on commit 5e724de

Please sign in to comment.