参考: UmiJS request
当和后端通讯时,通常我们需要一个全局的错误处理:
- 403:没有权限,跳到登录页面。
- 404:资源不存在的错误。
- 其它:弹出 notification dialog,显示错误。
无法 overwrite 全局错误处理
当提供了 catch 语句时,通常就不需要 global error handler。如果能像面向对象那样,重写父类的方法,就很方便了。
可是基于 promise 是做不到的,后面写的 catch 是最后执行的,没法 stop 全局的错误处理。Example:
const myrequest = (url: string) => { const p = new Promise((resolve, reject) => { // resolve(1); reject("you are wrong: " + url); }).catch((reason) => { console.log("Global error handler: " + reason); throw reason; }); return p; } myrequest("localhost:8000") .catch((reason) => { console.log("Outer error handler:" + reason); });
UmiJS 提供了 skipErrorHandler 属性
参考:运行时配置示例
getData 是使用 @umijs/openapi 自动生成的。
getData(params, {skipErrorHandler: true}) .then((e) => { // handle error });