Skip to content

Commit

Permalink
Add hook to handleError.
Browse files Browse the repository at this point in the history
Why:

* It is hard to send exceptions to an exception tracker (e.g. Rollbar)
  when they're all caught and sent to console.error.

This change addresses the need by:

* Update handleError to check for the existence of a logException
  function on the window, then pass the exception to it if it exists.
  • Loading branch information
squaresurf committed Aug 8, 2016
1 parent c77d3b6 commit 5ca8b16
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions node_package/src/ReactOnRails.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ctx = context();

const DEFAULT_OPTIONS = {
traceTurbolinks: false,
exceptionLogger: function(e) {}
};

ctx.ReactOnRails = {
Expand Down Expand Up @@ -56,13 +57,19 @@ ctx.ReactOnRails = {
* Set options for ReactOnRails, typically before you call ReactOnRails.register
* Available Options:
* `traceTurbolinks: true|false Gives you debugging messages on Turbolinks events
* `exceptionLogger: function Gets passed the exception as the first argument
*/
setOptions(options) {
if (options.hasOwnProperty('traceTurbolinks')) {
this._options.traceTurbolinks = options.traceTurbolinks;
delete options.traceTurbolinks;
}

if (options.hasOwnProperty('exceptionLogger')) {
this._options.exceptionLogger = options.exceptionLogger;
delete options.exceptionLogger;
}

if (Object.keys(options).length > 0) {
throw new Error('Invalid options passed to ReactOnRails.options: ', JSON.stringify(options));
}
Expand Down
2 changes: 2 additions & 0 deletions node_package/src/handleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ ${e.stack}`;
const reactElement = React.createElement('pre', null, msg);
return ReactDOMServer.renderToString(reactElement);
}

ReactOnRails.options('excptionLogger')(e);
};
9 changes: 9 additions & 0 deletions node_package/tests/ReactOnRails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ test('ReactOnRails accepts traceTurbolinks as an option false', (assert) => {
assert.equal(actual, false);
});

test('ReactOnRails accepts exceptionLogger as an option function', (assert) => {
ReactOnRails.resetOptions();
assert.plan(1);
const logger = function() {};
ReactOnRails.setOptions({ exceptionLogger: logger });
const actual = ReactOnRails.option('exceptionLogger');
assert.equal(actual, logger);
});

test('ReactOnRails not specified has traceTurbolinks as false', (assert) => {
ReactOnRails.resetOptions();
assert.plan(1);
Expand Down
5 changes: 4 additions & 1 deletion spec/dummy/client/app/startup/clientRegistration.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import CssModulesImagesFontsExample from '../components/CssModulesImagesFontsExa
import SharedReduxStore from '../stores/SharedReduxStore'

ReactOnRails.setOptions({
traceTurbolinks: true
traceTurbolinks: true,
exceptionLogger: function(e) {
console.log('Log from exceptionLogger:', e);
}
});

ReactOnRails.register({
Expand Down

0 comments on commit 5ca8b16

Please sign in to comment.