Skip to content

Commit 4ee09cb

Browse files
Make Http hosting model able to report exceptions that happened while locating the function to invoke
1 parent 7ce5f8d commit 4ee09cb

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/Microsoft.AspNetCore.NodeServices/Content/Node/entrypoint-http.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@
6767
}
6868
var server = http.createServer(function (req, res) {
6969
readRequestBodyAsJson(req, function (bodyJson) {
70-
var resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
71-
var invokedModule = dynamicRequire(resolvedPath);
72-
var func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
73-
if (!func) {
74-
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
75-
}
7670
var hasSentResult = false;
7771
var callback = function (errorValue, successValue) {
7872
if (!hasSentResult) {
@@ -110,6 +104,12 @@
110104
}
111105
});
112106
try {
107+
var resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
108+
var invokedModule = dynamicRequire(resolvedPath);
109+
var func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
110+
if (!func) {
111+
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
112+
}
113113
func.apply(null, [callback].concat(bodyJson.args));
114114
}
115115
catch (synchronousException) {

src/Microsoft.AspNetCore.NodeServices/TypeScript/HttpNodeInstanceEntryPoint.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ if (parsedArgs.watch) {
1616

1717
const server = http.createServer((req, res) => {
1818
readRequestBodyAsJson(req, bodyJson => {
19-
const resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
20-
const invokedModule = dynamicRequire(resolvedPath);
21-
const func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
22-
if (!func) {
23-
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
24-
}
25-
2619
let hasSentResult = false;
2720
const callback = (errorValue, successValue) => {
2821
if (!hasSentResult) {
@@ -31,9 +24,9 @@ const server = http.createServer((req, res) => {
3124
res.statusCode = 500;
3225

3326
if (errorValue.stack) {
34-
res.end(errorValue.stack);
27+
res.end(errorValue.stack);
3528
} else {
36-
res.end(errorValue.toString());
29+
res.end(errorValue.toString());
3730
}
3831
} else if (typeof successValue !== 'string') {
3932
// Arbitrary object/number/etc - JSON-serialize it
@@ -61,6 +54,13 @@ const server = http.createServer((req, res) => {
6154
});
6255

6356
try {
57+
const resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
58+
const invokedModule = dynamicRequire(resolvedPath);
59+
const func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
60+
if (!func) {
61+
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
62+
}
63+
6464
func.apply(null, [callback].concat(bodyJson.args));
6565
} catch (synchronousException) {
6666
callback(synchronousException, null);

0 commit comments

Comments
 (0)