{"record":{"id":"93d334429845ccac","repo":"d2lang/d2","slug":"response-error-message","errorCode":null,"errorMessage":"response.error.message","messagePattern":"response\\.error\\.message","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"d2js/js/src/worker.browser.js","lineNumber":32,"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":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/d2js/js/src/worker.browser.js#L14-L50","documentation":"In the browser worker's handleMessage, the compile case calls the WASM d2.compile, JSON-parses the result, and if the response object contains an error field it throws new Error(response.error.message). This is how D2 surfaces compilation failures from Go/WASM (e.g., invalid D2 script) to the main thread, where they arrive as a rejected promise with that message.","triggerScenarios":"Calling d2.compile() (via new D2()) with D2 source that fails to compile: syntax errors, unknown shape/keyword, invalid connections, or bad options passed in the request object.","commonSituations":"Typos in D2 diagram script; using D2 syntax unsupported by the bundled wasm version; passing options with invalid values (e.g., unknown theme id or layout engine); programmatically generated scripts with malformed input.","solutions":["Read the thrown message — it is the D2 compiler error including line/position — and fix the diagram source","Validate/lint the D2 script before compiling (e.g., editor with D2 syntax support)","Pin/check the wasm D2 version (d2.version()) if the script uses newer syntax","Wrap compile in try/catch and surface the message to your users or logs"],"exampleFix":"// before\nawait d2.compile('a -> '); // invalid script, throws compiler error\n// after\nawait d2.compile('a -> b'); // valid D2 script","handlingStrategy":"validation","validationCode":"function isPlainD2Script(src) {\n  return typeof src === 'string' && src.trim().length > 0 && !/[\\u0000-\\u0008]/.test(src);\n}\nif (!isPlainD2Script(source)) throw new TypeError('D2 source must be a non-empty string');","typeGuard":"function isCompileRequest(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 (line/position)\n  console.error('D2 compile failed:', err.message);\n  throw new SyntaxError('Invalid D2 script: ' + err.message);\n}","preventionTips":["Use D2 syntax highlighting/linting in your editor before compiling","Validate user-submitted scripts and show the compiler message inline","Pin your d2.wasm version and check d2.version() when using new syntax","Keep generated scripts under tests with sample compile checks"],"tags":["compile-error","wasm","d2-syntax","worker"],"backgroundTag":"d2-compile-error","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}