Skip to content

Commit adce2f3

Browse files
committed
Initial commit
0 parents  commit adce2f3

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
lib
3+
*.log
4+
DS_Store

CODE_OF_CONDUCT.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Contributor Code of Conduct
2+
3+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4+
5+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6+
7+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8+
9+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10+
11+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12+
13+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# react-transform-catch-errors
2+
3+
A [React Transform](https://github.com/gaearon/babel-plugin-react-transform) that catches errors inside `render()` function and renders a React component with an error message instead.
4+
5+
It’s up to you to choose the React component to render an error message. For example, you may use [redbox-react](https://github.com/KeywordBrain/redbox-react) that imitates React Native “red screen of death”.
6+
7+
## Installation
8+
9+
First, install the [Babel plugin](https://raw.githubusercontent.com/gaearon/babel-plugin-react-transform):
10+
11+
```
12+
npm install --save-dev babel-plugin-react-transform
13+
```
14+
15+
Then, install the transform:
16+
17+
```
18+
npm install --save-dev react-transform-catch-errors
19+
```
20+
21+
Finally, install the component for rendering errors, for example:
22+
23+
```js
24+
npm install --save-dev redbox-react
25+
```
26+
27+
You may also use a custom component instead.
28+
29+
Now edit your `.babelrc` to include `extra.babel-plugin-react-transform`.
30+
It must be an array of the transforms you want to use:
31+
32+
```js
33+
{
34+
"stage": 0,
35+
"plugins": [
36+
"react-transform"
37+
],
38+
"extra": {
39+
// must be defined and be an array
40+
"react-transform": [{
41+
"target": "react-transform-catch-errors",
42+
// now go the imports!
43+
// the first import is your React distribution
44+
// (if you use React Native, pass "react-native" instead)
45+
// the second import is the React component to render error
46+
// (it can be a local path too, like "./src/ErrorReporter")
47+
"imports": ["react", "redbox-react"]
48+
}]
49+
// note: you can put more transforms into array
50+
// this is just one of them!
51+
}
52+
}
53+
```
54+
55+
This transform has no effect when `process.env.NODE_ENV` is set to `'production'`.
56+
57+
## License
58+
59+
MIT

package.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "react-transform-catch-errors",
3+
"version": "0.1.0",
4+
"description": "React Transform that catches errors inside React components",
5+
"main": "lib/index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/gaearon/react-transform-catch-errors.git"
9+
},
10+
"author": "Dan Abramov <[email protected]>",
11+
"license": "MIT",
12+
"bugs": {
13+
"url": "https://github.com/gaearon/react-transform-catch-errors/issues"
14+
},
15+
"homepage": "https://github.com/gaearon/react-transform-catch-errors#readme",
16+
"scripts": {
17+
"clean": "rimraf lib",
18+
"build": "babel src --out-dir lib",
19+
"prepublish": "npm run clean && npm run build"
20+
},
21+
"keywords": [
22+
"react-transform",
23+
"react",
24+
"reactjs",
25+
"errors",
26+
"rhl",
27+
"dx"
28+
],
29+
"devDependencies": {
30+
"babel": "^5.8.23",
31+
"rimraf": "^2.4.3"
32+
}
33+
}

src/development.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default function catchErrors({ filename, components, imports }) {
2+
const [React, ErrorReporter] = imports;
3+
if (!React || !React.Component) {
4+
throw new Error('imports[0] for react-transform-catch-errors does not look like React.');
5+
}
6+
if (typeof ErrorReporter !== 'function') {
7+
throw new Error('imports[1] for react-transform-catch-errors does not look like a React component.');
8+
}
9+
10+
return function wrapToCatchErrors(ReactClass, componentId) {
11+
const originalRender = ReactClass.prototype.render;
12+
ReactClass.prototype.render = function tryRender() {
13+
try {
14+
return originalRender.apply(this, arguments);
15+
} catch (err) {
16+
console.error(err);
17+
return React.createElement(ErrorReporter, {
18+
error: err
19+
});
20+
}
21+
};
22+
return ReactClass;
23+
};
24+
}

src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (process.env.NODE_ENV === 'production') {
2+
module.exports = require('./production');
3+
} else {
4+
module.exports = require('./development');
5+
}

src/production.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function noop() {
2+
return ReactClass => ReactClass;
3+
}

0 commit comments

Comments
 (0)