onError method
The callback will be executed on error.
If you want to continue the error , call handler.next.
If you want to complete the response with some custom data directly,
you can resolve a Response object with handler.resolve and other
error interceptor(s) will be skipped.
If you want to complete the response with an error message directly,
you can reject a DioException object with handler.reject, and other
error interceptor(s) will be skipped.
Implementation
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
// error统一处理
switch (err.type) {
case DioExceptionType.cancel:
{
debugPrint("请求取消");
}
case DioExceptionType.connectionTimeout:
{
debugPrint("连接超时");
}
case DioExceptionType.sendTimeout:
{
debugPrint("请求超时");
}
case DioExceptionType.receiveTimeout:
{
debugPrint("响应超时");
}
case DioExceptionType.badResponse:
try {
int? errCode = err.response?.statusCode;
// String errMsg = err.response.statusMessage;
// ErrorEntity(code: errCode, message: errMsg);
switch (errCode) {
case 400:
{
debugPrint("请求语法错误");
}
case 401:
{
debugPrint("没有权限");
}
case 403:
{
debugPrint("服务器拒绝执行");
}
case 404:
{
debugPrint("请求地址不存在");
}
case 405:
{
debugPrint("请求方法被禁止");
}
case 500:
{
debugPrint("服务器内部错误");
}
case 502:
{
debugPrint("无效的请求");
}
case 503:
{
debugPrint("服务器挂了");
}
case 505:
{
debugPrint("不支持HTTP协议请求");
}
default:
{
// ErrorEntity(code: errCode, message: "未知错误");
debugPrint(err.response?.statusMessage);
}
}
} on Exception catch (_) {
debugPrint("未知错误");
}
default:
{
debugPrint(err.message);
}
}
// 错误提示
debugPrint('DioError===: ${err.toString()}');
// err.err = appException;
super.onError(err, handler);
}