File size: 9,819 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
import { useMemo, useRef, Suspense, useCallback } from 'react'
import type { DebugInfo } from '../../shared/types'
import { Overlay, OverlayBackdrop } from '../components/overlay'
import { RuntimeError } from './runtime-error'
import { getErrorSource } from '../../../shared/lib/error-source'
import { HotlinkedText } from '../components/hot-linked-text'
import { PseudoHtmlDiff } from './runtime-error/component-stack-pseudo-html'
import {
ErrorOverlayLayout,
type ErrorOverlayLayoutProps,
} from '../components/errors/error-overlay-layout/error-overlay-layout'
import {
getHydrationErrorStackInfo,
isHydrationError,
NEXTJS_HYDRATION_ERROR_LINK,
} from '../../shared/react-19-hydration-error'
import type { ReadyRuntimeError } from '../utils/get-error-by-type'
import { useFrames } from '../utils/get-error-by-type'
import type { ErrorBaseProps } from '../components/errors/error-overlay/error-overlay'
import type { HydrationErrorState } from '../../shared/hydration-error'
import { useActiveRuntimeError } from '../hooks/use-active-runtime-error'
import { formatCodeFrame } from '../components/code-frame/parse-code-frame'
import stripAnsi from 'next/dist/compiled/strip-ansi'
export interface ErrorsProps extends ErrorBaseProps {
getSquashedHydrationErrorDetails: (error: Error) => HydrationErrorState | null
runtimeErrors: ReadyRuntimeError[]
debugInfo: DebugInfo
onClose: () => void
}
function isNextjsLink(text: string): boolean {
return text.startsWith('https://nextjs.org')
}
export function HydrationErrorDescription({ message }: { message: string }) {
return <HotlinkedText text={message} matcher={isNextjsLink} />
}
export function GenericErrorDescription({ error }: { error: Error }) {
const environmentName =
'environmentName' in error ? error.environmentName : ''
const envPrefix = environmentName ? `[ ${environmentName} ] ` : ''
// The environment name will be displayed as a label, so remove it
// from the message (e.g. "[ Server ] hello world" -> "hello world").
let message = error.message
if (message.startsWith(envPrefix)) {
message = message.slice(envPrefix.length)
}
return (
<>
<HotlinkedText text={message} matcher={isNextjsLink} />
</>
)
}
export function getErrorTypeLabel(
error: Error,
type: ReadyRuntimeError['type']
): ErrorOverlayLayoutProps['errorType'] {
if (type === 'recoverable') {
return `Recoverable ${error.name}`
}
if (type === 'console') {
return `Console ${error.name}`
}
return `Runtime ${error.name}`
}
const noErrorDetails = {
hydrationWarning: null,
notes: null,
reactOutputComponentDiff: null,
}
export function useErrorDetails(
error: Error | undefined,
getSquashedHydrationErrorDetails: (error: Error) => HydrationErrorState | null
): {
hydrationWarning: string | null
notes: string | null
reactOutputComponentDiff: string | null
} {
return useMemo(() => {
if (error === undefined) {
return noErrorDetails
}
const pagesRouterErrorDetails = getSquashedHydrationErrorDetails(error)
if (pagesRouterErrorDetails !== null) {
return {
hydrationWarning: pagesRouterErrorDetails.warning ?? null,
notes: null,
reactOutputComponentDiff:
pagesRouterErrorDetails.reactOutputComponentDiff ?? null,
}
}
if (!isHydrationError(error)) {
return noErrorDetails
}
const { message, notes, diff } = getHydrationErrorStackInfo(error)
if (message === null) {
return noErrorDetails
}
return {
hydrationWarning: message,
notes,
reactOutputComponentDiff: diff,
}
}, [error, getSquashedHydrationErrorDetails])
}
export function Errors({
getSquashedHydrationErrorDetails,
runtimeErrors,
debugInfo,
onClose,
...props
}: ErrorsProps) {
const dialogResizerRef = useRef<HTMLDivElement | null>(null)
const {
isLoading,
errorCode,
errorType,
notes,
hydrationWarning,
activeIdx,
errorDetails,
activeError,
setActiveIndex,
} = useActiveRuntimeError({ runtimeErrors, getSquashedHydrationErrorDetails })
// Get parsed frames data
const frames = useFrames(activeError)
const firstFrame = useMemo(() => {
const firstFirstPartyFrameIndex = frames.findIndex(
(entry) =>
!entry.ignored &&
Boolean(entry.originalCodeFrame) &&
Boolean(entry.originalStackFrame)
)
return frames[firstFirstPartyFrameIndex] ?? null
}, [frames])
const generateErrorInfo = useCallback(() => {
if (!activeError) return ''
const parts: string[] = []
// 1. Error Type
if (errorType) {
parts.push(`## Error Type\n${errorType}`)
}
// 2. Error Message
const error = activeError.error
let message = error.message
if ('environmentName' in error && error.environmentName) {
const envPrefix = `[ ${error.environmentName} ] `
if (message.startsWith(envPrefix)) {
message = message.slice(envPrefix.length)
}
}
if (message) {
parts.push(`## Error Message\n${message}`)
}
// Append call stack
if (frames.length > 0) {
const visibleFrames = frames.filter((frame) => !frame.ignored)
if (visibleFrames.length > 0) {
const stackLines = visibleFrames
.map((frame) => {
if (frame.originalStackFrame) {
const { methodName, file, line1, column1 } =
frame.originalStackFrame
return ` at ${methodName} (${file}:${line1}:${column1})`
} else if (frame.sourceStackFrame) {
const { methodName, file, line1, column1 } =
frame.sourceStackFrame
return ` at ${methodName} (${file}:${line1}:${column1})`
}
return ''
})
.filter(Boolean)
if (stackLines.length > 0) {
parts.push(`\n${stackLines.join('\n')}`)
}
}
}
// 3. Code Frame (decoded)
if (firstFrame?.originalCodeFrame) {
const decodedCodeFrame = stripAnsi(
formatCodeFrame(firstFrame.originalCodeFrame)
)
parts.push(`## Code Frame\n${decodedCodeFrame}`)
}
// Format as markdown error info
const errorInfo = `${parts.join('\n\n')}
Next.js version: ${props.versionInfo.installed} (${process.env.__NEXT_BUNDLER})\n`
return errorInfo
}, [activeError, errorType, firstFrame, frames, props.versionInfo])
if (isLoading) {
// TODO: better loading state
return (
<Overlay>
<OverlayBackdrop />
</Overlay>
)
}
if (!activeError) {
return null
}
const error = activeError.error
const isServerError = ['server', 'edge-server'].includes(
getErrorSource(error) || ''
)
return (
<ErrorOverlayLayout
errorCode={errorCode}
errorType={errorType}
errorMessage={
hydrationWarning ? (
<HydrationErrorDescription message={hydrationWarning} />
) : (
<GenericErrorDescription error={error} />
)
}
onClose={isServerError ? undefined : onClose}
debugInfo={debugInfo}
error={error}
runtimeErrors={runtimeErrors}
activeIdx={activeIdx}
setActiveIndex={setActiveIndex}
dialogResizerRef={dialogResizerRef}
generateErrorInfo={generateErrorInfo}
{...props}
>
<div className="error-overlay-notes-container">
{notes ? (
<>
<p
id="nextjs__container_errors__notes"
className="nextjs__container_errors__notes"
>
{notes}
</p>
</>
) : null}
{hydrationWarning ? (
<p
id="nextjs__container_errors__link"
className="nextjs__container_errors__link"
>
<HotlinkedText
text={`See more info here: ${NEXTJS_HYDRATION_ERROR_LINK}`}
/>
</p>
) : null}
</div>
{errorDetails.reactOutputComponentDiff ? (
<PseudoHtmlDiff
reactOutputComponentDiff={errorDetails.reactOutputComponentDiff || ''}
/>
) : null}
<Suspense fallback={<div data-nextjs-error-suspended />}>
<RuntimeError
key={activeError.id.toString()}
error={activeError}
dialogResizerRef={dialogResizerRef}
/>
</Suspense>
</ErrorOverlayLayout>
)
}
export const styles = `
.nextjs-error-with-static {
bottom: calc(16px * 4.5);
}
p.nextjs__container_errors__link {
font-size: var(--size-14);
}
p.nextjs__container_errors__notes {
color: var(--color-stack-notes);
font-size: var(--size-14);
line-height: 1.5;
}
.nextjs-container-errors-body > h2:not(:first-child) {
margin-top: calc(16px + 8px);
}
.nextjs-container-errors-body > h2 {
color: var(--color-title-color);
margin-bottom: 8px;
font-size: var(--size-20);
}
.nextjs-toast-errors-parent {
cursor: pointer;
transition: transform 0.2s ease;
}
.nextjs-toast-errors-parent:hover {
transform: scale(1.1);
}
.nextjs-toast-errors {
display: flex;
align-items: center;
justify-content: flex-start;
}
.nextjs-toast-errors > svg {
margin-right: 8px;
}
.nextjs-toast-hide-button {
margin-left: 24px;
border: none;
background: none;
color: var(--color-ansi-bright-white);
padding: 0;
transition: opacity 0.25s ease;
opacity: 0.7;
}
.nextjs-toast-hide-button:hover {
opacity: 1;
}
.nextjs__container_errors__error_title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
}
.error-overlay-notes-container {
margin: 8px 2px;
}
.error-overlay-notes-container p {
white-space: pre-wrap;
}
`
|