【问题标题】:How do I catch AWS Amplify errors?如何捕获 AWS Amplify 错误?
【发布时间】:2022-09-27 19:07:30
【问题描述】:

如果您考虑以下代码

  React.useEffect(() => {
    Auth.currentUserInfo()
      .then((data) => {
        if (data.username) {
          //do something with data
        }
      })
      .catch((error) => console.log(\'No logged in user\'))
  }, [])

当我查看我的控制台时,我看到所有这些都与我的控制台日志混合在一起。

[ERROR] 14:14.682 AuthClass - No current user
at node_modules/@aws-amplify/core/lib-esm/Logger/ConsoleLogger.js:115:9 in prototype._log
at node_modules/@aws-amplify/core/lib-esm/Logger/ConsoleLogger.js:192:18 in <anonymous>
at node_modules/@aws-amplify/auth/lib-esm/Auth.js:2097:38 in user.confirmPassword$argument_2.onFailure

No logged in user
Error: ENOENT: no such file or directory, open \'/Users/XXXXX/react-native-discord/http:/192.168.50.85:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false\'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at getCodeFrame (/Users/XXXXX/react-native-discord/node_modules/metro/src/Server.js:949:18)
    at Server._symbolicate (/Users/XXXXX/react-native-discord/node_modules/metro/src/Server.js:1022:22)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at Server._processRequest (/Users/XXXXX/react-native-discord/node_modules/metro/src/Server.js:429:7) {
  errno: -2,
  syscall: \'open\',
  code: \'ENOENT\',
  path: \'/Users/XXXXX/react-native-discord/http:/192.168.50.85:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false\'
}
Error: ENOENT: no such file or directory, open \'/Users/XXXXX/react-native-discord/http:/192.168.50.85:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false\'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at getCodeFrame (/Users/XXXXX/react-native-discord/node_modules/metro/src/Server.js:949:18)
    at Server._symbolicate (/Users/XXXXX/react-native-discord/node_modules/metro/src/Server.js:1022:22)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at Server._processRequest (/Users/XXXXX/react-native-discord/node_modules/metro/src/Server.js:429:7) {
  errno: -2,
  syscall: \'open\',
  code: \'ENOENT\',
  path: \'/Users/XXXXX/react-native-discord/http:/192.168.50.85:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false\'
}

我在我的设备上得到了这个

try/catchcatch() 似乎都没有发现错误。我怎样才能更好地处理错误而不会在我的控制台和设备中爆炸?

    标签: javascript reactjs typescript react-native aws-amplify


    【解决方案1】:

    您可以尝试将组件包装在错误边界中并使用后备组件处理它。尝试使用https://github.com/bvaughn/react-error-boundary

    【讨论】:

    • 这不合适,因为所讨论的代码不一定是组件,而只是任意组件中的挂钩。
    【解决方案2】:

    从堆栈跟踪来看,触发反应错误屏幕的是在 aws-amplify 库中调用 console.error。尝试在您的应用入口点文件中设置the error reporting from the console.error as false

    console.reportErrorsAsExceptions = false;
    

    或者升级到 react-native 最新版本也许this 也能解决

    顺便说一句,您的应用程序应该只在开发时中断;在您的产品构建中,本机反应应该忽略它并让您处理代码中的错误。

    【讨论】:

      【解决方案3】:

      有同样的问题,Auth.currentUserInfo 永远不会触发捕获。所以我使用了Auth.currentAuthenticatedUser(),它不会触发在登录页面上抛出错误。

      【讨论】:

        【解决方案4】:

        重构您如何处理登录用户的检查可能会有所帮助:

          let [user, setUser] = useState(null)
          useEffect(() => {
            let updateUser = async authState => {
              try {
                let user = await Auth.currentAuthenticatedUser()
                setUser(user)
              } catch {
                setUser(null)
              }
            }
            Hub.listen('auth', updateUser) // listen for login/signup events
            updateUser() // check manually the first time because we won't get a Hub event
            return () => Hub.remove('auth', updateUser) // cleanup
          }, []);
        

        GitHub Issue Reference

        【讨论】:

        • 它仍然会抛出错误
        • 为什么我需要将其传递给 Hub.listen() ? Auth.XXX() 方法与listen() 无关
        猜你喜欢
        • 1970-01-01
        • 2020-05-10
        • 1970-01-01
        • 2018-09-12
        • 1970-01-01
        • 2022-01-02
        • 2021-08-25
        • 2021-03-17
        • 2022-12-15
        相关资源
        最近更新 更多