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.
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:
You can find information on how to set up and start n8n in the documentation.
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.
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.
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.
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.
Congratulations on creating your first error workflow! 🎉
Here’s a GIF of me following the steps mentioned above.
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.
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.
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.
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.
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.
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 💙
Previously published at https://medium.com/n8n-io/creating-error-workflows-in-n8n-6e03c9ecbc0f