HOW-TO

How to catch a 200 response that hides a failure in n8n

Published · Updated

Code

js
// n8n Code node, mode "Run Once for All Items".
// Place it right after the HTTP Request node you want to guard.
const REQUIRED_FIELDS = ['id'];          // fields every good response must contain
const ERROR_KEYS = ['error', 'errors'];  // body keys that mean failure even on HTTP 200
const STEP_NAME = 'Save lead to CRM';    // shown in the alert so you know where to look

function findProblem(json) {
  if (json === null || typeof json !== 'object') {
    return 'response body is not a JSON object';
  }
  for (const key of ERROR_KEYS) {
    const value = json[key];
    const isEmptyList = Array.isArray(value) && value.length === 0;
    if (value && !isEmptyList) {
      return `body contains "${key}": ${JSON.stringify(value).slice(0, 200)}`;
    }
  }
  if (json.success === false || json.ok === false) {
    return 'body reports success/ok = false';
  }
  for (const field of REQUIRED_FIELDS) {
    const value = json[field];
    if (value === undefined || value === null || value === '') {
      return `missing required field "${field}"`;
    }
  }
  return null;
}

function checkItems(items) {
  if (!Array.isArray(items) || items.length === 0) {
    throw new Error(`[${STEP_NAME}] silent failure: step returned 0 items`);
  }
  items.forEach((item, index) => {
    const problem = findProblem(item.json);
    if (problem) {
      throw new Error(`[${STEP_NAME}] silent failure on item ${index}: ${problem}`);
    }
  });
  return items;
}

// Inside n8n, $input exists and the checked items pass through unchanged.
if (typeof $input !== 'undefined') return checkItems($input.all());

Why it happens

n8n marks a node as successful when the HTTP call returns a 2xx status. Many APIs return 200 and put the real result in the body, for example {"error": "quota exceeded"} or an empty object. n8n does not read that body for you, so the workflow goes green while nothing was saved. The same happens when the node is set to continue on error, because the error becomes ordinary data.

Steps

  1. Open the workflow and add a Code node directly after the HTTP Request node.
  2. Set the mode to "Run Once for All Items" and paste the code.
  3. Set REQUIRED_FIELDS to fields a real success always returns, such as the new record's id.
  4. Set ERROR_KEYS to the keys your API uses for failures.
  5. Set STEP_NAME to a label that tells you which step broke.
  6. In the workflow settings, choose an error workflow that starts with an Error Trigger and sends the message to Slack, Telegram or email.
  7. Send a bad request once on purpose and confirm the alert arrives.

Pitfalls

  • If the HTTP Request node returns zero items, n8n skips the Code node. Turn on "Always Output Data" on the HTTP Request node so the check still runs and catches the empty item.
  • Error workflows run for production executions of an active workflow. A manual test run shows the error in the editor but sends no alert.
  • The error message includes up to 200 characters of the API's error value. Shorten or remove that part if the API echoes personal data.
  • One bad item stops the whole run. If you want the other items to continue, send bad items to a separate branch with an IF node instead of throwing.

Seen on: n8n Community ·

Related service: n8n, Make and Zapier workflow automation