diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index a4ee53ef..fdeffc65 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -372,12 +372,36 @@ Server.prototype.processRequest = function(req, res, next) { Activity.endEvent(startReqEventId); } }, - function(error) { - handleError(res, error); - } + this._handleError.bind(this, res, optionsJson) ).done(); }; +Server.prototype._handleError = function(res, packageID, error) { + res.writeHead(error.status || 500, { + 'Content-Type': 'application/json; charset=UTF-8', + }); + + if (error.type === 'TransformError' || error.type === 'NotFoundError') { + error.errors = [{ + description: error.description, + filename: error.filename, + lineNumber: error.lineNumber, + }]; + res.end(JSON.stringify(error)); + + if (error.type === 'NotFoundError') { + delete this._packages[packageID]; + } + } else { + console.error(error.stack || error); + res.end(JSON.stringify({ + type: 'InternalError', + message: 'react-packager has encountered an internal error, ' + + 'please check your terminal error output for more details', + })); + } +}; + function getOptionsFromUrl(reqUrl) { // `true` to parse the query param as an object. var urlObj = url.parse(reqUrl, true); @@ -417,26 +441,3 @@ function getBoolOptionFromQuery(query, opt, defaultVal) { return query[opt] === 'true' || query[opt] === '1'; } - -function handleError(res, error) { - res.writeHead(error.status || 500, { - 'Content-Type': 'application/json; charset=UTF-8', - }); - - if (error.type === 'TransformError' || error.type === 'NotFoundError') { - error.errors = [{ - description: error.description, - filename: error.filename, - lineNumber: error.lineNumber, - }]; - console.error(error); - res.end(JSON.stringify(error)); - } else { - console.error(error.stack || error); - res.end(JSON.stringify({ - type: 'InternalError', - message: 'react-packager has encountered an internal error, ' + - 'please check your terminal error output for more details', - })); - } -}