mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 10:08:57 +00:00
In HttpNodeInstance, correctly report response serialisation errors back to .NET (previously, it just timed out)
This commit is contained in:
@@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/***/ },
|
/***/ },
|
||||||
|
|||||||
@@ -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());
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user