[react-packager] better error when main file not found

This commit is contained in:
Amjad Masad 2015-03-26 10:45:12 -07:00
parent 7a97043a3e
commit 031adabd18
2 changed files with 18 additions and 3 deletions

View File

@ -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;

View File

@ -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);