Console.log and throw error in callbacks

Hi,

I’m trying to implement logging and erroring when a remote call fails through callbacks like so.

    client.send(command).then(data => {
        console.log('[Success]' + data);
    }).catch(error => {
        console.log('[Error]' + JSON.stringify(error, null));
        throw new Error('User ' + JSON.stringify(event.user) + 'error: ' + error);
    });

But neither my logs nor my errors are being picked up by the action. Am I doing this incorrectly? This same code works in my local IDE and is able to print out the message. I am using the testing feature the action editor has.

Given actions are async/await-based (as opposed to callback-based), you may need to try something like:

try {
  const data = await client.send(command);
  console.log('[Success]' + data);
} catch (error) {
  console.log('[Error]' + JSON.stringify(error, null));
  throw new Error('User ' + JSON.stringify(event.user) + 'error: ' + error);
}
2 Likes

that works, thanks. However I’m still curious why this code works in my IDE but not when deployed.

Thanks for helping on this one Thameera!

Glad to hear it worked.

I’m still curious why this code works in my IDE but not when deployed.

Callbacks don’t play well with actions. Note that the action function exits before the promise functions have time to execute in your original code. Using await you signal that the action needs to wait for the promises to finish.

2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.