Skip to content

Commit f358d8e

Browse files
In HttpNodeInstance, correctly report response serialisation errors back to .NET (previously, it just timed out)
1 parent 465d0c8 commit f358d8e

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,21 @@
6969
if(!hasSentResult){
7070
hasSentResult=true;
7171
if(errorValue){
72-
res.statusCode=500;
73-
if(errorValue.stack){
74-
res.end(errorValue.stack);
75-
}
76-
else{
77-
res.end(errorValue.toString());
78-
}
72+
respondWithError(res,errorValue);
7973
}
8074
elseif(typeofsuccessValue!=='string'){
8175
// Arbitrary object/number/etc - JSON-serialize it
76+
varsuccessValueJson=void0;
77+
try{
78+
successValueJson=JSON.stringify(successValue);
79+
}
80+
catch(ex){
81+
// JSON serialization error - pass it back to .NET
82+
respondWithError(res,ex);
83+
return;
84+
}
8285
res.setHeader('Content-Type','application/json');
83-
res.end(JSON.stringify(successValue));
86+
res.end(JSON.stringify(successValueJson));
8487
}
8588
else{
8689
// String - can bypass JSON-serialization altogether
@@ -129,6 +132,10 @@
129132
.on('data',function(chunk){requestBodyAsString+=chunk;})
130133
.on('end',function(){callback(JSON.parse(requestBodyAsString));});
131134
}
135+
functionrespondWithError(res,errorValue){
136+
res.statusCode=500;
137+
res.end(errorValue.stack||errorValue.toString());
138+
}
132139

133140

134141
/***/},

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ const server = http.createServer((req, res) =>{
1717
if(!hasSentResult){
1818
hasSentResult=true;
1919
if(errorValue){
20-
res.statusCode=500;
21-
22-
if(errorValue.stack){
23-
res.end(errorValue.stack);
24-
}else{
25-
res.end(errorValue.toString());
26-
}
20+
respondWithError(res,errorValue);
2721
}elseif(typeofsuccessValue!=='string'){
2822
// Arbitrary object/number/etc - JSON-serialize it
23+
letsuccessValueJson: string;
24+
try{
25+
successValueJson=JSON.stringify(successValue);
26+
}catch(ex){
27+
// JSON serialization error - pass it back to .NET
28+
respondWithError(res,ex);
29+
return;
30+
}
2931
res.setHeader('Content-Type','application/json');
30-
res.end(JSON.stringify(successValue));
32+
res.end(JSON.stringify(successValueJson));
3133
}else{
3234
// String - can bypass JSON-serialization altogether
3335
res.setHeader('Content-Type','text/plain');
@@ -82,3 +84,8 @@ function readRequestBodyAsJson(request, callback){
8284
.on('data',chunk=>{requestBodyAsString+=chunk;})
8385
.on('end',()=>{callback(JSON.parse(requestBodyAsString));});
8486
}
87+
88+
functionrespondWithError(res: http.ServerResponse,errorValue: any){
89+
res.statusCode=500;
90+
res.end(errorValue.stack||errorValue.toString());
91+
}

0 commit comments

Comments
(0)