Skip to content

Commit

Permalink
Fix CLI RPC status code and output (#228)
Browse files Browse the repository at this point in the history
* Fixed response code in cli-rpc.js

* Transfer STDERR output with cli-rpc.js

Co-authored-by: Herbert Scheffknecht <[email protected]>
  • Loading branch information
hscheffknecht and hscheffknecht authored Feb 9, 2022
1 parent 6ce886c commit d365ab0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions bin/standalone/cli-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,35 @@ class Dispatcher {
response.setHeader('Content-Type', 'text/plain');

let child;
let responseBuffer = '';

try {
child = spawn('bash', ['-c', requestBody]);
} catch (error) {
console.error(error);
response.write(error.message);
response.statusCode = 400;
response.write(error.message);
response.end();
return;
}

console.info(requestBody);
child.stdout.on('data', (chunk) => {
response.write(chunk.toString());
responseBuffer += chunk.toString();
});
child.stderr.on('data', (chunk) => {
responseBuffer += chunk.toString();
});
child.on('close', (code) => {
response.statusCode = code === 0 ? 200 : 400;
response.write(responseBuffer);
response.end();
});

child.on('error', (error) => {
console.error(error);
response.write(error.message);
response.statusCode = 400;
response.write(error.message);
response.end();
});
}
Expand All @@ -71,8 +77,8 @@ class Dispatcher {

if (error) {
response.setHeader('Content-Type', 'text/plain');
response.write(error.toString());
response.statusCode = 500;
response.write(error.toString());
response.end();
return;
}
Expand All @@ -82,8 +88,8 @@ class Dispatcher {
}

response.setHeader('Content-Type', 'text/yaml');
response.write(schemaContent);
response.statusCode = 200;
response.write(schemaContent);
response.end();
});
}
Expand Down

0 comments on commit d365ab0

Please sign in to comment.