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

🥅 Throws precise StateError for handler's duplicated calls #2130

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Changes from 1 commit
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
30 changes: 26 additions & 4 deletions dio/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ abstract class _BaseHandler {
Future<InterceptorState> get future => _completer.future;

bool get isCompleted => _completer.isCompleted;

void _throwIfCompleted() {
if (_completer.isCompleted) {
throw StateError(
'The `handler` has already been called, '
'make sure each handler gets called only once.',
);
}
}
}

/// The handler for interceptors to handle before the request has been sent.
Expand All @@ -35,6 +44,7 @@ class RequestInterceptorHandler extends _BaseHandler {
/// Typically, the method should be called once interceptors done
/// manipulating the [requestOptions].
void next(RequestOptions requestOptions) {
_throwIfCompleted();
_completer.complete(InterceptorState<RequestOptions>(requestOptions));
_processNextInQueue?.call();
}
Expand All @@ -50,6 +60,7 @@ class RequestInterceptorHandler extends _BaseHandler {
Response response, [
bool callFollowingResponseInterceptor = false,
]) {
_throwIfCompleted();
_completer.complete(
InterceptorState<Response>(
response,
Expand All @@ -68,8 +79,11 @@ class RequestInterceptorHandler extends _BaseHandler {
/// unless [callFollowingErrorInterceptor] is true
/// which delivers [InterceptorResultType.rejectCallFollowing]
/// to the [InterceptorState].
void reject(DioException error,
[bool callFollowingErrorInterceptor = false]) {
void reject(
DioException error, [
bool callFollowingErrorInterceptor = false,
]) {
_throwIfCompleted();
_completer.completeError(
InterceptorState<DioException>(
error,
Expand All @@ -90,6 +104,7 @@ class ResponseInterceptorHandler extends _BaseHandler {
/// Typically, the method should be called once interceptors done
/// manipulating the [response].
void next(Response response) {
_throwIfCompleted();
_completer.complete(
InterceptorState<Response>(response),
);
Expand All @@ -98,6 +113,7 @@ class ResponseInterceptorHandler extends _BaseHandler {

/// Completes the request by resolves the [response] as the result.
void resolve(Response response) {
_throwIfCompleted();
_completer.complete(
InterceptorState<Response>(
response,
Expand All @@ -114,8 +130,11 @@ class ResponseInterceptorHandler extends _BaseHandler {
/// unless [callFollowingErrorInterceptor] is true
/// which delivers [InterceptorResultType.rejectCallFollowing]
/// to the [InterceptorState].
void reject(DioException error,
[bool callFollowingErrorInterceptor = false]) {
void reject(
DioException error, [
bool callFollowingErrorInterceptor = false,
]) {
_throwIfCompleted();
_completer.completeError(
InterceptorState<DioException>(
error,
Expand All @@ -136,6 +155,7 @@ class ErrorInterceptorHandler extends _BaseHandler {
/// Typically, the method should be called once interceptors done
/// manipulating the [error].
void next(DioException error) {
_throwIfCompleted();
_completer.completeError(
InterceptorState<DioException>(error),
error.stackTrace,
Expand All @@ -145,6 +165,7 @@ class ErrorInterceptorHandler extends _BaseHandler {

/// Completes the request by resolves the [response] as the result.
void resolve(Response response) {
_throwIfCompleted();
_completer.complete(
InterceptorState<Response>(
response,
Expand All @@ -156,6 +177,7 @@ class ErrorInterceptorHandler extends _BaseHandler {

/// Completes the request by reject with the [error] as the result.
void reject(DioException error) {
_throwIfCompleted();
_completer.completeError(
InterceptorState<DioException>(error, InterceptorResultType.reject),
error.stackTrace,
Expand Down
Loading