2017-03-14 08:05:18 +08:00
|
|
|
/**
|
2017-09-25 04:48:13 +08:00
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
2013-11-09 08:53:52 +08:00
|
|
|
*
|
2017-09-25 04:48:13 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2013-11-09 08:53:52 +08:00
|
|
|
*
|
2016-08-26 06:23:13 +08:00
|
|
|
* @flow
|
2013-11-09 08:53:52 +08:00
|
|
|
*/
|
|
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
import invariant from 'fbjs/lib/invariant';
|
2017-12-01 03:10:46 +08:00
|
|
|
import invokeGuardedCallback from './invokeGuardedCallback';
|
2017-03-24 02:45:38 +08:00
|
|
|
|
2017-07-22 06:34:41 +08:00
|
|
|
const ReactErrorUtils = {
|
|
|
|
|
// Used by Fiber to simulate a try-catch.
|
[invokeGuardedCallback] Handle nested errors across separate renderers (#10270)
invokeGuardedCallback is a function we use in place of try-catch
statement. It accepts a function, and if the function throws, it
captures the error. In production, the implementation is a normal try-
catch. In development, we swap out the prod implementation for a special
version designed to preserve "Pause on all exceptions" behavior of the
browser DevTools.
invokeGuardedCallbackDev works by dispatching an event to a dummy DOM
node and calling the provided function inside a handler for that event.
We also attach an error event handler to the window object. If the
function throws, the global event handler is called and we can access
the error.
The global event handler is added and removed right before and after the
fake event is dispatched. But if invokeGuardedCallbackDev is nested --
that is, if it's invoked inside the body of another
invokeGuardedCallbackDev -- multiple error event handlers will attached
simultaneously. We only want the handler that corresponds to the deepest
level to handle the error. So we keep track of a depth counter, and
within the event handler, we only handle the error if the current depth
matches the depth at the time the function was invoked.
The problem that we discovered, and that this PR fixes, is that the
depth counter is local to each renderer. So if you nest separate copies
of invokeGuardedCallback from separate renderers, each renderer will
have its own depth counter, and multiple error handlers will fire for a
single, nested error.
2017-07-26 00:44:43 +08:00
|
|
|
_caughtError: (null: mixed),
|
|
|
|
|
_hasCaughtError: (false: boolean),
|
2017-03-24 03:13:07 +08:00
|
|
|
|
2017-07-22 06:34:41 +08:00
|
|
|
// Used by event system to capture/rethrow the first error.
|
[invokeGuardedCallback] Handle nested errors across separate renderers (#10270)
invokeGuardedCallback is a function we use in place of try-catch
statement. It accepts a function, and if the function throws, it
captures the error. In production, the implementation is a normal try-
catch. In development, we swap out the prod implementation for a special
version designed to preserve "Pause on all exceptions" behavior of the
browser DevTools.
invokeGuardedCallbackDev works by dispatching an event to a dummy DOM
node and calling the provided function inside a handler for that event.
We also attach an error event handler to the window object. If the
function throws, the global event handler is called and we can access
the error.
The global event handler is added and removed right before and after the
fake event is dispatched. But if invokeGuardedCallbackDev is nested --
that is, if it's invoked inside the body of another
invokeGuardedCallbackDev -- multiple error event handlers will attached
simultaneously. We only want the handler that corresponds to the deepest
level to handle the error. So we keep track of a depth counter, and
within the event handler, we only handle the error if the current depth
matches the depth at the time the function was invoked.
The problem that we discovered, and that this PR fixes, is that the
depth counter is local to each renderer. So if you nest separate copies
of invokeGuardedCallback from separate renderers, each renderer will
have its own depth counter, and multiple error handlers will fire for a
single, nested error.
2017-07-26 00:44:43 +08:00
|
|
|
_rethrowError: (null: mixed),
|
|
|
|
|
_hasRethrowError: (false: boolean),
|
2017-03-24 03:13:07 +08:00
|
|
|
|
2017-04-05 19:34:14 +08:00
|
|
|
/**
|
|
|
|
|
* Call a function while guarding against errors that happens within it.
|
|
|
|
|
* Returns an error if it throws, otherwise null.
|
2017-07-28 00:45:07 +08:00
|
|
|
*
|
2017-07-27 01:01:31 +08:00
|
|
|
* In production, this is implemented using a try-catch. The reason we don't
|
|
|
|
|
* use a try-catch directly is so that we can swap out a different
|
|
|
|
|
* implementation in DEV mode.
|
2017-04-05 19:34:14 +08:00
|
|
|
*
|
|
|
|
|
* @param {String} name of the guard to use for logging or debugging
|
|
|
|
|
* @param {Function} func The function to invoke
|
|
|
|
|
* @param {*} context The context to use when calling the function
|
|
|
|
|
* @param {...*} args Arguments for function
|
|
|
|
|
*/
|
2017-02-25 08:19:29 +08:00
|
|
|
invokeGuardedCallback: function<A, B, C, D, E, F, Context>(
|
|
|
|
|
name: string | null,
|
Add stack unwinding phase for handling errors (#12201)
* Add stack unwinding phase for handling errors
A rewrite of error handling, with semantics that more closely match
stack unwinding.
Errors that are thrown during the render phase unwind to the nearest
error boundary, like before. But rather than synchronously unmount the
children before retrying, we restart the failed subtree within the same
render phase. The failed children are still unmounted (as if all their
keys changed) but without an extra commit.
Commit phase errors are different. They work by scheduling an error on
the update queue of the error boundary. When we enter the render phase,
the error is popped off the queue. The rest of the algorithm is
the same.
This approach is designed to work for throwing non-errors, too, though
that feature is not implemented yet.
* Add experimental getDerivedStateFromCatch lifecycle
Fires during the render phase, so you can recover from an error within the same
pass. This aligns error boundaries more closely with try-catch semantics.
Let's keep this behind a feature flag until a future release. For now, the
recommendation is to keep using componentDidCatch. Eventually, the advice will
be to use getDerivedStateFromCatch for handling errors and componentDidCatch
only for logging.
* Reconcile twice to remount failed children, instead of using a boolean
* Handle effect immediately after its thrown
This way we don't have to store the thrown values on the effect list.
* ReactFiberIncompleteWork -> ReactFiberUnwindWork
* Remove startTime
* Remove TypeOfException
We don't need it yet. We'll reconsider once we add another exception type.
* Move replay to outer catch block
This moves it out of the hot path.
2018-02-24 09:38:42 +08:00
|
|
|
func: (a: A, b: B, c: C, d: D, e: E, f: F) => mixed,
|
2017-02-25 08:19:29 +08:00
|
|
|
context: Context,
|
|
|
|
|
a: A,
|
|
|
|
|
b: B,
|
|
|
|
|
c: C,
|
|
|
|
|
d: D,
|
|
|
|
|
e: E,
|
|
|
|
|
f: F,
|
2017-07-22 06:34:41 +08:00
|
|
|
): void {
|
|
|
|
|
invokeGuardedCallback.apply(ReactErrorUtils, arguments);
|
2017-02-25 08:19:29 +08:00
|
|
|
},
|
2015-09-23 06:29:52 +08:00
|
|
|
|
2013-11-09 08:53:52 +08:00
|
|
|
/**
|
2017-02-09 06:20:23 +08:00
|
|
|
* Same as invokeGuardedCallback, but instead of returning an error, it stores
|
|
|
|
|
* it in a global so it can be rethrown by `rethrowCaughtError` later.
|
2017-07-27 01:01:31 +08:00
|
|
|
* TODO: See if _caughtError and _rethrowError can be unified.
|
2017-02-09 06:20:23 +08:00
|
|
|
*
|
|
|
|
|
* @param {String} name of the guard to use for logging or debugging
|
|
|
|
|
* @param {Function} func The function to invoke
|
|
|
|
|
* @param {*} context The context to use when calling the function
|
|
|
|
|
* @param {...*} args Arguments for function
|
2015-06-20 13:31:40 +08:00
|
|
|
*/
|
2017-02-09 06:20:23 +08:00
|
|
|
invokeGuardedCallbackAndCatchFirstError: function<A, B, C, D, E, F, Context>(
|
|
|
|
|
name: string | null,
|
2017-03-02 06:38:29 +08:00
|
|
|
func: (a: A, b: B, c: C, d: D, e: E, f: F) => void,
|
2017-02-09 06:20:23 +08:00
|
|
|
context: Context,
|
|
|
|
|
a: A,
|
|
|
|
|
b: B,
|
|
|
|
|
c: C,
|
|
|
|
|
d: D,
|
|
|
|
|
e: E,
|
|
|
|
|
f: F,
|
|
|
|
|
): void {
|
2017-07-22 06:34:41 +08:00
|
|
|
ReactErrorUtils.invokeGuardedCallback.apply(this, arguments);
|
|
|
|
|
if (ReactErrorUtils.hasCaughtError()) {
|
|
|
|
|
const error = ReactErrorUtils.clearCaughtError();
|
|
|
|
|
if (!ReactErrorUtils._hasRethrowError) {
|
|
|
|
|
ReactErrorUtils._hasRethrowError = true;
|
|
|
|
|
ReactErrorUtils._rethrowError = error;
|
|
|
|
|
}
|
2017-02-09 06:20:23 +08:00
|
|
|
}
|
|
|
|
|
},
|
2015-06-20 13:31:40 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* During execution of guarded functions we will capture the first error which
|
|
|
|
|
* we will rethrow to be handled by the top level error handler.
|
2013-11-09 08:53:52 +08:00
|
|
|
*/
|
2015-06-20 13:31:40 +08:00
|
|
|
rethrowCaughtError: function() {
|
2017-07-22 06:34:41 +08:00
|
|
|
return rethrowCaughtError.apply(ReactErrorUtils, arguments);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
hasCaughtError: function() {
|
|
|
|
|
return ReactErrorUtils._hasCaughtError;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
clearCaughtError: function() {
|
|
|
|
|
if (ReactErrorUtils._hasCaughtError) {
|
|
|
|
|
const error = ReactErrorUtils._caughtError;
|
|
|
|
|
ReactErrorUtils._caughtError = null;
|
|
|
|
|
ReactErrorUtils._hasCaughtError = false;
|
|
|
|
|
return error;
|
|
|
|
|
} else {
|
|
|
|
|
invariant(
|
|
|
|
|
false,
|
|
|
|
|
'clearCaughtError was called but no error was captured. This error ' +
|
|
|
|
|
'is likely caused by a bug in React. Please file an issue.',
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-06-02 08:00:36 +08:00
|
|
|
},
|
2013-11-09 08:53:52 +08:00
|
|
|
};
|
|
|
|
|
|
2017-07-22 06:34:41 +08:00
|
|
|
let rethrowCaughtError = function() {
|
|
|
|
|
if (ReactErrorUtils._hasRethrowError) {
|
|
|
|
|
const error = ReactErrorUtils._rethrowError;
|
|
|
|
|
ReactErrorUtils._rethrowError = null;
|
|
|
|
|
ReactErrorUtils._hasRethrowError = false;
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
export default ReactErrorUtils;
|