| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | // Detect uncaught JavaScript errors and write them to the DOM in dev mode |
| b69ab31 | | | 9 | if (import.meta.hot) { |
| b69ab31 | | | 10 | let errorContainer: HTMLDivElement | null = null; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | const createErrorContainer = () => { |
| b69ab31 | | | 13 | if (!errorContainer) { |
| b69ab31 | | | 14 | errorContainer = document.createElement('div'); |
| b69ab31 | | | 15 | errorContainer.id = 'uncaught-error-display'; |
| b69ab31 | | | 16 | errorContainer.style.cssText = ` |
| b69ab31 | | | 17 | position: fixed; |
| b69ab31 | | | 18 | bottom: 0; |
| b69ab31 | | | 19 | left: 0; |
| b69ab31 | | | 20 | right: 0; |
| b69ab31 | | | 21 | max-height: 300px; |
| b69ab31 | | | 22 | background-color: rgba(220, 38, 38, 0.95); |
| b69ab31 | | | 23 | color: white; |
| b69ab31 | | | 24 | font-family: monospace; |
| b69ab31 | | | 25 | font-size: 12px; |
| b69ab31 | | | 26 | z-index: 999999; |
| b69ab31 | | | 27 | border-top: 3px solid #991b1b; |
| b69ab31 | | | 28 | display: flex; |
| b69ab31 | | | 29 | flex-direction: column; |
| b69ab31 | | | 30 | `; |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | // Create header row |
| b69ab31 | | | 33 | const header = document.createElement('div'); |
| b69ab31 | | | 34 | header.style.cssText = ` |
| b69ab31 | | | 35 | display: flex; |
| b69ab31 | | | 36 | justify-content: space-between; |
| b69ab31 | | | 37 | align-items: center; |
| b69ab31 | | | 38 | padding: 12px 16px; |
| b69ab31 | | | 39 | border-bottom: 1px solid rgba(255, 255, 255, 0.3); |
| b69ab31 | | | 40 | font-weight: bold; |
| b69ab31 | | | 41 | `; |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | const title = document.createElement('span'); |
| b69ab31 | | | 44 | title.textContent = 'Uncaught Webview Errors (development mode only)'; |
| b69ab31 | | | 45 | header.appendChild(title); |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | const closeButton = document.createElement('button'); |
| b69ab31 | | | 48 | closeButton.textContent = '×'; |
| b69ab31 | | | 49 | closeButton.style.cssText = ` |
| b69ab31 | | | 50 | background: none; |
| b69ab31 | | | 51 | border: none; |
| b69ab31 | | | 52 | color: white; |
| b69ab31 | | | 53 | font-size: 24px; |
| b69ab31 | | | 54 | line-height: 1; |
| b69ab31 | | | 55 | cursor: pointer; |
| b69ab31 | | | 56 | padding: 0; |
| b69ab31 | | | 57 | width: 24px; |
| b69ab31 | | | 58 | height: 24px; |
| b69ab31 | | | 59 | display: flex; |
| b69ab31 | | | 60 | align-items: center; |
| b69ab31 | | | 61 | justify-content: center; |
| b69ab31 | | | 62 | `; |
| b69ab31 | | | 63 | closeButton.onclick = () => { |
| b69ab31 | | | 64 | if (errorContainer) { |
| b69ab31 | | | 65 | errorContainer.style.display = 'none'; |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | }; |
| b69ab31 | | | 68 | header.appendChild(closeButton); |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | // Create errors content container |
| b69ab31 | | | 71 | const errorsContent = document.createElement('div'); |
| b69ab31 | | | 72 | errorsContent.id = 'uncaught-error-display-content'; |
| b69ab31 | | | 73 | errorsContent.style.cssText = ` |
| b69ab31 | | | 74 | overflow-y: auto; |
| b69ab31 | | | 75 | padding: 16px; |
| b69ab31 | | | 76 | flex: 1; |
| b69ab31 | | | 77 | `; |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | errorContainer.appendChild(header); |
| b69ab31 | | | 80 | errorContainer.appendChild(errorsContent); |
| b69ab31 | | | 81 | document.body.appendChild(errorContainer); |
| b69ab31 | | | 82 | } |
| b69ab31 | | | 83 | return errorContainer; |
| b69ab31 | | | 84 | }; |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | const displayError = ( |
| b69ab31 | | | 87 | message: string, |
| b69ab31 | | | 88 | source?: string, |
| b69ab31 | | | 89 | lineno?: number, |
| b69ab31 | | | 90 | colno?: number, |
| b69ab31 | | | 91 | error?: Error, |
| b69ab31 | | | 92 | ) => { |
| b69ab31 | | | 93 | const container = createErrorContainer(); |
| b69ab31 | | | 94 | const errorsContent = container.querySelector('#uncaught-error-display-content'); |
| b69ab31 | | | 95 | if (!errorsContent) { |
| b69ab31 | | | 96 | return; |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | // Show the container if it was hidden |
| b69ab31 | | | 100 | container.style.display = 'flex'; |
| b69ab31 | | | 101 | |
| b69ab31 | | | 102 | const errorEntry = document.createElement('div'); |
| b69ab31 | | | 103 | errorEntry.style.cssText = ` |
| b69ab31 | | | 104 | margin-bottom: 12px; |
| b69ab31 | | | 105 | padding-bottom: 12px; |
| b69ab31 | | | 106 | border-bottom: 1px solid rgba(255, 255, 255, 0.3); |
| b69ab31 | | | 107 | `; |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | const timestamp = new Date().toLocaleTimeString(); |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | // Create header line |
| b69ab31 | | | 112 | const header = document.createElement('strong'); |
| b69ab31 | | | 113 | header.textContent = `[${timestamp}] Uncaught Error:`; |
| b69ab31 | | | 114 | errorEntry.appendChild(header); |
| b69ab31 | | | 115 | errorEntry.appendChild(document.createElement('br')); |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | // Add error message |
| b69ab31 | | | 118 | const messageText = document.createTextNode(message); |
| b69ab31 | | | 119 | errorEntry.appendChild(messageText); |
| b69ab31 | | | 120 | errorEntry.appendChild(document.createElement('br')); |
| b69ab31 | | | 121 | |
| b69ab31 | | | 122 | // Add source location if available |
| b69ab31 | | | 123 | if (source) { |
| b69ab31 | | | 124 | const sourceSpan = document.createElement('span'); |
| b69ab31 | | | 125 | sourceSpan.style.opacity = '0.8'; |
| b69ab31 | | | 126 | sourceSpan.textContent = `at ${source}:${lineno}:${colno}`; |
| b69ab31 | | | 127 | errorEntry.appendChild(sourceSpan); |
| b69ab31 | | | 128 | errorEntry.appendChild(document.createElement('br')); |
| b69ab31 | | | 129 | } |
| b69ab31 | | | 130 | |
| b69ab31 | | | 131 | // Add stack trace if available |
| b69ab31 | | | 132 | if (error?.stack) { |
| b69ab31 | | | 133 | const stackPre = document.createElement('pre'); |
| b69ab31 | | | 134 | stackPre.style.cssText = |
| b69ab31 | | | 135 | 'margin-top: 8px; opacity: 0.9; white-space: pre-wrap; word-break: break-all;'; |
| b69ab31 | | | 136 | stackPre.textContent = error.stack; |
| b69ab31 | | | 137 | errorEntry.appendChild(stackPre); |
| b69ab31 | | | 138 | } |
| b69ab31 | | | 139 | |
| b69ab31 | | | 140 | errorsContent.insertBefore(errorEntry, errorsContent.firstChild); |
| b69ab31 | | | 141 | }; |
| b69ab31 | | | 142 | |
| b69ab31 | | | 143 | const handleError = (event: ErrorEvent) => { |
| b69ab31 | | | 144 | displayError(event.message, event.filename, event.lineno, event.colno, event.error); |
| b69ab31 | | | 145 | }; |
| b69ab31 | | | 146 | |
| b69ab31 | | | 147 | const handleUnhandledRejection = (event: PromiseRejectionEvent) => { |
| b69ab31 | | | 148 | const error = event.reason; |
| b69ab31 | | | 149 | let message = 'Unhandled Promise Rejection'; |
| b69ab31 | | | 150 | let errorObj: Error | undefined; |
| b69ab31 | | | 151 | |
| b69ab31 | | | 152 | if (error instanceof Error) { |
| b69ab31 | | | 153 | message = error.message; |
| b69ab31 | | | 154 | errorObj = error; |
| b69ab31 | | | 155 | } else if (typeof error === 'string') { |
| b69ab31 | | | 156 | message = error; |
| b69ab31 | | | 157 | } else if (error != null) { |
| b69ab31 | | | 158 | message = String(error); |
| b69ab31 | | | 159 | } |
| b69ab31 | | | 160 | |
| b69ab31 | | | 161 | displayError(`Promise: ${message}`, undefined, undefined, undefined, errorObj); |
| b69ab31 | | | 162 | }; |
| b69ab31 | | | 163 | |
| b69ab31 | | | 164 | window.addEventListener('error', handleError); |
| b69ab31 | | | 165 | window.addEventListener('unhandledrejection', handleUnhandledRejection); |
| b69ab31 | | | 166 | |
| b69ab31 | | | 167 | // TODO: we should probably call this on hot reload somehow |
| b69ab31 | | | 168 | const _dispose = () => { |
| b69ab31 | | | 169 | window.removeEventListener('error', handleError); |
| b69ab31 | | | 170 | window.removeEventListener('unhandledrejection', handleUnhandledRejection); |
| b69ab31 | | | 171 | errorContainer?.remove(); |
| b69ab31 | | | 172 | errorContainer = null; |
| b69ab31 | | | 173 | }; |
| b69ab31 | | | 174 | } |