mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 01:58:29 +00:00
Make Http hosting model able to report exceptions that happened while locating the function to invoke
This commit is contained in:
@@ -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) {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user