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

Improve default parameter naming for Interceptor #2047

Merged
merged 4 commits into from
Nov 29, 2023
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
10 changes: 5 additions & 5 deletions dio/README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ dio.interceptors.add(
// 如果你想终止请求并触发一个错误,你可以使用 `handler.reject(error)`。
return handler.next(response);
},
onError: (DioException e, ErrorInterceptorHandler handler) {
onError: (DioException error, ErrorInterceptorHandler handler) {
// 如果你想完成请求并返回一些自定义数据,你可以使用 `handler.resolve(response)`。
return handler.next(e);
return handler.next(error);
},
),
);
Expand Down Expand Up @@ -886,9 +886,9 @@ void initAdapter() {

```dart
final cancelToken = CancelToken();
dio.get(url, cancelToken: cancelToken).catchError((DioException err) {
if (CancelToken.isCancel(err)) {
print('Request canceled: ${err.message}');
dio.get(url, cancelToken: cancelToken).catchError((DioException error) {
if (CancelToken.isCancel(error)) {
print('Request canceled: ${error.message}');
} else {
// handle error.
}
Expand Down
10 changes: 5 additions & 5 deletions dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,11 @@ dio.interceptors.add(
// you can reject a `DioException` object using `handler.reject(dioError)`.
return handler.next(response);
},
onError: (DioException e, ErrorInterceptorHandler handler) {
onError: (DioException error, ErrorInterceptorHandler handler) {
// Do something with response error.
// If you want to resolve the request with some custom data,
// you can resolve a `Response` object using `handler.resolve(response)`.
return handler.next(e);
return handler.next(error);
},
),
);
Expand Down Expand Up @@ -913,9 +913,9 @@ When a token's `cancel()` is invoked, all requests with this token will be cance

```dart
final cancelToken = CancelToken();
dio.get(url, cancelToken: cancelToken).catchError((DioException err) {
if (CancelToken.isCancel(err)) {
print('Request canceled: ${err.message}');
dio.get(url, cancelToken: cancelToken).catchError((DioException error) {
if (CancelToken.isCancel(error)) {
print('Request canceled: ${error.message}');
} else {
// handle error.
}
Expand Down
4 changes: 2 additions & 2 deletions dio/doc/migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Before:

```dart
Never throwDioError() {
final error = DioError(request: requestOptions, error: err);
final error = DioError(request: requestOptions, error: e);
error.message = 'Custom message.';
error.stackTrace = StackTrace.current;
throw error;
Expand All @@ -113,7 +113,7 @@ After:
Never throwDioError() {
DioError error = DioError(
request: requestOptions,
error: err,
error: e,
stackTrace: StackTrace.current
);
error = error.copyWith(message: 'Custom message.');
Expand Down
18 changes: 9 additions & 9 deletions dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ abstract class DioMixin implements Dio {
FutureOr<dynamic> Function(Object) errorInterceptorWrapper(
InterceptorErrorCallback interceptor,
) {
return (err) {
final state = err is InterceptorState
? err
: InterceptorState(assureDioException(err, requestOptions));
return (error) {
final state = error is InterceptorState
? error
: InterceptorState(assureDioException(error, requestOptions));
Future<InterceptorState> handleError() async {
final errorHandler = ErrorInterceptorHandler();
interceptor(state.data, errorHandler);
Expand All @@ -442,7 +442,7 @@ abstract class DioMixin implements Dio {
Future(handleError),
);
} else {
throw err;
throw error;
}
};
}
Expand Down Expand Up @@ -690,15 +690,15 @@ abstract class DioMixin implements Dio {

@internal
static DioException assureDioException(
Object err,
Object error,
RequestOptions requestOptions,
) {
if (err is DioException) {
return err;
if (error is DioException) {
return error;
}
return DioException(
requestOptions: requestOptions,
error: err,
error: error,
);
}

Expand Down
8 changes: 4 additions & 4 deletions dio/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ typedef InterceptorSendCallback = void Function(

/// The signature of [Interceptor.onResponse].
typedef InterceptorSuccessCallback = void Function(
Response<dynamic> e,
Response<dynamic> response,
ResponseInterceptorHandler handler,
);

/// The signature of [Interceptor.onError].
typedef InterceptorErrorCallback = void Function(
DioException e,
DioException error,
ErrorInterceptorHandler handler,
);

Expand Down Expand Up @@ -371,10 +371,10 @@ class QueuedInterceptor extends Interceptor {
}

void _handleError(
DioException err,
DioException error,
ErrorInterceptorHandler handler,
) {
_handleQueue(_errorQueue, err, handler, onError);
_handleQueue(_errorQueue, error, handler, onError);
}

void _handleQueue<T, V extends _BaseHandler>(
Expand Down
65 changes: 33 additions & 32 deletions dio/test/interceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,24 @@ void main() {
handler.next(response); //continue
}
},
onError: (err, handler) {
if (err.requestOptions.path == '/reject-next-response') {
onError: (error, handler) {
if (error.requestOptions.path == '/reject-next-response') {
handler.resolve(
Response(
requestOptions: err.requestOptions,
requestOptions: error.requestOptions,
data: 100,
),
);
} else if (err.requestOptions.path ==
} else if (error.requestOptions.path ==
'/resolve-next/reject-next') {
handler.next(err.copyWith(error: 1));
handler.next(error.copyWith(error: 1));
} else {
if (err.requestOptions.path == '/reject-next/reject') {
handler.reject(err);
if (error.requestOptions.path == '/reject-next/reject') {
handler.reject(error);
} else {
int count = err.error as int;
int count = error.error as int;
count++;
handler.next(err.copyWith(error: count));
handler.next(error.copyWith(error: count));
}
}
},
Expand All @@ -148,15 +148,15 @@ void main() {
handler.next(response); //continue
}
},
onError: (err, handler) {
if (err.requestOptions.path == '/resolve-next/reject-next') {
int count = err.error as int;
onError: (error, handler) {
if (error.requestOptions.path == '/resolve-next/reject-next') {
int count = error.error as int;
count++;
handler.next(err.copyWith(error: count));
handler.next(error.copyWith(error: count));
} else {
int count = err.error as int;
int count = error.error as int;
count++;
handler.next(err.copyWith(error: count));
handler.next(error.copyWith(error: count));
}
},
),
Expand Down Expand Up @@ -218,8 +218,8 @@ void main() {
}
handler.next(reqOpt.copyWith(path: '/xxx'));
},
onError: (err, handler) {
handler.next(err.copyWith(error: 'unexpected error'));
onError: (error, handler) {
handler.next(error.copyWith(error: 'unexpected error'));
},
),
);
Expand Down Expand Up @@ -421,34 +421,35 @@ void main() {
response.data = response.data['data'];
handler.next(response);
},
onError: (DioException e, ErrorInterceptorHandler handler) {
if (e.response?.requestOptions != null) {
switch (e.response!.requestOptions.path) {
onError: (DioException error, ErrorInterceptorHandler handler) {
final response = error.response;
if (response != null) {
switch (response.requestOptions.path) {
case urlNotFound:
return handler.next(e);
return handler.next(error);
case urlNotFound1:
return handler.resolve(
Response(
requestOptions: e.requestOptions,
requestOptions: error.requestOptions,
data: 'fake data',
),
);
case urlNotFound2:
return handler.resolve(
Response(
data: 'fake data',
requestOptions: e.requestOptions,
requestOptions: error.requestOptions,
),
);
case urlNotFound3:
return handler.next(
e.copyWith(
error: 'custom error info [${e.response!.statusCode}]',
error.copyWith(
error: 'custom error info [${response.statusCode}]',
),
);
}
}
handler.next(e);
handler.next(error);
},
),
);
Expand Down Expand Up @@ -514,17 +515,17 @@ void main() {
..options.baseUrl = MockAdapter.mockBase
..interceptors.add(
InterceptorsWrapper(
onError: (DioException e, ErrorInterceptorHandler handler) {
iError = e;
handler.next(e);
onError: (DioException error, ErrorInterceptorHandler handler) {
iError = error;
handler.next(error);
},
),
)
..interceptors.add(
QueuedInterceptorsWrapper(
onError: (DioException e, ErrorInterceptorHandler handler) {
qError = e;
handler.next(e);
onError: (DioException error, ErrorInterceptorHandler handler) {
qError = error;
handler.next(error);
},
),
);
Expand Down
12 changes: 6 additions & 6 deletions dio/test/stacktrace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ void main() async {
StackTrace? caughtStackTrace;
dio.interceptors.addAll([
InterceptorsWrapper(
onError: (err, handler) {
caughtStackTrace = err.stackTrace;
handler.next(err);
onError: (error, handler) {
caughtStackTrace = error.stackTrace;
handler.next(error);
},
),
InterceptorsWrapper(
Expand Down Expand Up @@ -341,9 +341,9 @@ void main() async {
StackTrace? caughtStackTrace;
dio.interceptors.addAll([
QueuedInterceptorsWrapper(
onError: (err, handler) {
caughtStackTrace = err.stackTrace;
handler.next(err);
onError: (error, handler) {
caughtStackTrace = error.stackTrace;
handler.next(error);
},
),
QueuedInterceptorsWrapper(
Expand Down
29 changes: 13 additions & 16 deletions example/lib/response_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,35 @@ void main() async {
response.data = json.decode(response.data['data']);
handler.next(response);
},
onError: (DioException e, handler) {
if (e.response != null) {
switch (e.response!.requestOptions.path) {
onError: (DioException error, ErrorInterceptorHandler handler) {
final response = error.response;
if (response != null) {
switch (response.requestOptions.path) {
case urlNotFound:
return handler.next(e);
return handler.next(error);
case urlNotFound1:
handler.resolve(
return handler.resolve(
Response(
requestOptions: e.requestOptions,
requestOptions: error.requestOptions,
data: 'fake data',
),
);
break;
case urlNotFound2:
handler.resolve(
return handler.resolve(
Response(
requestOptions: e.requestOptions,
requestOptions: error.requestOptions,
data: 'fake data',
),
);
break;
case urlNotFound3:
handler.next(
e.copyWith(
error: 'custom error info [${e.response!.statusCode}]',
return handler.next(
error.copyWith(
error: 'custom error info [${response.statusCode}]',
),
);
break;
}
} else {
handler.next(e);
}
handler.next(error);
},
),
);
Expand Down
8 changes: 4 additions & 4 deletions plugins/cookie_manager/lib/src/cookie_mgr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@ class CookieManager extends Interceptor {
newCookies.isNotEmpty ? newCookies : null;
handler.next(options);
}).catchError((dynamic e, StackTrace s) {
final err = DioException(
final error = DioException(
requestOptions: options,
error: e,
stackTrace: s,
);
handler.reject(err, true);
handler.reject(error, true);
});
}

@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
_saveCookies(response).then((_) => handler.next(response)).catchError(
(dynamic e, StackTrace s) {
final err = DioException(
final error = DioException(
requestOptions: response.requestOptions,
error: e,
stackTrace: s,
);
handler.reject(err, true);
handler.reject(error, true);
},
);
}
Expand Down