Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

判断接口返回错误,判断的是http状态码, #82

Closed
wants to merge 1 commit into from
Closed

判断接口返回错误,判断的是http状态码, #82

wants to merge 1 commit into from

Conversation

AP0rPi7uQ9
Copy link

判断接口返回错误,判断的是http状态码,
判断接口返回正常,现在判断的是data中的code,
难道不应该保持一致吗?

判断接口返回正常,现在用的是data中的code,
难道不应该保持一致吗?
@AP0rPi7uQ9
Copy link
Author

正常应该也应该判断http状态码的吧?

要不然只有错误可以自定义code,接口正常的自定义code就不行了。

少量自定义code还可以在config中设置一下,如果大量自定义code,总不能一个code一个code的往config里面加把。

建议还是设置那些http状态码是正确的,比如设置200,201,202,这样code可以自定义,哪怕大量的自定义code也没关系,只要返回的http状态码在前端的config文件中出现即可。

@FlowPeakFish
Copy link
Contributor

个人觉得逻辑没有问题

http状态码<300的,都会走onFulfilled
现在更多的是业务层级错误处理,假定restful有一套统一输出格式(指不管成功与否都有相应的数据格式)情况下进行处理

    const { code, msg } = data;
    // 操作正常Code数组
    const codeVerificationArray = isArray(successCode)
      ? [...successCode]
      : [...[successCode]];
    // 是否操作正常
    if (codeVerificationArray.includes(code)) {
      return data;
    } else {
      handleCode(code, msg);
      return Promise.reject(
        "vue-admin-beautiful请求异常拦截:" +
          JSON.stringify({ url: config.url, code, msg }) || "Error"
      );
    }

所以会使用data中的code,由后端来提供该请求是否顺利,非successCode之外的,根据msg提示错误消息
如果在请求成功的情况下也需要消息提示,可以将msg的消息通过全局Message发出提示

当http>500、服务器宕机、网络问题等意料之外之外的请求时,才会走onRejected
先通过status判断是否服务器问题
如果服务器无回应,再根据错误消息给出错误提示

@zxwk1998 zxwk1998 closed this Aug 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants