Log response when JSON parsing fails
Problem
Users running commands through the wrapper have been reporting errors such as
node-flyway:FlywayExecutable Received response from Flyway CLI: +0ms
node-flyway:ConvertJsonToResponse SyntaxError: Unexpected end of JSON input
node-flyway:ConvertJsonToResponse at JSON.parse (<anonymous>)
node-flyway:ConvertJsonToResponse at ConvertJsonToResponse.toFlywayResponse
However, with no visibility into the unparseable response, it's difficult to troubleshoot.
Proposed solution
Add special handling for caught syntax errors so the raw response can be logged.
https://github.com/domdinnes/node-flyway/blob/e7aea49b754adbf84782713dd383ef94dee354f5/src/response/json-to-response.ts#L20-L29
Only the first attempt of JSON.parse would need special handling since invalid json would throw at this point.
try {
const error: any = cast(JSON.parse(json), toReference("FlywayErrorWrapper"));
if(error.error) {
response.error = error.error;
}
}
catch (err) {
ConvertJsonToResponse.logger.log(err);
if (err instanceof SyntaxError) {
ConvertJsonToResponse.logger.log(json);
}
}
@domdinnes I'm happy to open a PR for this one as well, if you're amenable.
@cadam11 thank you! I also noticed some issues with parsing error responses but haven't got round to raising a formal issue. You're welcome to make this change.
@domdinnes PR is up– do you have a specific schedule for publishing updates to NPM?