{"record":{"id":"fc73b369db6f302a","repo":"d2lang/d2","slug":"response-error-message-fc73b3","errorCode":null,"errorMessage":"response.error.message","messagePattern":"response\\.error\\.message","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"d2js/js/src/worker.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.js#L16-L52","documentation":"In the Node worker (worker.js) compile case, d2.compile (WASM) returns a JSON string; a truthy response.error makes the worker throw Error(response.error.message), which is posted back to the main thread and rejects the caller's promise. This is the Node-side counterpart of the browser compile error and surfaces D2 compilation failures.","triggerScenarios":"Calling d2.compile() on a D2 instance in Node with a script that fails to compile: syntax errors, invalid references, unknown keywords, or invalid options in the request object.","commonSituations":"Server-side rendering of user-supplied D2 scripts with syntax errors; CI jobs compiling diagrams checked into a repo with typos; options incompatible with the bundled wasm D2 version.","solutions":["Fix the diagram source based on the error message (it includes compiler diagnostics)","Lint/validate D2 scripts in CI before rendering","Check d2.version() for compatibility with any newer syntax used","Wrap compile in try/catch and return a meaningful error to the caller"],"exampleFix":"// before\nawait d2.compile('a -> b ->'); // trailing incomplete connection\n// after\nawait d2.compile('a -> b');","handlingStrategy":"validation","validationCode":"if (typeof source !== 'string' || source.trim() === '') {\n  throw new TypeError('D2 source must be a non-empty string');\n}\nconst result = await d2.compile(source);","typeGuard":"function isCompileInput(input) {\n  return typeof input === 'string' ||\n    (input !== null && typeof input === 'object' && !Array.isArray(input));\n}","tryCatchPattern":"try {\n  const result = await d2.compile(source);\n} catch (err) {\n  // err.message contains the D2 compiler diagnostic from the wasm build\n  console.error('D2 compile failed (node):', err.message);\n  throw new SyntaxError('Invalid D2 script: ' + err.message);\n}","preventionTips":["Lint D2 files in CI before server-side compilation","Return the compiler diagnostic to API consumers instead of a generic 500","Pin the d2.wasm version; verify syntax compatibility after upgrades","Add unit tests compiling representative diagrams"],"tags":["compile-error","wasm","node","d2-syntax"],"backgroundTag":"d2-compile-error","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}