-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
Copy patherrors.tsx
283 lines (260 loc) · 7.87 KB
/
errors.tsx
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
import { useState, useMemo, useEffect, useRef, Suspense } from 'react'
import type { DebugInfo } from '../../types'
import { Overlay } 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 {
type HydrationErrorState,
getHydrationWarningType,
} from '../../../errors/hydration-error-info'
import {
isConsoleError,
getConsoleErrorType,
} from '../../../errors/console-error'
import { extractNextErrorCode } from '../../../../../lib/error-telemetry-utils'
import {
ErrorOverlayLayout,
type ErrorOverlayLayoutProps,
} from '../components/errors/error-overlay-layout/error-overlay-layout'
import { NEXTJS_HYDRATION_ERROR_LINK } from '../../../is-hydration-error'
import type { ReadyRuntimeError } from '../../utils/get-error-by-type'
import type { ErrorBaseProps } from '../components/errors/error-overlay/error-overlay'
export interface ErrorsProps extends ErrorBaseProps {
runtimeErrors: ReadyRuntimeError[]
debugInfo: DebugInfo
onClose: () => void
}
type ReadyErrorEvent = ReadyRuntimeError
function isNextjsLink(text: string): boolean {
return text.startsWith('https://nextjs.org')
}
function ErrorDescription({
error,
hydrationWarning,
}: {
error: Error
hydrationWarning: string | null
}) {
const unhandledErrorType = isConsoleError(error)
? getConsoleErrorType(error)
: null
const isConsoleErrorStringMessage = unhandledErrorType === 'string'
// If the error is:
// - hydration warning
// - captured console error or unhandled rejection
// skip displaying the error name
const title =
isConsoleErrorStringMessage || hydrationWarning ? '' : error.name + ': '
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 (
<>
{title}
<HotlinkedText
text={hydrationWarning || message}
matcher={isNextjsLink}
/>
</>
)
}
function getErrorType(error: Error): ErrorOverlayLayoutProps['errorType'] {
if (isConsoleError(error)) {
return 'Console Error'
}
return 'Runtime Error'
}
export function Errors({
runtimeErrors,
debugInfo,
onClose,
...props
}: ErrorsProps) {
const dialogResizerRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
// Close the error overlay when pressing escape
function handleKeyDown(event: KeyboardEvent) {
if (event.key === 'Escape') {
onClose()
}
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [onClose])
const isLoading = useMemo<boolean>(() => {
return runtimeErrors.length < 1
}, [runtimeErrors.length])
const [activeIdx, setActiveIndex] = useState<number>(0)
const activeError = useMemo<ReadyErrorEvent | null>(
() => runtimeErrors[activeIdx] ?? null,
[activeIdx, runtimeErrors]
)
if (isLoading) {
// TODO: better loading state
return <Overlay />
}
if (!activeError) {
return null
}
const error = activeError.error
const isServerError = ['server', 'edge-server'].includes(
getErrorSource(error) || ''
)
const errorType = getErrorType(error)
const errorDetails: HydrationErrorState = (error as any).details || {}
const notes = errorDetails.notes || ''
const [warningTemplate, serverContent, clientContent] =
errorDetails.warning || [null, '', '']
const hydrationErrorType = getHydrationWarningType(warningTemplate)
const hydrationWarning = warningTemplate
? warningTemplate
.replace('%s', serverContent)
.replace('%s', clientContent)
.replace('%s', '') // remove the %s for stack
.replace(/%s$/, '') // If there's still a %s at the end, remove it
.replace(/^Warning: /, '')
.replace(/^Error: /, '')
: null
const errorCode = extractNextErrorCode(error)
const footerMessage = isServerError
? 'This error happened while generating the page. Any console logs will be displayed in the terminal window.'
: undefined
return (
<ErrorOverlayLayout
errorCode={errorCode}
errorType={errorType}
errorMessage={
<ErrorDescription error={error} hydrationWarning={hydrationWarning} />
}
onClose={isServerError ? undefined : onClose}
debugInfo={debugInfo}
error={error}
runtimeErrors={runtimeErrors}
activeIdx={activeIdx}
setActiveIndex={setActiveIndex}
footerMessage={footerMessage}
dialogResizerRef={dialogResizerRef}
{...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>
{hydrationWarning &&
(activeError.componentStackFrames?.length ||
!!errorDetails.reactOutputComponentDiff) ? (
<PseudoHtmlDiff
className="nextjs__container_errors__component-stack"
hydrationMismatchType={hydrationErrorType}
firstContent={serverContent}
secondContent={clientContent}
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_inspect_copy_button {
cursor: pointer;
background: none;
border: none;
color: var(--color-ansi-bright-white);
font-size: var(--size-24);
padding: 0;
margin: 0;
margin-left: 8px;
transition: opacity 0.25s ease;
}
.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;
}
`