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