handleError method

  1. @override
void handleError(
  1. Exception ex, {
  2. dynamic onFail(
    1. String?,
    2. String?,
    3. Exception
    )?,
})
override

统一异常处理

Implementation

@override
void handleError(Exception ex,
    {Function(String?, String?, Exception)? onFail}) {
  String? msg;
  String? errorCode;
  if (ex is DioException) {
    // dio 异常
    switch (ex.type) {
      case DioExceptionType.connectionTimeout:
        msg = "网络连接超时";
        break;
      case DioExceptionType.receiveTimeout:
        msg = "网络接收超时";
        break;
      case DioExceptionType.sendTimeout:
        msg = "网络发送超时";
        break;
      case DioExceptionType.cancel:
        msg = "请求已取消";
        break;
      case DioExceptionType.badResponse:
        //状态码异常
        var statusCode = ex.response?.statusCode;
        if (statusCode != null) {
          msg = ex.message;
          errorCode = statusCode.toString();
        } else {
          msg = ex.message;
        }
        break;
      case DioExceptionType.unknown:
        if (ex.error is SocketException) {
          msg = "网络连接异常";
        } else {
          msg = ex.message;
        }
        break;
      case DioExceptionType.badCertificate:
        msg = "证书错误";
        break;
      case DioExceptionType.connectionError:
        msg = "网络连接异常";
        break;
    }
  } else if (ex is BizException) {
    msg = ex.message;
    errorCode = ex.code;
  } else {
    msg = ex.toString();
  }
  if (onFail != null) {
    onFail.call(errorCode, msg, ex);
  } else {
    dealError(errorCode, msg, ex);
  }
}