{"record":{"id":"28ebecfce2598bb7","repo":"d2lang/d2","slug":"response-error-message-28ebec","errorCode":null,"errorMessage":"response.error.message","messagePattern":"response\\.error\\.message","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"d2js/js/src/worker.node.js","lineNumber":34,"sourceCode":"\n    switch (type) {\n      case \"init\":\n        try {\n          if (isNode) {\n            loadScript(data.wasmExecContent);\n          }\n          d2 = await initWasm(data.wasm);\n          currentPort.postMessage({ type: \"ready\" });\n        } catch (err) {\n          currentPort.postMessage({ type: \"error\", error: err.message });\n        }\n        break;\n\n      case \"compile\":\n        try {\n          const result = await d2.compile(JSON.stringify(data));\n          const response = JSON.parse(result);\n          if (response.error) throw new Error(response.error.message);\n          currentPort.postMessage({ id, type: \"result\", data: response.data });\n        } catch (err) {\n          currentPort.postMessage({ id, type: \"error\", error: err.message });\n        }\n        break;\n\n      case \"render\":\n        try {\n          const result = await d2.render(JSON.stringify(data));\n          const response = JSON.parse(result);\n          if (response.error) throw new Error(response.error.message);\n          const decoded = new TextDecoder().decode(\n            Uint8Array.from(atob(response.data), (c) => c.charCodeAt(0))\n          );\n          currentPort.postMessage({ id, type: \"result\", data: decoded });\n        } catch (err) {\n          currentPort.postMessage({ id, type: \"error\", error: err.message });\n        }","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/d2js/js/src/worker.node.js#L16-L52","documentation":"In the Node worker's handleMessage, the 'compile' case calls await d2.compile(JSON.stringify(data)) and, if the JSON envelope returned includes an `error` field, throws new Error(response.error.message). This surfaces a D2 compilation failure (invalid diagram syntax) as a worker 'error' response to the main thread.","triggerScenarios":"Posting {type:'compile'} with a D2 document that fails compilation — syntax errors, unknown shape/icon keywords, invalid connections, or a payload not shaped as d2.compile expects.","commonSituations":"Generating D2 from templates and emitting invalid syntax; using keywords/newer syntax unsupported by the installed d2 WASM; passing an object where the compiled API expected JSON.stringify'd input fields.","solutions":["Read err.message in the main-thread 'error' response — it contains the D2 compiler's line/column diagnostics; fix the document.","Validate the D2 syntax with the d2 CLI (d2 validate) before compiling in the worker.","Confirm the request payload structure matches what d2.compile expects (JSON.stringify(data) contract).","Check d2 WASM version compatibility with the syntax you are using; upgrade d2js if using newer language features."],"exampleFix":"// before\nworker.postMessage({ id, type: \"compile\", data: { d2: generated } });\n// after\n// fix the source indicated in err.message, e.g.:\n//   err.message === \"...: unclosed block\" -> close the block in `generated`\nconst fixed = closeBlocks(generated);\nworker.postMessage({ id, type: \"compile\", data: { d2: fixed } });","handlingStrategy":"validation","validationCode":"// pre-validate the D2 document before compiling\nconstbalancedBlocks = (doc) => {\n  let n = 0;\n  for (const ch of doc) if (ch === \"{\") n++; else if (ch === \"}\") n--;\n  return n === 0;\n};\nif (typeof doc !== \"string\" || !doc.trim() || !balancedBlocks(doc)) {\n  throw new TypeError(\"compile requires a non-empty, structurally valid D2 document\");\n}","typeGuard":"function isCompileRequest(data) {\n  return data !== null && typeof data === \"object\" &&\n    typeof data.d2 === \"string\" && data.d2.trim().length > 0;\n}","tryCatchPattern":"worker.onmessage = (e) => {\n  if (e.data.type === \"error\") {\n    // e.data.error holds d2 compile diagnostics incl. line/col\n    console.error(\"D2 compile failed:\", e.data.error);\n    return;\n  }\n  // e.data.data = compiled result\n};","preventionTips":["Run `d2 validate` (CLI/CI) on generated documents before compiling in the worker.","Handle the error response and surface the line/column diagnostics to users.","Keep d2js and the d2 WASM upgraded together so new syntax is supported.","Escape or sanitize dynamic values interpolated into D2 documents."],"tags":["worker","compile","d2-syntax","error-propagation"],"backgroundTag":"d2-compile-failed","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}