|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow strict |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +import type {ExtendedError} from './ExtendedError'; |
| 14 | + |
| 15 | +import {SyntheticError, handleException} from './ExceptionsManager'; |
| 16 | + |
| 17 | +type ErrorInfo = { |
| 18 | + +componentStack?: ?string, |
| 19 | + // $FlowFixMe[unclear-type] unknown props and state. |
| 20 | + +errorBoundary?: ?React$Component<any, any>, |
| 21 | +}; |
| 22 | + |
| 23 | +export function onUncaughtError(errorValue: mixed, errorInfo: ErrorInfo): void { |
| 24 | + let error; |
| 25 | + |
| 26 | + // Typically, `errorValue` should be an error. However, other values such as |
| 27 | + // strings (or even null) are sometimes thrown. |
| 28 | + if (errorValue instanceof Error) { |
| 29 | + /* $FlowFixMe[class-object-subtyping] added when improving typing for |
| 30 | + * this parameters */ |
| 31 | + error = (errorValue: ExtendedError); |
| 32 | + } else if (typeof errorValue === 'string') { |
| 33 | + /* $FlowFixMe[class-object-subtyping] added when improving typing for |
| 34 | + * this parameters */ |
| 35 | + error = (new SyntheticError(errorValue): ExtendedError); |
| 36 | + } else { |
| 37 | + /* $FlowFixMe[class-object-subtyping] added when improving typing for |
| 38 | + * this parameters */ |
| 39 | + error = (new SyntheticError('Unspecified error'): ExtendedError); |
| 40 | + } |
| 41 | + try { |
| 42 | + // $FlowFixMe[incompatible-use] this is in try/catch. |
| 43 | + error.componentStack = errorInfo.componentStack; |
| 44 | + error.isComponentError = true; |
| 45 | + } catch { |
| 46 | + // Ignored. |
| 47 | + } |
| 48 | + |
| 49 | + // Uncaught errors are fatal. |
| 50 | + handleException(error, true); |
| 51 | +} |
| 52 | + |
| 53 | +export function onCaughtError(errorValue: mixed, errorInfo: ErrorInfo): void { |
| 54 | + let error; |
| 55 | + |
| 56 | + // Typically, `errorValue` should be an error. However, other values such as |
| 57 | + // strings (or even null) are sometimes thrown. |
| 58 | + if (errorValue instanceof Error) { |
| 59 | + /* $FlowFixMe[class-object-subtyping] added when improving typing for |
| 60 | + * this parameters */ |
| 61 | + error = (errorValue: ExtendedError); |
| 62 | + } else if (typeof errorValue === 'string') { |
| 63 | + /* $FlowFixMe[class-object-subtyping] added when improving typing for |
| 64 | + * this parameters */ |
| 65 | + error = (new SyntheticError(errorValue): ExtendedError); |
| 66 | + } else { |
| 67 | + /* $FlowFixMe[class-object-subtyping] added when improving typing for |
| 68 | + * this parameters */ |
| 69 | + error = (new SyntheticError('Unspecified error'): ExtendedError); |
| 70 | + } |
| 71 | + try { |
| 72 | + // $FlowFixMe[incompatible-use] this is in try/catch. |
| 73 | + error.componentStack = errorInfo.componentStack; |
| 74 | + error.isComponentError = true; |
| 75 | + } catch { |
| 76 | + // Ignored. |
| 77 | + } |
| 78 | + |
| 79 | + // Caught errors are not fatal. |
| 80 | + handleException(error, false); |
| 81 | +} |
| 82 | + |
| 83 | +export function onRecoverableError( |
| 84 | + errorValue: mixed, |
| 85 | + errorInfo: ErrorInfo, |
| 86 | +): void { |
| 87 | + let error; |
| 88 | + |
| 89 | + // Typically, `errorValue` should be an error. However, other values such as |
| 90 | + // strings (or even null) are sometimes thrown. |
| 91 | + if (errorValue instanceof Error) { |
| 92 | + /* $FlowFixMe[class-object-subtyping] added when improving typing for |
| 93 | + * this parameters */ |
| 94 | + error = (errorValue: ExtendedError); |
| 95 | + } else if (typeof errorValue === 'string') { |
| 96 | + /* $FlowFixMe[class-object-subtyping] added when improving typing for |
| 97 | + * this parameters */ |
| 98 | + error = (new SyntheticError(errorValue): ExtendedError); |
| 99 | + } else { |
| 100 | + /* $FlowFixMe[class-object-subtyping] added when improving typing for |
| 101 | + * this parameters */ |
| 102 | + error = (new SyntheticError('Unspecified error'): ExtendedError); |
| 103 | + } |
| 104 | + try { |
| 105 | + // $FlowFixMe[incompatible-use] this is in try/catch. |
| 106 | + error.componentStack = errorInfo.componentStack; |
| 107 | + error.isComponentError = true; |
| 108 | + } catch { |
| 109 | + // Ignored. |
| 110 | + } |
| 111 | + |
| 112 | + // Recoverable errors should only be warnings. |
| 113 | + // This will make it a soft error in LogBox. |
| 114 | + // TODO: improve the logging for recoverable errors in prod. |
| 115 | + console.warn(error); |
| 116 | +} |
0 commit comments