{"record":{"id":"82a6f730cc261cda","repo":"denoland/deno","slug":"err-invalid-arg-value-82a6f7","errorCode":"ERR_INVALID_ARG_VALUE","errorMessage":"The argument 'headers' is invalid. Received ${inspect(obj)}","messagePattern":"The argument 'headers' is invalid\\. Received (.+?)","errorType":"exception","errorClass":"NodeTypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_http_server.js","lineNumber":534,"sourceCode":"  if (typeof reason === \"string\") {\n    this.statusMessage = reason;\n  } else {\n    this.statusMessage ||= STATUS_CODES[statusCode] || \"unknown\";\n    obj ??= reason;\n  }\n  this.statusCode = statusCode;\n\n  // Enforce no body for 204 and 304 responses\n  if (statusCode === 204 || statusCode === 304) {\n    this._hasBody = false;\n  }\n\n  let headers;\n  if (this[kOutHeaders]) {\n    // Slow-case: progressive API and header fields are passed.\n    if (ArrayIsArray(obj)) {\n      if (obj.length % 2 !== 0) {\n        throw new ERR_INVALID_ARG_VALUE(\"headers\", obj);\n      }\n      for (let n = 0; n < obj.length; n += 2) {\n        const k = obj[n + 0];\n        if (k) this.removeHeader(k);\n      }\n      for (let n = 0; n < obj.length; n += 2) {\n        const k = obj[n + 0];\n        if (k) this.appendHeader(k, obj[n + 1]);\n      }\n    } else if (obj) {\n      const keys = ObjectKeys(obj);\n      for (let i = 0; i < keys.length; i++) {\n        const k = keys[i];\n        if (k) this.setHeader(k, obj[k]);\n      }\n    }\n    headers = this[kOutHeaders];\n  } else {","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/_http_server.js#L516-L552","documentation":"In writeHead's slow path - taken when setHeader/appendHeader were already used (this[kOutHeaders] is set) - an array argument must be a flat [name1, value1, name2, value2, ...] list. An odd-length array leaves a name without a value, so the polyfill throws ERR_INVALID_ARG_VALUE('headers', obj) exactly like Node.","triggerScenarios":"Mixing the progressive API (res.setHeader earlier) with writeHead(200, reason, ['Content-Type', 'text/html', 'X-Extra']) whose array has odd length; dynamically built pairs arrays where a key is pushed without its value.","commonSituations":"Conditionally appending header pairs with array.push(name) while forgetting the value; spreading a computed array that can drop entries; header builders whose schema drifted so a value can be undefined and skipped.","solutions":["Fix the array to even length with strict name/value pairs","Prefer the object form writeHead(200, { 'content-type': 'text/html' }) which cannot be odd","When the array is dynamic, assert headers.length % 2 === 0 before calling","Centralize header construction in one tested helper"],"exampleFix":"// before\nconst h = ['content-type', 'text/html'];\nif (cors) h.push('access-control-allow-origin'); // odd length -> throws\nres.writeHead(200, h);\n\n// after\nconst h = { 'content-type': 'text/html' };\nif (cors) h['access-control-allow-origin'] = '*';\nres.writeHead(200, h);","handlingStrategy":"validation","validationCode":"function validHeaderPairs(headers) {\n  if (!Array.isArray(headers)) return true;\n  return headers.length % 2 === 0;\n}\nif (validHeaderPairs(pairs)) {\n  res.writeHead(200, reason, pairs);\n}","typeGuard":"function isHeaderPairArray(v) {\n  return !Array.isArray(v) || v.length % 2 === 0;\n}","tryCatchPattern":"try {\n  res.writeHead(200, reason, pairs);\n} catch (e) {\n  if (e.code === 'ERR_INVALID_ARG_VALUE') {\n    res.writeHead(200, reason); // fall back to headers set via setHeader\n  } else throw e;\n}","preventionTips":["Prefer the object headers form whenever pairs are built dynamically","Push header names and values together in a single helper call","Assert even length in the header-builder's unit tests"],"tags":["http","validation","node-compat"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}