-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-catcher.js
More file actions
134 lines (115 loc) · 3.24 KB
/
error-catcher.js
File metadata and controls
134 lines (115 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict';
var DEFAULT_STYLE = {
display : 'inline-block',
color : '#ffffff',
background : '#aa0000',
whiteSpace : 'pre-wrap',
wordSpacing : 'normal',
wordBreak : 'normal'
};
var SAFE_RESULTS = {
getInitialState: {},
shouldComponentUpdate: true
};
var RENDERED_KEYS = {
componentDidMount: true,
componentDidUpdate: true
};
/**
* Overloads some component's methods to catch errors and pass them to the
* report function. The report function may return a ReactElement or
* ReactComponent to be rendered in the event of an error.
*
* @param {Object} React
* @param {String} filename
* @param {String} displayName
* @param {ReactComponent|Function} reporter
* @return {Function}
* @api public
*/
module.exports = errorCatcher;
function errorCatcher (React, filename, displayName, reporter) {
var report;
if (reporter.prototype && typeof reporter.prototype.render === 'function') {
report = function (error, instance, filename, displayName) {
return React.createElement(reporter, {
error: error,
instance: instance,
filename: filename,
displayName: displayName
});
};
} else {
report = reporter;
}
return function patch (Component) {
var proto = Component.prototype;
var keys = Object.getOwnPropertyNames(proto);
var nextRender = null;
if (Component._patchedToCatch) {
return Component;
}
Component._patchedToCatch = true;
keys.forEach(function (key) {
var method = proto[key];
if (typeof method !== 'function') {
return;
}
function getResult () {
var result;
try {
result = method.apply(this, arguments);
} catch (error) {
nextRender = report(error, this, filename, displayName);
if (nextRender) {
setTimeout(function () {
this.forceUpdate();
}.bind(this));
}
if (SAFE_RESULTS[key]) {
result = SAFE_RESULTS[key];
}
}
return result;
}
if (key === 'render') {
proto[key] = function () {
var result = nextRender; // render reporter's message if truthy
nextRender = null; // and then forget it
return result
|| getResult.apply(this, arguments)
|| renderDefault(React, displayName);
};
} else if (report.rendered && RENDERED_KEYS[key]) {
proto[key] = function () {
report.rendered(this, filename, displayName);
return getResult.apply(this, arguments);
};
} else {
proto[key] = getResult;
}
});
// ensure the reporter knows when the component has mounted/updated
if (report.rendered) {
for (var key in RENDERED_KEYS) {
if (!proto[key]) {
proto[key] = function () {
report.rendered(this, filename, displayName);
}
}
}
}
return Component;
}
}
/**
* Creates a ReactElement containing the `displayName`.
*
* @param {Object} React
* @param {String} displayName
* @return {ReactElement}
* @api private
*/
function renderDefault (React, displayName) {
return React.createElement('div', {style: DEFAULT_STYLE}, displayName);
}