未调用React Native ErrorUtils全局处理程序

时间:2020-04-06 03:25:49

标签: react-native

我正在使用RN 0.61.5,并设置了自定义错误处理程序ErrorUtils.setGlobalHandler(customHandler);

export const setGlobalErrorHandler = once(nativeModule => {
  console.log("inside setGlobalErrorHandler")
  const originalHandler = ErrorUtils.getGlobalHandler();
  console.log("originalHandler:", originalHandler)


  async function handler(error, fatal) {
    if (__DEV__) {
      console.log("is dev env")
      return originalHandler(error, fatal);
    }

    console.log("inside setGlobalErrorHandler, is not dev env")


    if (!isError(error)) {
      console.log("is not error type")
      await nativeModule.logPromise(`Unknown Error: ${error}`);
      return originalHandler(error, fatal);
    }

    try {
      console.log("create JS error")
      const stackFrames = await StackTrace.fromError(error, { offline: true });
      await nativeModule.recordErrorPromise(createNativeErrorObj(error, stackFrames, false));
    } catch (e) {
      console.log("met with error:", JSON.stringify(e));
      // do nothing
    }
    return originalHandler(error, fatal);
  }

  ErrorUtils.setGlobalHandler(handler);
  console.log("newHandler:", ErrorUtils.getGlobalHandler())

  return handler;
});

通过单击按钮时抛出JS错误进行了一次小测试,但是我的customHandler未被调用。该应用程序不会显示红色错误框,也不会崩溃。我怀疑其他地方正在处理该错误。我确实实现了任何可以捕获所有未捕获的错误的ErrorBoundary

0 个答案:

没有答案