1/** 2 * Copyright (c) 650 Industries. 3 * Copyright (c) Meta Platforms, Inc. and affiliates. 4 * 5 * This source code is licensed under the MIT license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8import parseErrorStack from '../parseErrorStack'; 9class SyntheticError extends Error { 10 name = ''; 11} 12/** 13 * Handles the developer-visible aspect of errors and exceptions 14 */ 15let exceptionID = 0; 16function parseException(e, isFatal) { 17 const stack = parseErrorStack(e?.stack); 18 const currentExceptionID = ++exceptionID; 19 const originalMessage = e.message || ''; 20 let message = originalMessage; 21 if (e.componentStack != null) { 22 message += `\n\nThis error is located at:${e.componentStack}`; 23 } 24 const namePrefix = e.name == null || e.name === '' ? '' : `${e.name}: `; 25 if (!message.startsWith(namePrefix)) { 26 message = namePrefix + message; 27 } 28 message = e.jsEngine == null ? message : `${message}, js engine: ${e.jsEngine}`; 29 const data = { 30 message, 31 originalMessage: message === originalMessage ? null : originalMessage, 32 name: e.name == null || e.name === '' ? null : e.name, 33 componentStack: typeof e.componentStack === 'string' ? e.componentStack : null, 34 stack, 35 id: currentExceptionID, 36 isFatal, 37 extraData: { 38 jsEngine: e.jsEngine, 39 rawStack: e.stack, 40 }, 41 }; 42 return { 43 ...data, 44 isComponentError: !!e.isComponentError, 45 }; 46} 47/** 48 * Logs exceptions to the (native) console and displays them 49 */ 50function handleException(e) { 51 let error; 52 if (e instanceof Error) { 53 error = e; 54 } 55 else { 56 // Workaround for reporting errors caused by `throw 'some string'` 57 // Unfortunately there is no way to figure out the stacktrace in this 58 // case, so if you ended up here trying to trace an error, look for 59 // `throw '<error message>'` somewhere in your codebase. 60 error = new SyntheticError(e); 61 } 62 require('../../LogBox').default.addException(parseException(error, true)); 63} 64const ErrorUtils = { 65 parseException, 66 handleException, 67 SyntheticError, 68}; 69export default ErrorUtils; 70//# sourceMappingURL=index.js.map