UNPKG

2.19 kBJavaScriptView Raw
1/**
2 * Copyright 2013-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 *
10 */
11
12'use strict';
13
14var caughtError = null;
15
16/**
17 * Call a function while guarding against errors that happens within it.
18 *
19 * @param {String} name of the guard to use for logging or debugging
20 * @param {Function} func The function to invoke
21 * @param {*} a First argument
22 * @param {*} b Second argument
23 */
24function invokeGuardedCallback(name, func, a) {
25 try {
26 func(a);
27 } catch (x) {
28 if (caughtError === null) {
29 caughtError = x;
30 }
31 }
32}
33
34var ReactErrorUtils = {
35 invokeGuardedCallback: invokeGuardedCallback,
36
37 /**
38 * Invoked by ReactTestUtils.Simulate so that any errors thrown by the event
39 * handler are sure to be rethrown by rethrowCaughtError.
40 */
41 invokeGuardedCallbackWithCatch: invokeGuardedCallback,
42
43 /**
44 * During execution of guarded functions we will capture the first error which
45 * we will rethrow to be handled by the top level error handler.
46 */
47 rethrowCaughtError: function () {
48 if (caughtError) {
49 var error = caughtError;
50 caughtError = null;
51 throw error;
52 }
53 }
54};
55
56if (process.env.NODE_ENV !== 'production') {
57 /**
58 * To help development we can get better devtools integration by simulating a
59 * real browser event.
60 */
61 if (typeof window !== 'undefined' && typeof window.dispatchEvent === 'function' && typeof document !== 'undefined' && typeof document.createEvent === 'function') {
62 var fakeNode = document.createElement('react');
63 ReactErrorUtils.invokeGuardedCallback = function (name, func, a) {
64 var boundFunc = func.bind(null, a);
65 var evtType = 'react-' + name;
66 fakeNode.addEventListener(evtType, boundFunc, false);
67 var evt = document.createEvent('Event');
68 evt.initEvent(evtType, false, false);
69 fakeNode.dispatchEvent(evt);
70 fakeNode.removeEventListener(evtType, boundFunc, false);
71 };
72 }
73}
74
75module.exports = ReactErrorUtils;
\No newline at end of file