handleError static method

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

统一异常处理

Implementation

static void handleError(Exception error,
    {Function(String?, String?, Exception ex)? onFail}) {
  String? code = "-1";
  String? msg = "Exception";
  if (error is DioError) {
    // dio 异常
    switch (error.type) {
      case DioErrorType.connectTimeout:
        msg = S.current.root_error02;
        break;
      case DioErrorType.receiveTimeout:
        msg = S.current.root_error02;
        break;
      case DioErrorType.sendTimeout:
        msg = S.current.root_error02;
        break;
      case DioErrorType.cancel:
        msg = "Request Cancel";
        break;
      case DioErrorType.response:
        //状态码异常
        var statusCode = error.response?.statusCode;
        if (statusCode != null) {
          msg = "error:[$statusCode]";
          code = statusCode.toString();
        }
        break;
      case DioErrorType.other:
        if (error.error is SocketException) {
          msg = S.current.root_error01;
        }
        break;
    }
  } else if (error is BizException) {
    msg = error.message;
    code = error.code;
  } else {
    //其他异常,例如json解析异常等
    msg = error.toString();
  }
  HttpPlatform.get.throwHandler
      ?.handleError(msg, code, error, onFail: onFail);
}