Make Http hosting model able to report exceptions that happened while locating the function to invoke

This commit is contained in:
SteveSandersonMS
2016-07-06 15:47:06 +01:00
parent 7ce5f8d4ad
commit 4ee09cbe82
2 changed files with 15 additions and 15 deletions

View File

@@ -67,12 +67,6 @@
} }
var server = http.createServer(function (req, res) { var server = http.createServer(function (req, res) {
readRequestBodyAsJson(req, function (bodyJson) { readRequestBodyAsJson(req, function (bodyJson) {
var resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
var invokedModule = dynamicRequire(resolvedPath);
var func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
if (!func) {
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
}
var hasSentResult = false; var hasSentResult = false;
var callback = function (errorValue, successValue) { var callback = function (errorValue, successValue) {
if (!hasSentResult) { if (!hasSentResult) {
@@ -110,6 +104,12 @@
} }
}); });
try { try {
var resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
var invokedModule = dynamicRequire(resolvedPath);
var func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
if (!func) {
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
}
func.apply(null, [callback].concat(bodyJson.args)); func.apply(null, [callback].concat(bodyJson.args));
} }
catch (synchronousException) { catch (synchronousException) {

View File

@@ -16,13 +16,6 @@ if (parsedArgs.watch) {
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
readRequestBodyAsJson(req, bodyJson => { readRequestBodyAsJson(req, bodyJson => {
const resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
const invokedModule = dynamicRequire(resolvedPath);
const func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
if (!func) {
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
}
let hasSentResult = false; let hasSentResult = false;
const callback = (errorValue, successValue) => { const callback = (errorValue, successValue) => {
if (!hasSentResult) { if (!hasSentResult) {
@@ -31,9 +24,9 @@ const server = http.createServer((req, res) => {
res.statusCode = 500; res.statusCode = 500;
if (errorValue.stack) { if (errorValue.stack) {
res.end(errorValue.stack); res.end(errorValue.stack);
} else { } else {
res.end(errorValue.toString()); res.end(errorValue.toString());
} }
} else if (typeof successValue !== 'string') { } else if (typeof successValue !== 'string') {
// Arbitrary object/number/etc - JSON-serialize it // Arbitrary object/number/etc - JSON-serialize it
@@ -61,6 +54,13 @@ const server = http.createServer((req, res) => {
}); });
try { try {
const resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
const invokedModule = dynamicRequire(resolvedPath);
const func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
if (!func) {
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
}
func.apply(null, [callback].concat(bodyJson.args)); func.apply(null, [callback].concat(bodyJson.args));
} catch (synchronousException) { } catch (synchronousException) {
callback(synchronousException, null); callback(synchronousException, null);