{"record":{"id":"cf2177a00353f955","repo":"different-ai/openwork","slug":"mcp-app-document-runtime-error","errorCode":"MCP_APP_DOCUMENT_RUNTIME_ERROR","errorMessage":"stage + \": \" + safeMessage(value)","messagePattern":"stage \\+ \": \" \\+ safeMessage\\(value\\)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ee/apps/den-api/src/generated-artifact-view-builder.ts","lineNumber":187,"sourceCode":"        loader: \"js\",\n        resolveDir: process.cwd(),\n      }))\n    },\n  }\n}\n\nconst GENERATED_ARTIFACT_RUNTIME_REPORTER = `\n(() => {\n  const safeMessage = (value) => {\n    if (value instanceof Error) return value.message.slice(0, 1000);\n    if (typeof value === \"string\") return value.slice(0, 1000);\n    return \"The generated Artifact application failed at runtime.\";\n  };\n  const report = (stage, value) => {\n    window.parent.postMessage({\n      method: \"ui/notifications/sandbox-diagnostic\",\n      params: {\n        code: \"MCP_APP_DOCUMENT_RUNTIME_ERROR\",\n        message: stage + \": \" + safeMessage(value),\n      },\n    }, \"*\");\n  };\n  window.__openworkReportArtifactRuntimeError = report;\n  window.addEventListener(\"error\", (event) => report(\"document-error\", event.error || event.message));\n  window.addEventListener(\"unhandledrejection\", (event) => report(\"unhandled-rejection\", event.reason));\n})();\n`.trim().replace(/<\\/script/giu, \"<\\\\/script\")\n\nasync function buildClientBundle(reactSource: string): Promise<string> {\n  const entry = `\n    import React from \"react\";\n    import { createRoot } from \"react-dom/client\";\n    import { App, PostMessageTransport } from \"@modelcontextprotocol/ext-apps\";\n    const ArtifactView = React.lazy(() => import(\"artifact:view\"));\n    const mount = document.getElementById(\"openwork-artifact-view-root\");\n    const reportRuntimeError = (stage, error) => {","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/ee/apps/den-api/src/generated-artifact-view-builder.ts#L169-L205","documentation":"This error originates inside the generated Artifact application document sandbox: the report helper posts a ui/notifications/sandbox-diagnostic message with code MCP_APP_DOCUMENT_RUNTIME_ERROR to the parent window whenever the generated app throws at runtime. The message text is built as stage + ': ' + safeMessage(value), where stage identifies where it fired (e.g. 'document-error' from the window error listener). It means the generated interactive view's document-level JavaScript failed while executing.","triggerScenarios":"An uncaught exception in the generated artifact document (window 'error' event), or code explicitly calling window.__openworkReportArtifactRuntimeError(stage, value): runtime errors in generated render code, undefined references, failed fetches inside the sandbox, errors during initial document evaluation.","commonSituations":"Generated app code referencing data that turned out null/undefined; schema drift between the artifact generator output and runtime APIs; unhandled promise rejections in the sandbox; syntax-valid but logically broken generated code hitting an edge case with real data.","solutions":["Read the stage prefix and message in the sandbox-diagnostic notification to locate where the document threw.","Fix the generated artifact code at the reported stage (guard null/undefined values, wrap async work in try/catch).","Regenerate the artifact and confirm the document loads without a document-error report.","If generated by a model, harden the generation prompt/template against the specific runtime failure."],"exampleFix":"// before: unguarded access in generated doc\nrender(data.items[0].title)\n// after: defensive access\nconst title = data?.items?.[0]?.title ?? ''; render(title)","handlingStrategy":"try-catch","validationCode":"window.addEventListener('error', (event) => report('document-error', event.error || event.message));\nwindow.addEventListener('unhandledrejection', (event) => report('unhandled-rejection', event.reason));","typeGuard":"function hasMessage(v: unknown): v is { message: string } {\n  return typeof v === 'object' && v !== null && typeof (v as { message?: unknown }).message === 'string';\n}","tryCatchPattern":"try { render(data); } catch (err) {\n  window.__openworkReportArtifactRuntimeError?.('render', err);\n  showFallbackView();\n}","preventionTips":["Wrap all async generated code in try/catch and report failures with a stage label","Guard optional data access (?. and defaults) in generated artifact code","Test generated artifacts with empty/partial data before shipping","Listen for both error and unhandledrejection in sandbox documents"],"tags":["sandbox","runtime-error","artifact","javascript"],"backgroundTag":"runtime-error-in-sandbox","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}