HOW-TO

How to detect silent failures in n8n workflows

Published · Updated

Code

js
// verify-destination.js - Node 22, no dependencies
// Usage: node verify-destination.js <id> '<expected JSON>'
const DEST_URL = "https://example.com/api/orders"; // read endpoint of the destination
const API_KEY = process.env.DEST_API_KEY || "YOUR_API_KEY";

// Read the record from the destination. Returns null when it does not exist.
async function readBack(id) {
  const res = await fetch(`${DEST_URL}/${encodeURIComponent(id)}`, {
    headers: { Authorization: `Bearer ${API_KEY}` },
  });
  if (res.status === 404) return null;
  if (!res.ok) throw new Error(`Read-back failed: HTTP ${res.status}`);
  return res.json();
}

// List every expected field that is missing or different at the destination.
function diff(expected, actual) {
  const mismatches = [];
  for (const [field, want] of Object.entries(expected)) {
    const got = actual[field];
    if (got === undefined || String(got) !== String(want)) {
      mismatches.push({ field, expected: want, actual: got ?? null });
    }
  }
  return mismatches;
}

// Check the destination, retry a few times, never modify anything.
async function verifyAtDestination({ id, expected, read = readBack, attempts = 3, delayMs = 2000 }) {
  const record = { id, expected, actual: null, mismatches: [], attempts: 0, ok: false, checkedAt: null };
  for (let i = 1; i <= attempts; i++) {
    record.attempts = i;
    record.actual = await read(id);
    record.mismatches = record.actual
      ? diff(expected, record.actual)
      : [{ field: "*", expected: "record exists", actual: null }];
    if (record.mismatches.length === 0) break;
    if (i < attempts) await new Promise((resolve) => setTimeout(resolve, delayMs));
  }
  record.ok = record.mismatches.length === 0;
  record.checkedAt = new Date().toISOString();
  return record;
}

async function main() {
  const record = await verifyAtDestination({
    id: process.argv[2],
    expected: JSON.parse(process.argv[3] || "{}"),
  });
  console.log(JSON.stringify(record)); // one evidence line per run: store it
  if (!record.ok) process.exitCode = 1; // non-zero exit fails the calling step
}

if (require.main === module && process.argv[2]) {
  main().catch((err) => {
    console.error(err.message);
    process.exitCode = 1;
  });
}

Why it happens

An execution log records that each step received a response without an error. It does not record that the right data arrived. If a provider renames a field, narrows a permission or stops sending a webhook, the step can still return success. The workflow tool looks at itself, so it reports green while the sheet row or the payment is missing.

Steps

  1. In the workflow, keep the ID and the key fields you just sent, such as amount and status.
  2. After the write step, run the script with that ID and the expected fields as JSON.
  3. The script reads the record from the destination and compares each expected field.
  4. If the record is not there yet, it waits and tries again, up to three times.
  5. It prints one JSON line with what was expected, what was found and when.
  6. If anything differs, it exits with code 1, so the calling step fails and your error workflow can alert you.

On self-hosted n8n you can call it from an Execute Command node. You can also run it from a scheduler against the IDs of recent runs.

Pitfalls

  • Values are compared as text, so 49.9 and 49.90 count as different. Normalise numbers and dates before you compare.
  • Some destinations need a few seconds before a new record is readable. Raise attempts or delayMs instead of removing the check.
  • The script only reads. Do not add an automatic repair, because overwriting the record destroys the evidence of what went wrong.
  • Keep the API key in an environment variable or the n8n credential store, with read-only scope. The Execute Command node may be disabled by default and is not available on n8n Cloud.

Seen on: Make Community ·

Related service: n8n, Make and Zapier workflow automation