Skip to content

Commit

Permalink
Extract common logic in ErrorHandlers to a shared method
Browse files Browse the repository at this point in the history
Summary:
Changelog: [internal]

Just removing some unnecessary duplication from this file.

Differential Revision: D67407220
  • Loading branch information
rubennorte authored and facebook-github-bot committed Dec 23, 2024
1 parent 5b6e35a commit 5fc1d62
Showing 1 changed file with 12 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ type ErrorInfo = {
+errorBoundary?: ?React$Component<any, any>,
};

export function onUncaughtError(errorValue: mixed, errorInfo: ErrorInfo): void {
function getExtendedError(
errorValue: mixed,
errorInfo: ErrorInfo,
): ExtendedError {
let error;

// Typically, `errorValue` should be an error. However, other values such as
Expand Down Expand Up @@ -50,38 +53,18 @@ export function onUncaughtError(errorValue: mixed, errorInfo: ErrorInfo): void {
// Ignored.
}

return error;
}

export function onUncaughtError(errorValue: mixed, errorInfo: ErrorInfo): void {
const error = getExtendedError(errorValue, errorInfo);

// Uncaught errors are fatal.
handleException(error, true);
}

export function onCaughtError(errorValue: mixed, errorInfo: ErrorInfo): void {
let error;

// Typically, `errorValue` should be an error. However, other values such as
// strings (or even null) are sometimes thrown.
if (errorValue instanceof Error) {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
// $FlowFixMe[incompatible-cast]
error = (errorValue: ExtendedError);
} else if (typeof errorValue === 'string') {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
// $FlowFixMe[incompatible-cast]
error = (new SyntheticError(errorValue): ExtendedError);
} else {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
// $FlowFixMe[incompatible-cast]
error = (new SyntheticError('Unspecified error'): ExtendedError);
}
try {
// $FlowFixMe[incompatible-use] this is in try/catch.
error.componentStack = errorInfo.componentStack;
error.isComponentError = true;
} catch {
// Ignored.
}
const error = getExtendedError(errorValue, errorInfo);

// Caught errors are not fatal.
handleException(error, false);
Expand All @@ -91,33 +74,7 @@ export function onRecoverableError(
errorValue: mixed,
errorInfo: ErrorInfo,
): void {
let error;

// Typically, `errorValue` should be an error. However, other values such as
// strings (or even null) are sometimes thrown.
if (errorValue instanceof Error) {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
// $FlowFixMe[incompatible-cast]
error = (errorValue: ExtendedError);
} else if (typeof errorValue === 'string') {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
// $FlowFixMe[incompatible-cast]
error = (new SyntheticError(errorValue): ExtendedError);
} else {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
// $FlowFixMe[incompatible-cast]
error = (new SyntheticError('Unspecified error'): ExtendedError);
}
try {
// $FlowFixMe[incompatible-use] this is in try/catch.
error.componentStack = errorInfo.componentStack;
error.isComponentError = true;
} catch {
// Ignored.
}
const error = getExtendedError(errorValue, errorInfo);

// Recoverable errors should only be warnings.
// This will make it a soft error in LogBox.
Expand Down

0 comments on commit 5fc1d62

Please sign in to comment.