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); 
  }) 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

另外,是否可以参考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();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
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%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
分享
微博
QQ
微信
回复
2024-08-27 18:54:17
相关问题
如何捕获应用发生的异常?
1306浏览 • 1回复 待解决
HarmonyOS 全局崩溃捕获问题
772浏览 • 1回复 待解决
HarmonyOS 捕获屏幕视频流
621浏览 • 1回复 待解决
ErrorManager捕获js异常
1420浏览 • 1回复 待解决
HarmonyOS 捕获异常Invalid parameter value
864浏览 • 1回复 待解决
HarmonyOS可以全局捕获异常吗?
767浏览 • 1回复 待解决
HarmonyOS 捕获异常再抛出为什么报错
899浏览 • 1回复 待解决
烧录捕获到异常信息,怎么解决?
9363浏览 • 2回复 待解决