├── .babelrc ├── .gitignore ├── CODE_OF_CONDUCT.md ├── README.md ├── package.json └── src └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "stage-0"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | lib 3 | *.log 4 | DS_Store 5 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 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/) 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | >## This Project Is Deprecated 2 | 3 | >React Hot Loader 3 is [on the horizon](https://github.com/gaearon/react-hot-loader/pull/240), and you can try it today ([boilerplate branch](https://github.com/gaearon/react-hot-boilerplate/pull/61), [upgrade example](https://github.com/gaearon/redux-devtools/commit/64f58b7010a1b2a71ad16716eb37ac1031f93915)). It fixes some [long-standing issues](https://twitter.com/dan_abramov/status/722040946075045888) with both React Hot Loader and React Transform, and is intended as a replacement for both. The docs are not there yet, but they will be added before the final release. For now, [this commit](https://github.com/gaearon/redux-devtools/commit/64f58b7010a1b2a71ad16716eb37ac1031f93915) is a good reference. 4 | 5 | # react-transform-catch-errors 6 | 7 | [![react-transform channel on discord](https://img.shields.io/badge/discord-react--transform%40reactiflux-61DAFB.svg?style=flat-square)](http://www.reactiflux.com) 8 | 9 | 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. 10 | 11 | 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”. 12 | 13 | ## 🚧🚧🚧🚧🚧 14 | 15 | This is **highly experimental tech**. If you’re enthusiastic about hot reloading, by all means, give it a try, but don’t bet your project on it. Either of the technologies it relies upon may change drastically or get deprecated any day. You’ve been warned 😉 . 16 | 17 | **This technology exists to prototype next-generation React developer experience**. Please don’t use it blindly if you don’t know the underlying technologies well. Otherwise you are likely to get disillusioned with JavaScript tooling. 18 | 19 | **No effort went into making this user-friendly yet. The goal is to eventually kill this technology** in favor of less hacky technologies baked into React. These projects are not long term. 20 | 21 | ## Installation 22 | 23 | First, install the [Babel plugin](https://github.com/gaearon/babel-plugin-react-transform): 24 | 25 | ``` 26 | npm install --save-dev babel-plugin-react-transform 27 | ``` 28 | 29 | Then, install the transform: 30 | 31 | ``` 32 | npm install --save-dev react-transform-catch-errors 33 | ``` 34 | 35 | Finally, install the component for rendering errors, for example: 36 | 37 | ```js 38 | npm install --save-dev redbox-react 39 | ``` 40 | 41 | You may also use a custom component instead. 42 | 43 | Now edit your `.babelrc` to include `extra.babel-plugin-react-transform`. 44 | It must be an array of the transforms you want to use: 45 | 46 | ```js 47 | { 48 | "presets": ["es2015", "stage-0"], 49 | "env": { 50 | // only enable it when process.env.NODE_ENV is 'development' or undefined 51 | "development": { 52 | "plugins": [["react-transform", { 53 | "transforms": [{ 54 | "transform": "react-transform-catch-errors", 55 | // now go the imports! 56 | "imports": [ 57 | 58 | // the first import is your React distribution 59 | // (if you use React Native, pass "react-native" instead) 60 | 61 | "react", 62 | 63 | // the second import is the React component to render error 64 | // (it can be a local path too, like "./src/ErrorReporter") 65 | 66 | "redbox-react" 67 | 68 | // the third import is OPTIONAL! 69 | // when specified, its export is used as options to the reporter. 70 | // see specific reporter's docs for the options it needs. 71 | 72 | // it will be imported from different files so it either has to be a Node module 73 | // or a file that you configure with Webpack/Browserify/SystemJS to resolve correctly. 74 | // for example, see https://github.com/gaearon/babel-plugin-react-transform/pull/28#issuecomment-144536185 75 | 76 | // , "my-reporter-options" 77 | ] 78 | }] 79 | // note: you can put more transforms into array 80 | // this is just one of them! 81 | }]] 82 | } 83 | } 84 | } 85 | ``` 86 | 87 | **It is up to you to ensure that the transform is not enabled when you compile the app in production mode.** The easiest way to do this is to put React Transform configuration inside `env.development` in `.babelrc` and ensure you’re calling `babel` with `NODE_ENV=development`. See [babelrc documentation](https://babeljs.io/docs/usage/babelrc/#env-option) for more details about using `env` option. 88 | 89 | ## License 90 | 91 | MIT 92 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-transform-catch-errors", 3 | "version": "1.0.2", 4 | "description": "React Transform that catches errors inside React components", 5 | "main": "lib/index.js", 6 | "files": [ 7 | "lib", 8 | "src" 9 | ], 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/gaearon/react-transform-catch-errors.git" 13 | }, 14 | "author": "Dan Abramov ", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/gaearon/react-transform-catch-errors/issues" 18 | }, 19 | "homepage": "https://github.com/gaearon/react-transform-catch-errors#readme", 20 | "scripts": { 21 | "clean": "rimraf lib", 22 | "build": "babel src --out-dir lib", 23 | "prepublish": "npm run clean && npm run build" 24 | }, 25 | "keywords": [ 26 | "react-transform", 27 | "react", 28 | "reactjs", 29 | "errors", 30 | "rhl", 31 | "dx" 32 | ], 33 | "devDependencies": { 34 | "babel-cli": "^6.3.17", 35 | "babel-core": "^6.3.21", 36 | "babel-preset-es2015": "^6.3.3", 37 | "babel-preset-stage-0": "^6.3.13", 38 | "rimraf": "^2.4.3" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export default function catchErrors({ filename, components, imports }) { 2 | const [React, ErrorReporter, reporterOptions] = imports; 3 | 4 | if (!React || !React.Component) { 5 | throw new Error('imports[0] for react-transform-catch-errors does not look like React.'); 6 | } 7 | if (typeof ErrorReporter !== 'function') { 8 | throw new Error('imports[1] for react-transform-catch-errors does not look like a React component.'); 9 | } 10 | 11 | return function wrapToCatchErrors(ReactClass, componentId) { 12 | const originalRender = ReactClass.prototype.render; 13 | 14 | Object.defineProperty(ReactClass.prototype, 'render', { 15 | configurable: true, 16 | value: function tryRender() { 17 | try { 18 | return originalRender.apply(this, arguments); 19 | } catch (err) { 20 | setTimeout(() => { 21 | if (typeof console.reportErrorsAsExceptions !== 'undefined') { 22 | let prevReportErrorAsExceptions = console.reportErrorsAsExceptions; 23 | // We're in React Native. Don't throw. 24 | // Stop react-native from triggering its own error handler 25 | console.reportErrorsAsExceptions = false; 26 | // Log an error 27 | console.error(err); 28 | // Reactivate it so other errors are still handled 29 | console.reportErrorsAsExceptions = prevReportErrorAsExceptions; 30 | } else { 31 | throw err; 32 | } 33 | }); 34 | 35 | return React.createElement(ErrorReporter, { 36 | error: err, 37 | filename, 38 | ...reporterOptions 39 | }); 40 | } 41 | } 42 | }); 43 | 44 | return ReactClass; 45 | }; 46 | } 47 | --------------------------------------------------------------------------------