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

[建议] 增强 request 模块错误处理能力,以及期待暴露更多 error 相关信息 #111

Open
ly525 opened this issue Mar 1, 2020 · 0 comments

Comments

@ly525
Copy link
Contributor

ly525 commented Mar 1, 2020

场景:

在用 ams 做登录(主要是登录接口,其它接口也可能会遇到这个情况)

因为我们是通过状态码来判断账号密码是否正确的(比如 401),可能并不像 ams examples 通过 response.data.code === 0 来判断

然后我遇到了一个问题,就是在 response 处理的时候

actions: {
        async goLogin() {
            try {
                const res = await this.$ams.request({
                    url: `${this.resource.api.prefix}login`,
                    method: 'post',
                    data: {
                        ...this.data,
                        email: this.data.username
                    }
                });
                // 默认successCode
                if (res.data.code === 0) {
                    this.$message.success('登录成功');
                }
            } catch (error) {
                // 然后发现 error 里面其实只能拿到 401 字符串,服务器端返回的其它message 拿不到
                console.log(error, error.status, 'error');
                // eslint-disable-next-line no-debugger
                debugger;
            }
        }
    }

image

image

建议方案

我看了下 ams的request 代码里面只返回了 status code 相关信息:

reject(new Error(xhr.status));

image

我个人觉得这里可以是否可以参照 axios 做一下改进呢(对外返回尽可能多的 response 以及 response error 相关信息):

  1. axios xhr:https://github.com/axios/axios/blob/master/lib/adapters/xhr.js#L61
  2. axios xhr reject:https://github.com/axios/axios/blob/521444513969a08ec5ef943c41ba0812845ed4f9/lib/core/settle.js#L12
  3. axios error:https://github.com/axios/axios/blob/521444513969a08ec5ef943c41ba0812845ed4f9/lib/core/settle.js#L12

简单说就是只处理了 200相关情况, 略微有些简单了 [Facepalm]

如果后端用了标准 Resetful-API 的话,就会有些尴尬(比如 create返回的状态码 是 201 ,而非 200)

期待早日修复😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant