Global Error Workflow
@danny
code:
data:privatelast updated:1 year ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 800,000+ developers using the Pipedream platform
steps.
trigger
Workflow Errors
Deploy to generate unique SDK code
This workflow runs on Pipedream's servers and is triggered by SDK requests.
steps.
filter_and_format_error_message
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
}
40
/*
   By default, this workflow sends at most one email per day, per error type,
   per workflow. The second time a specific error is raised from the same
   workflow in the same 24-hour period, no email will be sent. You can modify
   this behavior to apply any notification logic you'd like.
*/
import dateFormat from "dateformat"
import { differenceInHours } from 'date-fns'

const { code, msg, ts } = event.error
const { id, workflow_id, workflow_name } = event.original_context
this.workflowUrl = `https://pipedream.com/@/` + `${workflow_id}` + `?e=` + `${id}`

// $checkpoint allows us to save any JSON-serializable data in a workflow,
// reading it on subsequent invocations. Here, we save the last time we
// saw a specific error on a workflow, exiting if we've seen the error recently.
if ($checkpoint && $checkpoint[workflow_id] && $checkpoint[workflow_id][code]) {
  console.log(`We've seen error ${code} for workflow ${workflow_id} before. Checking the time of the last event.`)
  // Check if we've seen this error within the past 24 hours
  const lastErrorDate = new Date($checkpoint[workflow_id][code].lastSeen)
  const diffInHours = differenceInHours(new Date(ts), lastErrorDate)
  if (diffInHours <= 24) {
    $end(`Error ${code} occurred ${diffInHours} hours ago. No notification to send.`)
  }
}

// We haven't seen the error recently, so we can send our error notitication
const errorTime = dateFormat(event.original_context.ts, "h:MM:ss TT Z")
const errorDate = dateFormat(event.original_context.ts, "dddd, mmmm dS, yyyy")

const formattedErrMsg = msg ? `${code}${msg}` : code

this.subject = `[pipedream] Error in workflow "${workflow_name ?? workflow_id}" at ${errorTime}`
this.text = `Your workflow, "${workflow_name ?? workflow_id}," generated the following error at ${errorTime} on ${errorDate}:

${formattedErrMsg}

Link to workflow: ${this.workflowUrl}`
steps.
email_me
Customize and send an email to the email address you registered with Pipedream. The email will be sent by notifications@pipedream.com.
params
Subject
{{steps.filter_and_format_error_message.subject}}
string ·params.subject
Text
{{steps.filter_and_format_error_message.text}}
string ·params.text
Optional
code
async params => {
1
2
3
4
5
6
7
8
9
}
10
const options = {
  subject: params.subject,
  text: params.text,
}
if (params.html) {
  options.html = params.html
}
$send.email(options)
steps.
checkpoint
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
}
23
const merge = require('lodash.merge')

const { code, ts } = event.error
const { workflow_id } = event.original_context

// The first time our workflow runs, initialize $checkpoint
if (!$checkpoint) {
  console.log("First run of Global Error Workflow. Checkpointing initial state")
  $checkpoint = {} // workflow error gets set on merge below
}

// Finally, we need to checkpoint the time we saw this error for
// this workflow, merging it with the existing checkpoint data.
merge($checkpoint, {
  [workflow_id]: {
    [code]: {
      lastSeen: ts
    }
  }
})