node-flyway icon indicating copy to clipboard operation
node-flyway copied to clipboard

Log response when JSON parsing fails

Open cadam11 opened this issue 1 year ago • 3 comments

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);
     } 
 } 

cadam11 avatar Aug 01 '24 16:08 cadam11

@domdinnes I'm happy to open a PR for this one as well, if you're amenable.

cadam11 avatar Aug 01 '24 16:08 cadam11

@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 avatar Aug 01 '24 18:08 domdinnes

@domdinnes PR is up– do you have a specific schedule for publishing updates to NPM?

cadam11 avatar Aug 08 '24 00:08 cadam11