Skip to content

Commit

Permalink
Improve default parameter naming for Interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
hgraceb committed Nov 27, 2023
1 parent a7a7878 commit 2f2f5dd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 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 err, ErrorInterceptorHandler handler) {
// 如果你想完成请求并返回一些自定义数据,你可以使用 `handler.resolve(response)`。
return handler.next(e);
return handler.next(err);
},
),
);
Expand Down
4 changes: 2 additions & 2 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 err, 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(err);
},
),
);
Expand Down
4 changes: 2 additions & 2 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 err,
ErrorInterceptorHandler handler,
);

Expand Down
30 changes: 15 additions & 15 deletions dio/test/interceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -421,34 +421,34 @@ 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 err, ErrorInterceptorHandler handler) {
if (err.response?.requestOptions != null) {
switch (err.response!.requestOptions.path) {
case urlNotFound:
return handler.next(e);
return handler.next(err);
case urlNotFound1:
return handler.resolve(
Response(
requestOptions: e.requestOptions,
requestOptions: err.requestOptions,
data: 'fake data',
),
);
case urlNotFound2:
return handler.resolve(
Response(
data: 'fake data',
requestOptions: e.requestOptions,
requestOptions: err.requestOptions,
),
);
case urlNotFound3:
return handler.next(
e.copyWith(
error: 'custom error info [${e.response!.statusCode}]',
err.copyWith(
error: 'custom error info [${err.response!.statusCode}]',
),
);
}
}
handler.next(e);
handler.next(err);
},
),
);
Expand Down Expand Up @@ -514,17 +514,17 @@ void main() {
..options.baseUrl = MockAdapter.mockBase
..interceptors.add(
InterceptorsWrapper(
onError: (DioException e, ErrorInterceptorHandler handler) {
iError = e;
handler.next(e);
onError: (DioException err, ErrorInterceptorHandler handler) {
iError = err;
handler.next(err);
},
),
)
..interceptors.add(
QueuedInterceptorsWrapper(
onError: (DioException e, ErrorInterceptorHandler handler) {
qError = e;
handler.next(e);
onError: (DioException err, ErrorInterceptorHandler handler) {
qError = err;
handler.next(err);
},
),
);
Expand Down
18 changes: 9 additions & 9 deletions example/lib/response_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@ 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 err, handler) {
if (err.response != null) {
switch (err.response!.requestOptions.path) {
case urlNotFound:
return handler.next(e);
return handler.next(err);
case urlNotFound1:
handler.resolve(
Response(
requestOptions: e.requestOptions,
requestOptions: err.requestOptions,
data: 'fake data',
),
);
break;
case urlNotFound2:
handler.resolve(
Response(
requestOptions: e.requestOptions,
requestOptions: err.requestOptions,
data: 'fake data',
),
);
break;
case urlNotFound3:
handler.next(
e.copyWith(
error: 'custom error info [${e.response!.statusCode}]',
err.copyWith(
error: 'custom error info [${err.response!.statusCode}]',
),
);
break;
}
} else {
handler.next(e);
handler.next(err);
}
},
),
Expand Down

0 comments on commit 2f2f5dd

Please sign in to comment.