From 031adabd1878f3bfbcf263200bf8eb517cbece01 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Thu, 26 Mar 2015 10:45:12 -0700 Subject: [PATCH] [react-packager] better error when main file not found --- .../haste/DependencyGraph/index.js | 17 ++++++++++++++++- packager/react-packager/src/Server/index.js | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packager/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js b/packager/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js index adb012828..f517236d9 100644 --- a/packager/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js +++ b/packager/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js @@ -86,7 +86,11 @@ DependecyGraph.prototype.load = function() { DependecyGraph.prototype.getOrderedDependencies = function(entryPath) { var absolutePath = this._getAbsolutePath(entryPath); if (absolutePath == null) { - throw new Error('Cannot find entry file in any of the roots: ' + entryPath); + throw new NotFoundError( + 'Cannot find entry file %s in any of the roots: %j', + entryPath, + this._roots + ); } var module = this._graph[absolutePath]; @@ -664,4 +668,15 @@ function buildAssetMap(roots, exts) { return search(); } +function NotFoundError() { + Error.call(this); + Error.captureStackTrace(this, this.constructor); + var msg = util.format.apply(util, arguments); + this.message = msg; + this.type = this.name = 'NotFoundError'; + this.status = 404; +} + +NotFoundError.__proto__ = Error.prototype; + module.exports = DependecyGraph; diff --git a/packager/react-packager/src/Server/index.js b/packager/react-packager/src/Server/index.js index f40ebeeaf..ad417de11 100644 --- a/packager/react-packager/src/Server/index.js +++ b/packager/react-packager/src/Server/index.js @@ -281,11 +281,11 @@ function getBoolOptionFromQuery(query, opt, defaultVal) { } function handleError(res, error) { - res.writeHead(500, { + res.writeHead(error.status || 500, { 'Content-Type': 'application/json; charset=UTF-8', }); - if (error.type === 'TransformError') { + if (error.type === 'TransformError' || error.type === 'NotFoundError') { res.end(JSON.stringify(error)); } else { console.error(error.stack || error);