q***@163.com
q***@163.com
  • 发布:2022-11-14 20:29
  • 更新:2022-11-15 12:02
  • 阅读:272

云对象_before中返回错误使用return和throw的区别?

分类:uniCloud
async _before() {  
    // 通用预处理器  
    const methodName = this.getMethodName()  
    if (!this.getUniIdToken()) {  
      throw new Error('token不存在')  
    }  
    // token信息  
    const token = this.getUniIdToken()  
    const clientInfo = this.getClientInfo()  

    const tokenRes = await uniID  
      .createInstance({  
        clientInfo,  
      })  
      .checkToken(token)  

    if (tokenRes.errCode && tokenRes.errMsg) {  

      // throw tokenRes  
      throw {  
        errCode: 'hehe',  
        errMsg: 'lala',  
      }  
    }  
    let uid  
    if (tokenRes.uid) {  
      uid = tokenRes.uid  
    }  
    this.uid = uid  
  }

_before预处理的代码中我使用throw抛出错误,但是前端收到的是{"code":"FUNCTION_EXCUTE_ERROR","message":"请求云函数出错"},这是为什么?

我如果不使用throw,使用return去返回,这样更不行,直接往下走了。

2022-11-14 20:29 负责人:无 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

before内抛出的错误也可以在after内再加工进行return,参考uni-id-co的写法:uni-id-co

要回复问题请先登录注册