如何捕获AWS Amplify错误?当前catch无法拦截报错问题
问题描述
使用以下代码调用AWS Amplify的Auth.currentUserInfo()时,控制台输出大量错误信息,设备端也弹出报错界面,且.catch()和try/catch都无法捕获这些错误:
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' }
解决方法
1. 屏蔽Amplify内部非致命日志
Auth.currentUserInfo()输出的"No current user"是Amplify内部Logger主动打印的日志,不是Promise抛出的可捕获错误,所以.catch()抓不到。可以通过调整日志级别屏蔽这类信息:
import { Logger } from '@aws-amplify/core'; // 全局设置日志级别为WARN或ERROR,只输出严重错误 Logger.LOG_LEVEL = 'WARN';
如果只想针对Auth模块调整:
import { Auth } from '@aws-amplify/auth'; Auth.configure({ Logger: { level: 'WARN' } });
2. 修复Metro路径解析错误
控制台里的ENOENT错误是Metro服务器解析bundle路径时的问题,和Amplify调用无关,按以下步骤修复:
- 检查
metro.config.js,确保使用Expo默认配置:
const { getDefaultConfig } = require('@expo/metro-config'); const config = getDefaultConfig(__dirname); module.exports = config;
- 清除缓存并重启项目:
npx expo start --clear
- iOS环境额外执行:
npm install npx pod-install ios npx expo start --clear
3. 提前校验用户登录状态
在调用Auth.currentUserInfo()前,先用Auth.currentAuthenticatedUser()检查用户是否登录,这个方法的错误可以被正常捕获,避免触发Amplify内部日志:
React.useEffect(() => { const checkUser = async () => { try { // 先校验用户是否已认证 await Auth.currentAuthenticatedUser(); // 确认登录后再获取用户信息 const data = await Auth.currentUserInfo(); if (data.username) { // do something with data } } catch (error) { console.log('No logged in user'); // 处理未登录逻辑 } }; checkUser(); }, []);
4. 添加全局错误边界
用React错误边界捕获未预料的错误,避免设备端显示崩溃界面:
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: true }; } componentDidCatch(error, errorInfo) { console.log('全局捕获错误:', error, errorInfo); } render() { if (this.state.hasError) { return <h1>出了点小问题,请稍后再试。</h1>; } return this.props.children; } } // 在根组件中包裹应用内容 export default function App() { return ( <ErrorBoundary> {/* 你的应用内容 */} </ErrorBoundary> ); }
内容的提问来源于stack exchange,提问作者ProEvilz
相关产品推荐
相关产品推荐

