Skip to content

Commit

Permalink
Fix RN redbox messages for syntax errors by including error messages …
Browse files Browse the repository at this point in the history
…in payload

With RN 0.52, when there was a redbox due to a syntax error in a source file (with regular, non-delta bundler), the redbox would say just "No message provided". The JSON that Metro sent to RN did not include a "message" field because `JSON.stringify(error)` does not include `message`.

Test Plan: Add a syntax error to one of the files in RNTester's JS and load the RNTester app (from RN master). See that the redbox now says there was a transform error with info about where the syntax error is.

Also added a Jest test to Server-test.js.
  • Loading branch information
ide committed Jan 15, 2018
1 parent 23855d0 commit d356c4c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/metro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"mkdirp": "^0.5.1",
"request": "^2.79.0",
"rimraf": "^2.5.4",
"serialize-error": "^2.1.0",
"source-map": "^0.5.6",
"temp": "0.8.3",
"throat": "^4.1.0",
Expand Down
25 changes: 25 additions & 0 deletions packages/metro/src/Server/__tests__/Server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,31 @@ describe('processRequest', () => {
expect(response.body).toEqual('{"delta": "bundle"}');
});
});

it('should include the error message for transform errors', () => {
Serializers.deltaBundle.mockImplementation(async () => {
const transformError = new SyntaxError('test syntax error');
transformError.type = 'TransformError';
transformError.filename = 'testFile.js';
transformError.lineNumber = 123;
throw transformError;
});

return makeRequest(requestHandler, 'index.delta?platform=ios').then(
function(response) {
expect(() => JSON.parse(response.body)).not.toThrow();
const body = JSON.parse(response.body);
expect(body).toMatchObject({
type: 'TransformError',
message: 'test syntax error',
});
expect(body.errors).toContainEqual({
filename: 'testFile.js',
lineNumber: 123,
});
},
);
});
});

describe('/onchange endpoint', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/metro/src/Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const getOrderedDependencyPaths = require('../lib/getOrderedDependencyPaths');
const mime = require('mime-types');
const parsePlatformFilePath = require('../node-haste/lib/parsePlatformFilePath');
const path = require('path');
const serializeError = require('serialize-error');
const symbolicate = require('./symbolicate');
const url = require('url');

Expand Down Expand Up @@ -746,7 +747,7 @@ class Server {
res.writeHead(error.status || 500, {
'Content-Type': 'application/json; charset=UTF-8',
});
res.end(JSON.stringify(formattedError));
res.end(JSON.stringify(serializeError(formattedError)));
this._reporter.update({error, type: 'bundling_error'});

log({
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4869,6 +4869,10 @@ [email protected]:
range-parser "~1.0.3"
statuses "~1.2.1"

serialize-error@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/serialize-error/-/serialize-error-2.1.0.tgz#50b679d5635cdf84667bdc8e59af4e5b81d5f60a"

serve-favicon@~2.3.0:
version "2.3.2"
resolved "https://registry.yarnpkg.com/serve-favicon/-/serve-favicon-2.3.2.tgz#dd419e268de012ab72b319d337f2105013f9381f"
Expand Down

0 comments on commit d356c4c

Please sign in to comment.