HarmonyOS (react-native)promise.reject抛出异常对象只能接收到message参数

turboModule.ts使用promise.reject抛出异常对象。

checkDeviceSupportLocalAuthenticationWithBiometricsOnly(bool: boolean): Promise<{
  code?: number | undefined;
  errorCode?: string | undefined;
  note?: string | undefined
}> {
  try {
    userAuth.getAvailableStatus(userAuth.UserAuthType.FINGERPRINT, userAuth.AuthTrustLevel.ATL2);
    return Promise.resolve({})
  } catch (error) {
    try {
      userAuth.getAvailableStatus(userAuth.UserAuthType.FACE, userAuth.AuthTrustLevel.ATL2);
      return Promise.resolve({})
    } catch (error) {
      // 如果仅支持生物方式,则不考虑密码
      if (bool) {
        return Promise.reject({code: 0, message: '该设备暂不支持生物认证'})
      }else {
        try {
          userAuth.getAvailableStatus(userAuth.UserAuthType.PIN, userAuth.AuthTrustLevel.ATL2);
          return Promise.resolve({})
        }catch (error) {
          return Promise.reject({code: 0, message: '该设备暂不支持生物认证'})
        }
      }
    }
  }
}

在rn catch接收参数时只能拿到message参数:

static checkDeviceSupportLocalAuthenticationWithBiometricsOnly = (biometricsOnly: boolean, callback: (obj: {
  code?: number,
  errorCode?: string,
  note?: string
}) => void) => {
  RNLocalAuthentication?.checkDeviceSupportLocalAuthenticationWithBiometricsOnly(biometricsOnly)
    .then(() => {
      callback({ code: 1 })
    }).catch(({ code, message }) => {
    console.log(`====>>>>指纹认证失败,code:${code},message:${message}`)
    callback({ code, note: message })
  });
}
HarmonyOS
2024-12-25 09:55:26
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

这个问题是RN内部的规范,当前RN版本规定只能返回字符串,或者返回的对象中包含“message: string”字段。可以通过以下方法进行规避:在原生侧将结果对象转换为JSON字符串返回,JS侧再通过解析JSON,将对象解析出来。

分享
微博
QQ
微信
回复
2024-12-25 12:37:22
相关问题
HarmonyOS react-native应用编译失败
183浏览 • 1回复 待解决
HarmonyOS react-native集成问题
128浏览 • 1回复 待解决
HarmonyOS react-native如何自行打包
429浏览 • 1回复 待解决
HarmonyOS 使用react-native中的Image问题
142浏览 • 1回复 待解决
鸿蒙生态中有没有react-native适配?
13545浏览 • 1回复 待解决
HarmonyOS RN reject异常问题
86浏览 • 1回复 待解决
react-native 第三方库使用npm更新
1626浏览 • 1回复 待解决
HarmonyOS 适配 React native
650浏览 • 1回复 待解决
HarmonyOS 捕获异常抛出为什么报错
242浏览 • 1回复 待解决