ant-design / ant-design-pro

👨🏻‍💻👩🏻‍💻 Use Ant Design like a Pro!
https://pro.ant.design
MIT License
36.4k stars 8.14k forks source link

Unhandled Rejection (TypeError): Cannot read properties of null (reading 'success') #11245

Open cafeBabe1 opened 4 months ago

cafeBabe1 commented 4 months ago

欢迎页面-Ant-Design-Pro

是request.ts中的问题(如下)

// 中间件统一错误处理
  // 后端返回格式 { success: boolean, data: any }
  // 按照项目具体情况修改该部分逻辑
  requestMethodInstance.use(async (ctx, next) => {
    await next();
    const { req, res } = ctx;
    // @ts-ignore
    if (req.options?.skipErrorHandler) {
      return;
    }
    const { options } = req;
    const { getResponse } = options;
    const resData = getResponse ? res.data : res;
    const errorInfo = errorAdaptor(resData, ctx);
    if (errorInfo.success === false) {
      // 抛出错误到 errorHandler 中处理
      const error: RequestError = new Error(errorInfo.errorMessage);
      error.name = 'BizError';
      error.data = resData;
      error.info = errorInfo;
      error.request = req;
      error.response = res;
      throw error;
    }
  });

这段代码怎么让其不生效呢?

Fettes commented 4 months ago

凑巧看到,给点拙见。 看起来是接口没返回才会遇到的问题,提前判断下?

const { options } = req;
  const { getResponse } = options;
  const resData = getResponse ? res.data : res;

  // 在访问之前先检查 resData 是否为 null 或 undefined
  if (resData == null) {
    throw new Error('Response data is null or undefined');
  }

  const errorInfo = errorAdaptor(resData, ctx);