Skip to content

feat: display JS runtime errors in live preview panel#119

Open
Swathi-Chippa wants to merge 1 commit into
Debmallya-03:mainfrom
Swathi-Chippa:feat/runtime-error-display
Open

feat: display JS runtime errors in live preview panel#119
Swathi-Chippa wants to merge 1 commit into
Debmallya-03:mainfrom
Swathi-Chippa:feat/runtime-error-display

Conversation

@Swathi-Chippa
Copy link
Copy Markdown

[Feature] Display JavaScript Runtime Errors in the Live Preview Panel

Closes

Closes #92


What This PR Does

Previously, when JavaScript code threw a runtime error inside the live preview iframe, the output would silently fail with no feedback to the user. This PR adds a non-intrusive error strip beneath the preview panel that catches and displays runtime errors in real time.


Changes Made

All changes are confined to a single file: app/page.tsx

Phase 1 — Iframe Error Interceptor

Injected a small error interceptor script at the very top of <head> inside the combinedCode string, in both the auto-preview useEffect and runCodeManually. It intercepts:

  • window.onerror — catches synchronous JS errors with message, line, and column
  • window.addEventListener('unhandledrejection', ...) — catches uncaught Promise rejections

Both post a WEBIFY_ERROR message to the parent via postMessage, reusing the existing message type already handled in the codebase.

Phase 2 — State & UI

  • Added runtimeError state (message, line, column) initialized to null
  • Extended the existing WEBIFY_ERROR listener to call setRuntimeError alongside the existing consoleErrors update
  • Added setRuntimeError(null) at the start of both recompile paths to clear stale errors on every new run
  • Rendered a minimal error strip directly after the </iframe> tag inside PreviewErrorBoundary

Phase 3 — Edge Cases

  • CORS errors: If the browser reports "Script error." with line: 0 (caused by CDN scripts without crossorigin), a helpful tip is shown beneath the error message
  • Dismiss button: A button on the right side of the strip lets users manually clear the error
  • Infinite loop: Already handled by the existing 5-second iframe watchdog — its timeout message surfaces naturally through the same WEBIFY_ERROR channel

How to Test

  1. Open the editor and write a JS error in the JS pane:

    foo() // ReferenceError: foo is not defined

    → Error strip should appear below the preview with the message and line number

  2. Write a rejected promise:

    Promise.reject(new Error("something went wrong"))

    → Error strip should show the rejection message

  3. Fix the error and click Run (or let auto-refresh trigger)
    → Error strip should disappear immediately

  4. Add an external script without crossorigin:

    <script src="https://some-cdn.com/lib.js"></script>

    → Error strip should show the CORS tip

  5. Click on the error strip → it should dismiss


What Was Not Changed

  • No new files created
  • Sandbox attribute on the iframe is untouched
  • HTML validation error path (srcdoc / createPreviewErrorHtml) is untouched
  • No existing state, hooks, or component structure was refactored

Screenshots

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 25, 2026

@Swathi-Chippa is attempting to deploy a commit to the debmallya-03's projects Team on Vercel.

A member of the Team first needs to authorize it.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 25, 2026

Deploy Preview for webifynet failed. Why did it fail? →

Name Link
🔨 Latest commit 3c8bb26
🔍 Latest deploy log https://app.netlify.com/projects/webifynet/deploys/6a151d41e06e1200083a0028

@Swathi-Chippa
Copy link
Copy Markdown
Author

Hey @Debmallya-03! Just opened this PR for the runtime error display feature — all changes in app/page.tsx, covers onerror, promise rejections, CORS errors, and auto-clears on recompile. Let me know if anything needs tweaking

@Swathi-Chippa Swathi-Chippa reopened this May 26, 2026
@Swathi-Chippa
Copy link
Copy Markdown
Author

Hi @Debmallya-03 — the conflicts are resolved and the branch is clean now. The Netlify and Vercel deploy checks are awaiting maintainer approval to run. Could you approve the workflows so the deploy preview can be verified? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature/Bug] Display JavaScript runtime errors from the live preview panel

2 participants