0

Workspace or Email error handler

by
Published Aug 7, 2025
Scriptยท failure windmill
  • Submitted by dieriba.pro916 Bun
    Created 321 days ago
    1
    //native
    2
    export async function main(
    3
      email_recipients: string[], 
    4
      workspace_id: string,
    5
      job_id: string, // The UUID of the job that errored
    6
      path: string, // The path of the script or flow that errored
    7
      is_flow: boolean, // Whether the runnable is a flow
    8
      error: object, // The error details
    9
      started_at: string, // The start datetime of the latest job that failed
    10
      trigger_path?: string, // The path of the trigger that errored in the format <trigger_type>/<trigger_path>
    11
    ) {
    12
    
    
    13
      const token = process.env["WM_TOKEN"]
    14
      const base_internal_url = process.env["BASE_INTERNAL_URL"]
    15
      const fullUrl = `${base_internal_url}/api/w/${workspace_id}/jobs/send_email_with_instance_smtp`
    16
      const body = { workspace_id, job_id, is_flow, error, started_at, runnable_path: path, trigger_path, email_recipients }
    17
      try {
    18
        console.log(`Sending request to ${fullUrl}`)
    19
        let response = await fetch(fullUrl, {
    20
          method: "POST",
    21
          headers: {
    22
            "Content-Type": "application/json",
    23
            "Authorization": `Bearer ${token}`
    24
          },
    25
          body: JSON.stringify(body)
    26
        })
    27
    
    
    28
        if (!response.ok) {
    29
          throw Error(await response.text())
    30
        }
    31
    
    
    32
        response = await response.json()
    33
        console.log(`Response: ${response}`)
    34
        return response
    35
      } catch (error) {
    36
        console.error(`Error: ${error}`)
    37
      }
    38
    }