Best practices

(A)wait your actions

All actions return a promise.
Wait until the promise resolves, otherwise your hook won't work as expected.

You can use await because Cloudhooks is ES2016 compatible:

module.exports = async function (payload, actions) {
	const { data } = await actions.http.get('https://cloudhooks.dev');
}

Debug with log entries

If something goes wrong the hook, log entries can help you debug it.
Use console.log() and console.error() to add log and error entries, respectively.

Here is a hook example with logging:

module.exports = async function (payload, actions) {
  const { data } = await actions.http.get('https://cloudhooks.dev');

  if (data.length > 50*1024) {
 		console.log('Downloaded data is more than 50kb!');
  }
}

This is the output of the test run:

Use try/catch to handle errors

Actions might fail silently if you don't explicitly handle errors.
Catch errors by using try/catch in your hook code.

This example tries to download from a non-existent website:

module.exports = async function (payload, actions) {
  try {
    const { data } = await actions.http.get('https://no-such-website.dev');
  } catch (err) {
    console.error('Error occured: ', err)
  }
}

Here is the result of the test run:

On this page