Creating Error Workflows in n8n
Tags: tutorial, twilio, nocode, errors
Published: 2020-05-14 10:42:52

---
title: Creating Error Workflows in n8n
published: true
date: 2020-05-07 08:16:01 UTC
tags: tutorial,twilio,nocode,errorhandling
canonical_url: https://n8n.io/blog/creating-error-workflows-in-n8n/
---

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](https://n8n.io) 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](https://slack.com)/[Mattermost\(https://mattermost.com) notification to alert you about the error.

![](https://cdn-images-1.medium.com/max/1024/1*E7fbLnTdVkVCzelSfTXRRg.png)<figcaption>An error workflow in n8n</figcaption>

In this tutorial, I’ll teach you how to create an error workflow and use the [*Error Trigger*](https://docs.n8n.io/nodes/n8n-nodes-base.errorTrigger/) node. We’ll create two workflows (these workflows were built using n8n@0.111.0) in this tutorial to learn about error workflows:

1. **Workflow 1:** This will be an error workflow with an *Error Trigger*, a [*Mattermost*](https://docs.n8n.io/nodes/n8n-nodes-base.mattermost/) node, and a [*Twilio*](https://docs.n8n.io/nodes/n8n-nodes-base.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](https://docs.n8n.io/#/setup). Alternatively, you can sign-up for [n8n.cloud](https://n8n.cloud) to get access to our hosted service.

## 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](https://n8n.io/workflows/359) 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`

If you're using n8n.cloud, click on the ***Open*** button on your dashboard.

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* node. Once a workflow fails, this node automagically gets details about the failed workflow and the error workflow gets triggered.

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

{% youtube nAPFyfua2AQ %}

### 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](https://docs.n8n.io/credentials/mattermost/) 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 '{{$json["workflow"]["name"]}}' with the ID {{$json["workflow"]["id"]}} has encountered an error. The last node that was executed was {{$json["execution"]["lastNodeExecuted"]}}.
```

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

{% youtube zHHc9gKAJQQ %}

### 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](https://www.twilio.com/) account. You can create a free trial account and once you have done that, go to the [console](https://www.twilio.com/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.

![](https://cdn-images-1.medium.com/max/706/1*4tJ4CdPVQxd4-pVitwpguA.jpeg)<figcaption>Error notification SMS from the Twilio node</figcaption>

Congratulations on creating your first error workflow! 🎉

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

{% youtube 4Ug8tcKBI58 %}

## 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*](https://docs.n8n.io/nodes/n8n-nodes-base.cron/) node as the Trigger and a [*Function*](https://docs.n8n.io/nodes/n8n-nodes-base.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 dialogue 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 video of me following the steps mentioned above.

{% youtube Ng6ZVc-d2KQ %}

### 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.

![](https://cdn-images-1.medium.com/max/1024/1*koYE6DpmRk7-hiV5-dKoHA.png)<figcaption>Monitoring the workflow executions</figcaption>

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.

![](https://cdn-images-1.medium.com/max/1024/1*wdRINmrElYcBZzfLKMgExA.png)<figcaption>Workflow error notification in the Mattermost Incidents channel</figcaption>

![](https://cdn-images-1.medium.com/max/697/1*2J_UZXTUQ3YzPABTQNaJsg.jpeg)<figcaption>Workflow error notification sent as an SMS</figcaption>

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 video of me following the steps mentioned above.

{% youtube m6gZdwq0eG0 %}

## 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](https://n8n.io/workflows) 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](https://twitter.com/tanay1337) or ask for help on our [forum](https://community.n8n.io/) 💙

> This [post](https://n8n.io/blog/creating-error-workflows-in-n8n/) originally appeared on n8n.io [blog](https://n8n.io/blog).

* * *