1

I'm writing a trigger on a Cloudant database that should convert each new document into a Slack notification.

I've created a sequence of two actions: one to prepare the Slack message, one to send it. To send the Slack message I'm using the package action provided by IBM Bluemix OpenWhisk.

Cloudant changes feed --> Prepare Text --> Slack Post --> response

As the triggers send me all Cloudant events (new/modified/deleted documents), how can I only forward to Slack the NEW document event and ignore things like deleted documents.

Frederic Lavigne
  • 704
  • 3
  • 12

1 Answers1

2

For synchronous processing, simply return an error in our action

function main(doc) {
  if (doc._deleted) {
    // ignore deleted documents
    return { error: "ignoring deleted doc" };
  } else {
    // prepare the text for the Slack post action
    const slackMessage = ...
    return { text: slackMessage };
  }
}

return new Error("ignoring deleted doc") works too.

Or using the Promise object, one can call reject(reason) to interrupt the flow of a sequence.

function main(doc) {
  return new Promise((resolve, reject) => {
    if (doc._deleted) {
      // ignore deleted documents
      reject({ error: "ignoring deleted doc" });
    } else {
      // prepare the text for the Slack post action
      const slackMessage = ...
      resolve({ text: slackMessage });
    }
  };
}

The call to reject will stop the sequence flow. Any of reject('interrupted!'), reject(new Error('interrupted!'), reject({ error: 'interrupted!' }).

Warning: reject() will not work. You must provide a reason to reject.

Frederic Lavigne
  • 704
  • 3
  • 12