loading...
n8n

Creating Error Workflows in n8n

tanay1337 profile image Tanay Pant Originally published at Medium on ・7 min read

Errors can always creep up while working with software. It’s important to get notified about it so that it can be timely fixed. n8n allows you to set an ‘Error Workflow’ for your workflows. When your workflow runs into an error, the error workflow is triggered. This workflow can do anything that other workflows can like send an SMS or Slack/Mattermost notification to alert you about the error.

An error workflow in n8n

In this tutorial, I’ll teach you how to create an error workflow and use the Error Trigger node. We’ll create two workflows in this tutorial to learn about error workflows:

  1. Workflow 1: This will be an error workflow with an Error Trigger, a Mattermost node, and a Twilio Node. This workflow will be triggered when Workflow 2 encounters an error. It will then send a notification about the error to a Mattermost channel and an SMS.
  2. Workflow 2: This will be a bare minimum two-node workflow which will encounter an error when it is run.

You can find information on how to set up and start n8n in the documentation.

Workflow 1: Creating an error workflow

In this workflow, we need to get the information of the workflow that has failed, notify us about it in the ‘Incidents’ channel on Mattermost, and send an SMS about it to the specified person using Twilio.

I have also submitted Workflow 1 on n8n.io, in case you’d like to skim through this workflow. Please note that you’ll still need to configure a couple of things like your Mattermost and Twilio credentials as well as the settings of the node.

1. Error Trigger: Start the error workflow

First of all, start n8n with the following command:

n8n start

In the Node Editor view, add a new node by clicking on the + button on the top right of the Editor UI. Select the Error Trigger node under the Triggers section. Now, click on the Execute Node button and it will generate an example result for you.

Now, save the workflow. As opposed to other workflows with a trigger node, you won’t have to activate the workflow when using the Error Trigger. Once a workflow fails, this node automagically gets details about the failed workflow and the error workflow gets triggered.

Here’s a GIF of me following the steps mentioned above.

Creating the Error Trigger node

2. Mattermost node: Inform the incidents channel about the error

Now, we want to post a message that informs the members of the ‘Incidents’ channel that the workflow has encountered an error.

Add a Mattermost node by clicking on the + button on the top right and selecting the Mattermost node. In the Node Editor, enter your Mattermost credentials. Here’s some detailed information on how to create an access token for the credentials. I have used an access token from a bot account, but you can also use the access token from your account.

Now we need to enter the Channel ID. Since this is not a dynamic piece of information (the Incidents channel would always be there and hence, the ID will remain the same), we need to grab its Channel ID.

If you don’t already have a channel like this for the tutorial, you can manually create a new channel on Mattermost. To get its ID, click on the down arrow next to the channel name and click on the View Info option. This will reveal the ID of the channel. You can then copy and paste that in the Channel ID field in the node.

Finally, we will enter the Message. Since this would be a dynamic piece of information, click on the gears icon next to the field and select Add Expression. I have entered the following expression using the Variable Selector.

The workflow named '{{$node["Error Trigger"].json["workflow"]["name"]}}' with the ID {{$node["Error Trigger"].json["workflow"]["id"]}} has encountered an error. The last node that was executed was {{$node["Error Trigger"].json["execution"]["lastNodeExecuted"]}}.

Click on the Execute Node button and that will send a message to the ‘Incidents’ channel on Mattermost. Here’s a GIF of me following the steps mentioned above.

Creating a Mattermost node to inform the Incidents channel about the error

3. Twilio node: Send an SMS about the error

As the last step of the error workflow, we want to send an SMS to the person responsible for the workflow. We’ll accomplish that with the help of the Twilio node.

First of all, you’ll have to create a Twilio account. You can create a free trial account and once you have done that, go to the console and create a Trial Number for yourself. On that page, you can also acquire the Account Sid and Auth Token that you’ll need for the credentials.

Once you have done that, add a Twilio node in n8n. Create new credentials for the Twilio API and enter the details. Now, enter the trial number that you created in the From field and your number (the one that you verified your Twilio account with) in the To field. In the message field, we want to include dynamic pieces of information. To do that click on the gears icon next to the Message field and select Add Expression. I entered the following in that field:

The workflow named '{{$node["Error Trigger"].json["workflow"]["name"]}}' with the ID {{$node["Error Trigger"].json["workflow"]["id"]}} has encountered an error.

Click on the Execute Node button and that will send a message to the number specified in the node. Here’s a screenshot of how that looked for me.

Error notification SMS from the Twilio node

Congratulations on creating your first error workflow! 🎉

Here’s a GIF of me following the steps mentioned above.

Creating a Twilio node to get an SMS about the error

Workflow 2: Creating a workflow with an error

Now, we need to create a flawed workflow which throws an error upon being activated. We’ll use a Cron node as the Trigger and a Function node which is wrongly configured and will give us an error.

1. Cron node: Trigger the workflow

In the Node Editor view, add a new node by clicking on the + button on the top right of the Editor UI. Select the Cron node under the Triggers section. Click on the Add Cron Time button and select ‘Every Minute’ as the Mode.

Now, save the workflow. Saving the workflow will enable you to set an ‘Error Workflow’. Go to the Workflow icon on the left side of the screen and click on Settings. In the dialog box, select the error workflow that you created above for the Error Workflow field and click on the Save button. This will assign the Workflow 1 as the error workflow which will be triggered when this workflow encounters an error during execution.

Here’s a GIF of me following the steps mentioned above.

Creating a Cron node to trigger the workflow and setting an error workflow

2. Function node: Wrongly configured node which gives an error

Last but not the least, we need to create a wrongly configured node that gives an error upon execution. For that, add the Function node and change items[0] to items[1]. Don’t click on the Execute Node button yet. If you get the error now, n8n will not allow you to activate the workflow.

Activate the workflow by clicking on the Activate toggle on the top right. Voila, our workflow with an error is now live (never thought I’d say this) and will run every minute. You can keep an eye on the executions by clicking on the Executions icon on the left side of the screen. For me, it looks like the following after the first execution.

Monitoring the workflow executions

In the image above, you can see how the Error Workflow got triggered once the Workflow 2 encountered an error.

Note: You might want to deactivate the workflow after a couple of Error Workflow executions (or deactivate the Twilio node) otherwise you might run out of your free Twilio credits soon 😉

This is how the notifications from the Mattermost and Twilio nodes looked like after the Error Workflow got executed.

Workflow error notification in the Mattermost Incidents channel

Workflow error notification sent as an SMS

In case you feel that this is an ideal error workflow for you, you can set this as the error workflow for other workflows too. You can do that by following the same steps that you followed in Workflow 2, Step 1.

Here’s a GIF of me following the steps mentioned above.

Creating a wrongly configured Function node and activating the workflow

Conclusion

Today we learned about the Error Trigger node as well as learned to create and assign an error workflow. This helps us to ensure that all our workflows are running well and if any of them encounter an error for some reason, we get notified about them as soon as possible to fix them.

I am curious to learn what kind of nodes you’d use for an error workflow. Would that tie in with your incident response playbook? I’d love to check out what you create. Please consider sharing your workflows with the community.

In case you’ve run into an issue while following the tutorial, feel free to reach out to me on Twitter or ask for help on our forum 💙


Posted on by:

tanay1337 profile

Tanay Pant

@tanay1337

Tanay is an author, speaker, developer and tech enthusiast.

n8n

Free, open and self-hostable fair-code licensed workflow automation tool.

Discussion

pic
Editor guide