-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
94 lines (85 loc) · 3.21 KB
/
index.js
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
'use strict';
var React = require('react');
Object.defineProperty(exports, '__esModule', {
value: true
});
/**
* Configuration that allows overriding default behavior.
* @type {Object}
*/
var config = {
/**
* If enabled=false then this essentially disables the try/catch monkey-patch
* @type {Boolean}
*/
enabled: true,
/**
* Currently thif is configured to console.error a useful message if one of you
* Component lifecycle methos throws an error. You can override the implementation
* to integrate with you'r eown error logging.
* @param {[Object]} errorReport The report metadata including component, method, error thrown.
* @return {[void]}
*/
errorHandler: function errorHandler(errorReport) {
console.error('Error in ' + errorReport.component + '.' + errorReport.method + '(' + (errorReport.arguments ? '...' : '') + '): ' + errorReport.error, errorReport);
}
};
exports.config = config;
/**
* Implementation of the try/catch wrapper
* @param {[React.Component]} component The ES6 React.Component.prototype that contains the React lifecycle method.
* @param {[string]} method The name of the method to wrap ex: "render"
* @return {[React.Component]} Returns the same React.Component.prototype method monkey-patched with the specified method wrapped with a try/catch
*/
var wrapWithTryCatch = function wrapWithTryCatch(component, method) {
var unsafe = component[method];
component[method] = function () {
if (!config.enabled) {
return;
}
try {
return unsafe.apply(this, arguments);
} catch (err) {
var errorReport = {
component: component.constructor.name,
method: method,
props: this.props,
error: err
};
if (arguments.length > 0) {
errorReport.arguments = arguments;
}
var returnValue = config.errorHandler(errorReport);
if (method === 'render') {
return returnValue || React.createElement('h1', {}, 'error');
}
}
};
if (method === "constructor") {
console.log(component[method]);
}
};
/**
* Wraps each React.Component lifecycle method with a try/catch that enables easier development diagnostics of errors throwin within each method
* Methods wrapped include: `render, componentWillMount, componentDidMount, componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate, componentDidUpdate, componentWillUnmount`
* @param {[React.Component]} ComponentConstructor The React.Component you want to wrap lifecycle methods with a try/catch and error handler.
* @return {[void]} [description]
*/
var wrapReactLifecycleMethodsWithTryCatch = function wrapReactLifecycleMethodsWithTryCatch(ComponentConstructor) {
[
'render',
'componentWillMount',
'componentDidMount',
'componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'componentDidUpdate',
'componentWillUnmount'
].forEach(function (method) {
if (ComponentConstructor.prototype[method]) {
wrapWithTryCatch(ComponentConstructor.prototype, method);
}
});
};
exports.wrapReactLifecycleMethodsWithTryCatch = wrapReactLifecycleMethodsWithTryCatch;
exports['default'] = wrapReactLifecycleMethodsWithTryCatch;