From 8ef7198d624aa4a6f146fe8b6257ab3344e963f3 Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Mon, 29 Jul 2024 12:14:31 +0200 Subject: [PATCH] Improve error-handling --- lib/forwarder.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/forwarder.js b/lib/forwarder.js index d657bf2..7236362 100644 --- a/lib/forwarder.js +++ b/lib/forwarder.js @@ -50,25 +50,33 @@ export async function forwardToDaemon(resolver, config) { } }) .on('end', () => { - const notForwardedContent = content.slice(0, -5); + const endOfContent = content.slice(-5); - if(fixToStdout) { - content = content.slice(-5); - } - - if (content.startsWith('EXIT')) { - process.exitCode = Number(content.slice(4)); - } else { + if(!endOfContent.startsWith("EXIT")) { process.stdout.write(content); console.error('eslint_d: unexpected response'); process.exitCode = 1; + return; + } + + content = content.slice(0, -5); + const exitCode = Number(endOfContent.slice(4)); + process.exitCode = exitCode; + + if(exitCode !== 0 && exitCode !== 1) { + process.stdout.write(content); + return; } - if(fixToStdout) { - const object = JSON.parse(notForwardedContent); - const output = object[0].output; - process.stdout.write(output); + if(!fixToStdout) { + return; } + + const object = JSON.parse(content); + + const output = object[0].output || object[0].source; + + process.stdout.write(output); }) .on('error', async (err) => { if (err['code'] === 'ECONNREFUSED') {