In HttpNodeInstance, correctly report response serialisation errors back to .NET (previously, it just timed out)

This commit is contained in:
SteveSandersonMS
2016-09-07 17:59:13 +01:00
parent 465d0c8d15
commit f358d8e2b2
2 changed files with 30 additions and 16 deletions

View File

@@ -69,18 +69,21 @@
if (!hasSentResult) { if (!hasSentResult) {
hasSentResult = true; hasSentResult = true;
if (errorValue) { if (errorValue) {
res.statusCode = 500; respondWithError(res, errorValue);
if (errorValue.stack) {
res.end(errorValue.stack);
}
else {
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
var successValueJson = void 0;
try {
successValueJson = JSON.stringify(successValue);
}
catch (ex) {
// JSON serialization error - pass it back to .NET
respondWithError(res, ex);
return;
}
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(successValue)); res.end(JSON.stringify(successValueJson));
} }
else { else {
// String - can bypass JSON-serialization altogether // String - can bypass JSON-serialization altogether
@@ -129,6 +132,10 @@
.on('data', function (chunk) { requestBodyAsString += chunk; }) .on('data', function (chunk) { requestBodyAsString += chunk; })
.on('end', function () { callback(JSON.parse(requestBodyAsString)); }); .on('end', function () { callback(JSON.parse(requestBodyAsString)); });
} }
function respondWithError(res, errorValue) {
res.statusCode = 500;
res.end(errorValue.stack || errorValue.toString());
}
/***/ }, /***/ },

View File

@@ -17,17 +17,19 @@ const server = http.createServer((req, res) => {
if (!hasSentResult) { if (!hasSentResult) {
hasSentResult = true; hasSentResult = true;
if (errorValue) { if (errorValue) {
res.statusCode = 500; respondWithError(res, errorValue);
if (errorValue.stack) {
res.end(errorValue.stack);
} else {
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
let successValueJson: string;
try {
successValueJson = JSON.stringify(successValue);
} catch (ex) {
// JSON serialization error - pass it back to .NET
respondWithError(res, ex);
return;
}
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(successValue)); res.end(JSON.stringify(successValueJson));
} else { } else {
// String - can bypass JSON-serialization altogether // String - can bypass JSON-serialization altogether
res.setHeader('Content-Type', 'text/plain'); res.setHeader('Content-Type', 'text/plain');
@@ -82,3 +84,8 @@ function readRequestBodyAsJson(request, callback) {
.on('data', chunk => { requestBodyAsString += chunk; }) .on('data', chunk => { requestBodyAsString += chunk; })
.on('end', () => { callback(JSON.parse(requestBodyAsString)); }); .on('end', () => { callback(JSON.parse(requestBodyAsString)); });
} }
function respondWithError(res: http.ServerResponse, errorValue: any) {
res.statusCode = 500;
res.end(errorValue.stack || errorValue.toString());
}