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

Rework exceptions and stacktraces #1963

Draft
wants to merge 1 commit into
base: 6.0.0
Choose a base branch
from
Draft
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
3 changes: 1 addition & 2 deletions dio/lib/src/adapters/browser_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
requestOptions: options,
timeout: connectionTimeout,
),
StackTrace.current,
);
},
);
Expand Down Expand Up @@ -224,7 +223,7 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
completer.completeError(
DioException.requestCancelled(
requestOptions: options,
reason: 'The XMLHttpRequest was aborted.',
cause: 'The XMLHttpRequest was aborted.',
),
);
}
Expand Down
6 changes: 3 additions & 3 deletions dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ class IOHttpClientAdapter implements HttpClientAdapter {
timeout: options.connectTimeout ??
httpClient.connectionTimeout ??
Duration.zero,
error: e,
cause: e,
);
}
throw DioException.connectionError(
requestOptions: options,
reason: e.message,
error: e,
cause: e,
);
}

Expand Down Expand Up @@ -169,7 +169,7 @@ class IOHttpClientAdapter implements HttpClientAdapter {
throw DioException(
requestOptions: options,
type: DioExceptionType.badCertificate,
error: responseStream.certificate,
cause: responseStream.certificate,
message: 'The certificate of the response is not approved.',
);
}
Expand Down
4 changes: 2 additions & 2 deletions dio/lib/src/cancel_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class CancelToken {
void cancel([Object? reason]) {
_cancelError = DioException.requestCancelled(
requestOptions: requestOptions ?? RequestOptions(),
reason: reason,
stackTrace: StackTrace.current,
cause: reason,
causeStackTrace: StackTrace.current,
);
if (!_completer.isCompleted) {
_completer.complete(_cancelError);
Expand Down
10 changes: 5 additions & 5 deletions dio/lib/src/dio/dio_for_native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class DioForNative with DioMixin implements Dio {
}
} finally {
completer.completeError(
DioMixin.assureDioException(e, response.requestOptions),
assureDioException(e, response.requestOptions),
);
}
});
Expand All @@ -160,7 +160,7 @@ class DioForNative with DioMixin implements Dio {
completer.complete(response);
} catch (e) {
completer.completeError(
DioMixin.assureDioException(e, response.requestOptions),
assureDioException(e, response.requestOptions),
);
}
},
Expand All @@ -169,7 +169,7 @@ class DioForNative with DioMixin implements Dio {
await closeAndDelete();
} finally {
completer.completeError(
DioMixin.assureDioException(e, response.requestOptions),
assureDioException(e, response.requestOptions),
);
}
},
Expand All @@ -190,10 +190,10 @@ class DioForNative with DioMixin implements Dio {
throw DioException.receiveTimeout(
timeout: timeout,
requestOptions: response.requestOptions,
error: e,
cause: e,
);
} else {
throw e;
throw assureDioException(e, response.requestOptions);
}
},
);
Expand Down
55 changes: 27 additions & 28 deletions dio/lib/src/dio_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum DioExceptionType {
connectionError,

/// Default error type, Some other [Error]. In this case, you can use the
/// [DioException.error] if it is not null.
/// [DioException.cause] if it is not null.
unknown,
}

Expand Down Expand Up @@ -62,14 +62,10 @@ class DioException implements Exception {
required this.requestOptions,
this.response,
this.type = DioExceptionType.unknown,
this.error,
StackTrace? stackTrace,
this.cause,
StackTrace? causeStackTrace,
this.message,
}) : stackTrace = identical(stackTrace, StackTrace.empty)
? requestOptions.sourceStackTrace ?? StackTrace.current
: stackTrace ??
requestOptions.sourceStackTrace ??
StackTrace.current;
}) : _causeStackTrace = causeStackTrace;

factory DioException.badResponse({
required int statusCode,
Expand All @@ -82,21 +78,21 @@ class DioException implements Exception {
'invalid status code of $statusCode.',
requestOptions: requestOptions,
response: response,
error: null,
cause: null,
);

factory DioException.connectionTimeout({
required Duration timeout,
required RequestOptions requestOptions,
Object? error,
Object? cause,
}) =>
DioException(
type: DioExceptionType.connectionTimeout,
message: 'The request connection took '
'longer than $timeout. It was aborted.',
requestOptions: requestOptions,
response: null,
error: error,
cause: cause,
);

factory DioException.sendTimeout({
Expand All @@ -109,48 +105,48 @@ class DioException implements Exception {
'longer than $timeout to send data. It was aborted.',
requestOptions: requestOptions,
response: null,
error: null,
cause: null,
);

factory DioException.receiveTimeout({
required Duration timeout,
required RequestOptions requestOptions,
Object? error,
Object? cause,
}) =>
DioException(
type: DioExceptionType.receiveTimeout,
message: 'The request took '
'longer than $timeout to receive data. It was aborted.',
requestOptions: requestOptions,
response: null,
error: error,
cause: cause,
);

factory DioException.requestCancelled({
required RequestOptions requestOptions,
required Object? reason,
StackTrace? stackTrace,
required Object? cause,
StackTrace? causeStackTrace,
}) =>
DioException(
type: DioExceptionType.cancel,
message: 'The request was cancelled.',
requestOptions: requestOptions,
response: null,
error: reason,
stackTrace: stackTrace,
cause: cause,
causeStackTrace: causeStackTrace,
);

factory DioException.connectionError({
required RequestOptions requestOptions,
required String reason,
Object? error,
Object? cause,
}) =>
DioException(
type: DioExceptionType.connectionError,
message: 'The connection errored: $reason',
requestOptions: requestOptions,
response: null,
error: error,
cause: cause,
);

/// The request info for the request that throws exception.
Expand All @@ -164,11 +160,14 @@ class DioException implements Exception {

/// The original error/exception object;
/// It's usually not null when `type` is [DioExceptionType.unknown].
final Object? error;
final Object? cause;

/// The stacktrace of the original error/exception object;
/// It's usually not null when `type` is [DioExceptionType.unknown].
final StackTrace stackTrace;
final StackTrace? _causeStackTrace;

StackTrace? get causeStackTrace =>
_causeStackTrace ?? (cause is Error ? (cause as Error).stackTrace : null);

/// The error message that throws a [DioException].
final String? message;
Expand All @@ -178,25 +177,25 @@ class DioException implements Exception {
RequestOptions? requestOptions,
Response? response,
DioExceptionType? type,
Object? error,
StackTrace? stackTrace,
Object? cause,
StackTrace? causeStackTrace,
String? message,
}) {
return DioException(
requestOptions: requestOptions ?? this.requestOptions,
response: response ?? this.response,
type: type ?? this.type,
error: error ?? this.error,
stackTrace: stackTrace ?? this.stackTrace,
cause: cause ?? this.cause,
causeStackTrace: causeStackTrace ?? this.causeStackTrace,
message: message ?? this.message,
);
}

@override
String toString() {
String msg = 'DioException [${type.toPrettyDescription()}]: $message';
if (error != null) {
msg += '\nError: $error';
if (cause != null) {
msg += '\nError: $cause';
}
return msg;
}
Expand Down
Loading
Loading