question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.catch doesn't trigger for .remote

See original GitHub issue

I have an issue where I have a task to run database migrations via knex that is failing, but shipit just keeps running and never triggers the catch block.

  shipit.blTask('migrations', async () => {
    return shipit
      .remote(`npx knex migrate:latest`, {
        cwd: `${shipit.releasePath}/build`,
      })
      .catch(e => {
        console.log(e);
        process.exit();
      });
  });

If I console log inside .then() I see:

 [ { child: [ChildProcess],
       stdout:
        '\n> @ migrations:latest /var/www/api/releases/20180926144638/build\n> npx knex migrate:latest\n\nKnex:warning - migration file "20180926133945_TEST_THIS_WILL_FAIL.js" failed\nKnex:warning - migration failed with error: undefined\n',
       stderr:
        'Migration failed\nnpm ERR! code ELIFECYCLE\nnpm ERR! errno 1\nnpm ERR! @ migrations:latest: `npx knex migrate:latest`\nnpm ERR! Exit status 1\nnpm ERR! \nnpm ERR! Failed at the @ migrations:latest script.\nnpm ERR! This is probably not a problem with npm. There is likely additional logging output above.\n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR!     /home/pemmy/.npm/_logs/2018-09-26T14_47_29_617Z-debug.log\n' } ] }
ChildProcess {
  _events:
   { close: [Function: exithandler],
     error: [Function: errorhandler] },
  _eventsCount: 2,
  _maxListeners: undefined,
  _closesNeeded: 3,
  _closesGot: 3,
  connected: false,
  signalCode: null,
  exitCode: 0,
...truncated

Not only does it not trigger the catch, but it shows an exit code of 0, which I assume is the exit code of the ssh command, not the command on the remote server.

The command on the remote server returns an exit code of 1.

How can I get this to trigger the .catch block so I can exit when migrations fail?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
utkarsh-dixitcommented, Jul 9, 2019

If a callback function is provided, it is called with the arguments (error, stdout, stderr). On success, error will be null. On error, error will be an instance of Error. The error.code property will be the exit code of the child process while error.signal will be set to the signal that terminated the process. Any exit code other than 0 is considered to be an error.

The stdout and stderr arguments passed to the callback will contain the stdout and stderr output of the child process. By default, Node.js will decode the output as UTF-8 and pass strings to the callback. The encoding option can be used to specify the character encoding used to decode the stdout and stderr output. If encoding is ‘buffer’, or an unrecognized character encoding, Buffer objects will be passed to the callback instead.

@mrkrstphr As per child_process docs, the promise would be only rejected if there was some problem in the “child_process” module in not being able to start the command. If there is something wrong with the child process (in your case npx knex migrate:latest), it will return the error in as stderr. Inside the code make sure that, the stderr is empty string. Try the code below and it should work:-

  shipit.blTask('migrations', async () => {
    return shipit
      .remote(`npx knex migrate:latest`, {
        cwd: `${shipit.releasePath}/build`,
      }).then(result=>{
        if(result.stderr !== ""){
          console.log(result.stderr);
          process.exit();
        }
      });
  });
1reaction
jfillacommented, Mar 4, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

powershell - Try/catch does not seem to have an effect
I was able to duplicate your result when trying to run a remote WMI query. The exception thrown is not caught by the...
Read more >
try catch doesn't work with Exchange cmdlets in a remote ...
Yes, I did take care to add the -ErrorAction Stop so the cmdlet should stop in case of any errors and try catch...
Read more >
Linked server error not caught by TRY-CATCH
The solution is to SET XACT_ABORT ON in the calling procedure before invoking the remote procedure. When XACT_ABORT is on the CATCH block...
Read more >
What TRY/CATCH Doesn't Handle - Brent Ozar Unlimited®
By having that xact_state check in the try block if somehow something does put the transaction in an uncommittable state and not trigger...
Read more >
Campaign does not get triggered by RemoteAction on some ...
Hi guys, i have an issue regarding triggering campaigns via Remote Actions. We have a scheduled Remote Action, that gets automatically ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found