Error reporter, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Error reporter

Use this task to get email reports when errors occur with events, tasks, and actions in Mechanic. Use this task out of the box, customize it, or borrow logic for your more advanced error reporting tasks.

Runs Occurs when an event fails, Occurs when a task fails, and Occurs when an action fails. Configuration includes email recipients.

15-day free trial – unlimited tasks

Documentation

Use this task to get email reports when errors occur with events, tasks, and actions in Mechanic. Use this task out of the box, customize it, or borrow logic for your more advanced error reporting tasks.

Read more about error events.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
mechanic/errors/event
mechanic/errors/task
mechanic/errors/action
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
email recipients (email, array, required)
Code
{% comment %}
  Only send error emails for the same errors at these thresholds.
{% endcomment %}

{% assign error_thresholds = array %}
{% assign error_thresholds[0] = 10 %}
{% assign error_thresholds[1] = 25 %}
{% assign error_thresholds[2] = 50 %}
{% assign error_thresholds[3] = 100 %}
{% assign error_thresholds[4] = 200 %}

{% comment %}
  Fingerprinting the error, so that we can track if this is something we have received recently.
  See: https://learn.mechanic.dev/techniques/debouncing-events#fingerprinting
{% endcomment %}

{% assign fingerprint_parts = hash %}
{% assign fingerprint_parts["topic"] = event.topic %}
{% assign fingerprint_parts["message"] = error.message %}
{% assign fingerprint_parts["task_id"] = error.task.id %}
{% assign fingerprint_parts["action_type"] = error.action.type %}
{% assign fingerprint = fingerprint_parts | json | sha256 %}
{% assign error_count_cache_key = "errors/" | append: fingerprint %}

{% comment %} How many times have we seen this error recently? {% endcomment %}

{% assign recent_error_count = cache[error_count_cache_key] | default: 0 %}

{% assign event_url = "https://admin.shopify.com/store/" | append: shop.myshopify_domain | replace: ".myshopify.com", "/apps/mechanic/events/" | append: error.event.id %}

{% capture email_subject -%}
  {{ event.topic }} on {{ shop.name }}
{%- endcapture %}

{% capture email_body %}
<p><b>This is an automated error notification from your Mechanic error reporting task.</b></p>

{% if error_thresholds contains recent_error_count -%}
  <p><b>Note:</b> There have been more than {{ recent_error_count }} errors of this type in the last 10 minutes.</p>
{%- endif %}

{% if event.preview %}
  <p>View error details</p>
{% else %}
  <p><a href="{{ event_url }}">View error details</a></p>
{% endif %}

<p>Error topic: {{ event.topic }}</p>

<p>Error message: <pre>{{ error.message }}</pre></p>

<p>Thanks,<br><br>{{ shop.name }} via Mechanic</p>
{% endcapture %}

{% comment %}
  We only send emails on the first error and again on specific thresholds
{% endcomment %}

{% if recent_error_count == 0 or error_thresholds contains recent_error_count %}
  {% action "email" %}
    {
      "to": {{ options.email_recipients__email_array_required | json  }},
      "subject": {{ email_subject | json }},
      "body": {{ email_body | json }},
      "reply_to": {{ shop.customer_email | json }},
      "from_display_name": {{ shop.name | json }}
    }
  {% endaction %}
{% endif %}

{% comment %}
  Increment recent error count for this error
{% endcomment %}

{% action "cache" %}
  {
    "incr": {
      "key": {{ fingerprint | json }},
      "ttl": 600
    }
  }
{% endaction %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more