internal/warpc: Improve the JS plugin API

* Move the error handling into commons and make sure the error returned also returns message errors
* Make the protocol version an int so it can be more easily compared
This commit is contained in:
Bjørn Erik Pedersen
2024-09-12 09:13:47 +02:00
parent fe7e137e28
commit 28f621d4a7
9 changed files with 68 additions and 43 deletions

View File

@@ -41,13 +41,21 @@ export function readInput(handle) {
if (currentLine[i] === 10) {
const chunk = currentLine.splice(j, i + 1);
const arr = new Uint8Array(chunk);
let json;
let message;
try {
json = JSON.parse(new TextDecoder().decode(arr));
message = JSON.parse(new TextDecoder().decode(arr));
} catch (e) {
throw new Error(`Error parsing JSON '${new TextDecoder().decode(arr)}' from stdin: ${e.message}`);
}
handle(json);
try {
handle(message);
} catch (e) {
let header = message.header;
header.err = e.message;
writeOutput({ header: header });
}
j = i + 1;
}
}