Skip to content

Commit

Permalink
Respond with HTTP 404 if bundle entry point cannot be resolved
Browse files Browse the repository at this point in the history
Summary:
Changelog: **[Fix]** Respond with HTTP 404 when a bundle entry point cannot be resolved.

Currently, Metro uses HTTP code 500 (Internal Server Error) for all errors. In the case of "bundle not found" errors, it can be useful to respond with 404 (Not Found) instead. This seemingly was Metro's behaviour at one point (D9772264) but has since regressed.

Reviewed By: huntie

Differential Revision: D51856058

fbshipit-source-id: 63ea7a7cbab5b0347def962867dcc720ac84946b
  • Loading branch information
motiz88 authored and facebook-github-bot committed Dec 11, 2023
1 parent 2c718eb commit cbfb96d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/metro/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const getGraphId = require('./lib/getGraphId');
const parseOptionsFromUrl = require('./lib/parseOptionsFromUrl');
const splitBundleOptions = require('./lib/splitBundleOptions');
const transformHelpers = require('./lib/transformHelpers');
const {
UnableToResolveError,
} = require('./node-haste/DependencyGraph/ModuleResolution');
const parsePlatformFilePath = require('./node-haste/lib/parsePlatformFilePath');
const MultipartResponse = require('./Server/MultipartResponse');
const symbolicate = require('./Server/symbolicate');
Expand Down Expand Up @@ -610,11 +613,23 @@ class Server {
* `entryFile` is relative to projectRoot, we need to use resolution function
* to find the appropriate file with supported extensions.
*/
const resolvedEntryFilePath = await this._resolveRelativePath(entryFile, {
relativeTo: 'server',
resolverOptions,
transformOptions,
});
let resolvedEntryFilePath;
try {
resolvedEntryFilePath = await this._resolveRelativePath(entryFile, {
relativeTo: 'server',
resolverOptions,
transformOptions,
});
} catch (error) {
const formattedError = formatBundlingError(error);

const status = error instanceof UnableToResolveError ? 404 : 500;
res.writeHead(status, {
'Content-Type': 'application/json; charset=UTF-8',
});
res.end(JSON.stringify(formattedError));
return;
}
const graphId = getGraphId(resolvedEntryFilePath, transformOptions, {
unstable_allowRequireContext:
this._config.transformer.unstable_allowRequireContext,
Expand Down
16 changes: 16 additions & 0 deletions packages/metro/src/integration_tests/__tests__/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,20 @@ describe('Metro development server serves bundles via HTTP', () => {
]),
);
});

test('responds with 404 when the bundle cannot be resolved', async () => {
const response = await fetch(
'http://localhost:' + config.server.port + '/doesnotexist.bundle',
);
expect(response.status).toBe(404);
});

test('responds with 500 when an import inside the bundle cannot be resolved', async () => {
const response = await fetch(
'http://localhost:' +
config.server.port +
'/build-errors/inline-requires-cannot-resolve-import.bundle',
);
expect(response.status).toBe(500);
});
});

0 comments on commit cbfb96d

Please sign in to comment.