We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
配置axios拦截器,每次req发送网络请求后可以在拦截器中自动捕获并格式化处理网络错误
req
将网络错误重新封装为
{code: xxx, err: xxx}
的形式并返回,其中code是网络错误码40x或50x,err是原本抛出的异常或错误信息
code
err
see https://www.jianshu.com/p/a0c67f4e145e
The text was updated successfully, but these errors were encountered:
使用axios.creator创建实例instance,为其添加了回复拦截器,用于封装错误代码和错误信息。而后将instance放入原本的req函数中,req函数的接口形式不变。
当Promise被resolve时,将正常返回response对象(为promise,需要.then执行) 当Promise被rejected时,将返回Promise.rejected对象,其value为{error: 错误代码, response: 错误回复本体}(可用.catch捕捉)
400: 客户端请求有语法错误 401: 请求未经授权 404: 页面未找到 403: Bad Gateway 500: Server Error -1: 不常见错误
正常访问:能够通过Jsonplaceholder网站的请求接口。 错误访问:访问本地的未知页面,能够按约定返回rejcted对象。 测试问题:受限CORS,向不存在的网页发请请求时,会先调用preflight进行OPTION查询,在本体运行前已经被预先拦截了,尝试解决未果。
close #21
Sorry, something went wrong.
sinoyou
Successfully merging a pull request may close this issue.
配置axios拦截器,每次
req
发送网络请求后可以在拦截器中自动捕获并格式化处理网络错误将网络错误重新封装为
的形式并返回,其中
code
是网络错误码40x或50x,err
是原本抛出的异常或错误信息see https://www.jianshu.com/p/a0c67f4e145e
The text was updated successfully, but these errors were encountered: