Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from geowarin/improve-errors
Browse files Browse the repository at this point in the history
Show line number when a view includes a script with an error
  • Loading branch information
geowarin committed Mar 26, 2016
2 parents e153ea3 + befe327 commit f042e65
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
40 changes: 29 additions & 11 deletions src/jsxTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,35 @@ function install (options) {

require.extensions[options.extension] = function loadJsx (module, filename) {
var content = require('fs').readFileSync(filename, 'utf8');
try {
var instrumented = transform(filename, content, options);
module._compile(instrumented, filename);
} catch (e) {
if (e.originalError) {
throw e.originalError;
}
e.message = 'Error compiling ' + filename + ': ' + e.message;
e.originalError = e;
throw e;
}
var instrumented = tryInstrumenting(filename, content, options);
tryCompiling(module, instrumented, filename);
};
installed = true;
}

function tryInstrumenting (filename, content, options) {
try {
var instrumented = transform(filename, content, options);
} catch (e) {
// When instrumenting nested components, we want to see only
// the first, happening in the innermost component
if (e.originalError) {
throw e.originalError;
}
e.message = 'Error compiling ' + filename + ': ' + e.message;
e.originalError = e;
throw e;
}
return instrumented;
}

function tryCompiling (module, instrumented, filename) {
try {
module._compile(instrumented, filename);
} catch (e) {
// Chrome does not always show the stack trace
// Better show it twice than never
console.error(e.stack);
throw e;
}
}
2 changes: 1 addition & 1 deletion test-integration/hot-reload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let rootDir;
let cleanup;

describe('loadJsx', function () {
this.timeout(10000);
this.timeout(5000);

before(() => {
cleanup = jsdom();
Expand Down
24 changes: 22 additions & 2 deletions test/spec/error.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
/* global it,describe,before,after */
/* global it,describe,before,beforeEach,after,afterEach */
'use strict';

const expect = require('expect');
let errors = [];
let savedConsoleError;

describe('loadJsx', () => {
describe('Error handling', () => {
before(() => {
const electronHot = require('../../src/index');
electronHot.install();
});

beforeEach(() => {
errors = [];
savedConsoleError = console.error;
console.error = (arg) => errors.push(arg);
});

it('should show helpful information', () => {
const exceptionMessage = getExceptionMessage(() => require('./../views/AppUsingErrorScript.jsx'));
expect(exceptionMessage)
.toMatch(/^unknownFunction is not defined$/);
expect(errors[0]).toMatch(/[\w\/\-\\:]+?error\.js:2:1/);
});

it('should throw when a component contains an error', () => {
const exceptionMessage = getExceptionMessage(() => require('./../views/ErrorComponent.jsx'));
expect(exceptionMessage)
Expand All @@ -21,6 +36,11 @@ describe('loadJsx', () => {
.toMatch(/^Error compiling [\w\/\-\\:]+?ErrorComponent\.jsx: Parse Error: Line 10: Unexpected token }/);
});

afterEach(() => {
errors = [];
console.error = savedConsoleError;
});

after(() => {
delete require.extensions['.jsx'];
});
Expand Down
11 changes: 11 additions & 0 deletions test/views/AppUsingErrorScript.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

const React = require('react');
require('./error');

module.exports = class App extends React.Component {

render() {
return <div></div>
}
};
2 changes: 2 additions & 0 deletions test/views/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

unknownFunction('hello');

0 comments on commit f042e65

Please sign in to comment.