HarmonyOS如何捕获UnhandledPromiseRejection

HarmonyOS中经常用到Promise方法,但其发生异常时,也可能导致后续代码无法执行。因此如何快速定位问题就是影响业务稳定发展的重要问题。但在现有文档中,未找到能够捕获UnhandledPromiseRejection异常的方法?

虽然errorManager有提供onUnhandledException方法,但经过测试,不能捕获promise未处理的异常。示例异常代码如下:

export function testThrowsInPromise (): Promise<boolean> { 
  const Tag = "【testThrowsInAsync】" ; 
  console.log(Tag+ "2、含有throw并返回Promise的方法,且没有catch"); 
  return new Promise((resolve, reject) => { 
    throw Error(Tag +"throws 错误"); 
    console.log(Tag +"There will not be executed after 'testThrowsInPromise'") 
    resolve(true); 
  }) 
}

另外,是否可以参考NodeJS,提供对应实现,示例代码如下:

process.on('uncaughtException', function (err,origin) { 
  //打印出错误 
  console.error('app error ====>>>', 
    `Caught exception: '${err}' \n`, 
    `Exception origin: '${origin} \n'`); 
}); 
function testThrowsInPromise() { 
  return new Promise((resolve, reject) => { 
    throw Error("throws 错误"); 
    resolve(true); 
  }) 
} 
testThrowsInPromise();
HarmonyOS
2024-08-27 11:20:22
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

已实现捕获promise异常:

import errorManager from '@ohos.app.ability.errorManager'; 
import { BusinessError } from '@ohos.base'; 
@Entry 
@Component 
struct Page422 { 
  @State message: string = 'Hello World'; 
  aboutToAppear(): void { 
    console.log("aaaaa-1") 
    let promise1 = new Promise<boolean>((resolve, reject) => { 
      reject(new Error('[uncaught error]Let it reject!!!')); 
    }).then(()=>{ 
      console.log("aaaaa-3") 
    }) 
    let observer: errorManager.UnhandledRejectionObserver = (reason: Error, promise: Promise<void>) => { 
      console.log("aaaaa-4") 
      if (promise === promise1) { 
        console.log("promise1 is rejected"); 
      } 
      console.log("reason.name: ", reason.name); 
      console.log("reason.message: ", reason.message); 
      if (reason.stack) { 
        console.log("reason.stack: ", reason.stack); 
      } 
    }; 
    try { 
      errorManager.on('unhandledRejection', observer); 
    } catch (paramError) { 
      let code = (paramError as BusinessError).code; 
      let message = (paramError as BusinessError).message; 
      console.error(`error: ${code}, ${message}`); 
    } 
  } 
  build() { 
    RelativeContainer() { 
      Text(this.message) 
        .id('HelloWorld') 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
        .alignRules({ 
          center: { anchor: '__container__', align: VerticalAlign.Center }, 
          middle: { anchor: '__container__', align: HorizontalAlign.Center } 
        }) 
        .onClick(()=>{ 
          let a = new Promise<boolean>((_, reject) => { 
            reject(new Error('[uncaught error]Let it reject!!!')); 
          }) 
          a 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-08-27 18:54:17
相关问题
如何捕获应用发生的异常?
583浏览 • 1回复 待解决
ErrorManager捕获js异常
588浏览 • 1回复 待解决
烧录捕获到异常信息,怎么解决?
8358浏览 • 2回复 待解决
HarmonyOS如何实现Toast
322浏览 • 1回复 待解决
HarmonyOS 如何打开应用?
140浏览 • 2回复 待解决
HarmonyOS 如何短震动?
79浏览 • 1回复 待解决
HarmonyOS TextInput如何clearFocus
287浏览 • 1回复 待解决
HarmonyOS CustomDialogController如何封装
362浏览 • 1回复 待解决
HarmonyOS 如何生成UUID?
166浏览 • 1回复 待解决
HarmonyOS如何读取文件
423浏览 • 1回复 待解决
HarmonyOS 如何获取rnInstance
326浏览 • 2回复 待解决