/
Errors.tsx
323 lines (296 loc) · 9.12 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
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
import * as React from 'react'
import {
TYPE_UNHANDLED_ERROR,
TYPE_UNHANDLED_REJECTION,
UnhandledError,
UnhandledRejection,
} from '../bus'
import {
Dialog,
DialogBody,
DialogContent,
DialogHeader,
} from '../components/Dialog'
import { LeftRightDialogHeader } from '../components/LeftRightDialogHeader'
import { Overlay } from '../components/Overlay'
import { Toast } from '../components/Toast'
import { getErrorByType, ReadyRuntimeError } from '../helpers/getErrorByType'
import { isNodeError } from '../helpers/nodeStackFrames'
import { noop as css } from '../helpers/noop-template'
import { RuntimeError } from './RuntimeError'
export type SupportedErrorEvent = {
id: number
event: UnhandledError | UnhandledRejection
}
export type ErrorsProps = { errors: SupportedErrorEvent[] }
type ReadyErrorEvent = ReadyRuntimeError
function getErrorSignature(ev: SupportedErrorEvent): string {
const { event } = ev
switch (event.type) {
case TYPE_UNHANDLED_ERROR:
case TYPE_UNHANDLED_REJECTION: {
return `${event.reason.name}::${event.reason.message}::${event.reason.stack}`
}
default: {
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _: never = event
return ''
}
const HotlinkedText: React.FC<{
text: string
}> = function HotlinkedText(props) {
const { text } = props
const linkRegex = /https?:\/\/[^\s/$.?#].[^\s"]*/i
return (
<>
{linkRegex.test(text)
? text.split(' ').map((word, index, array) => {
if (linkRegex.test(word)) {
return (
<React.Fragment key={`link-${index}`}>
<a href={word}>{word}</a>
{index === array.length - 1 ? '' : ' '}
</React.Fragment>
)
}
return index === array.length - 1 ? (
<React.Fragment key={`text-${index}`}>{word}</React.Fragment>
) : (
<React.Fragment key={`text-${index}`}>{word} </React.Fragment>
)
})
: text}
</>
)
}
export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) {
const [lookups, setLookups] = React.useState(
{} as { [eventId: string]: ReadyErrorEvent }
)
const [readyErrors, nextError] = React.useMemo<
[ReadyErrorEvent[], SupportedErrorEvent | null]
>(() => {
let ready: ReadyErrorEvent[] = []
let next: SupportedErrorEvent | null = null
// Ensure errors are displayed in the order they occurred in:
for (let idx = 0; idx < errors.length; ++idx) {
const e = errors[idx]
const { id } = e
if (id in lookups) {
ready.push(lookups[id])
continue
}
// Check for duplicate errors
if (idx > 0) {
const prev = errors[idx - 1]
if (getErrorSignature(prev) === getErrorSignature(e)) {
continue
}
}
next = e
break
}
return [ready, next]
}, [errors, lookups])
const isLoading = React.useMemo<boolean>(() => {
return readyErrors.length < 1 && Boolean(errors.length)
}, [errors.length, readyErrors.length])
React.useEffect(() => {
if (nextError == null) {
return
}
let mounted = true
getErrorByType(nextError).then(
(resolved) => {
// We don't care if the desired error changed while we were resolving,
// thus we're not tracking it using a ref. Once the work has been done,
// we'll store it.
if (mounted) {
setLookups((m) => ({ ...m, [resolved.id]: resolved }))
}
},
() => {
// TODO: handle this, though an edge case
}
)
return () => {
mounted = false
}
}, [nextError])
const [isMinimized, setMinimized] = React.useState<boolean>(false)
const [activeIdx, setActiveIndex] = React.useState<number>(0)
const previous = React.useCallback((e?: MouseEvent | TouchEvent) => {
e?.preventDefault()
setActiveIndex((v) => Math.max(0, v - 1))
}, [])
const next = React.useCallback(
(e?: MouseEvent | TouchEvent) => {
e?.preventDefault()
setActiveIndex((v) =>
Math.max(0, Math.min(readyErrors.length - 1, v + 1))
)
},
[readyErrors.length]
)
const activeError = React.useMemo<ReadyErrorEvent | null>(
() => readyErrors[activeIdx] ?? null,
[activeIdx, readyErrors]
)
// Reset component state when there are no errors to be displayed.
// This should never happen, but lets handle it.
React.useEffect(() => {
if (errors.length < 1) {
setLookups({})
setMinimized(false)
setActiveIndex(0)
}
}, [errors.length])
const minimize = React.useCallback((e?: MouseEvent | TouchEvent) => {
e?.preventDefault()
setMinimized(true)
}, [])
const reopen = React.useCallback(
(e?: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
e?.preventDefault()
setMinimized(false)
},
[]
)
// This component shouldn't be rendered with no errors, but if it is, let's
// handle it gracefully by rendering nothing.
if (errors.length < 1 || activeError == null) {
return null
}
if (isLoading) {
// TODO: better loading state
return <Overlay />
}
if (isMinimized) {
return (
<Toast className="nextjs-toast-errors-parent" onClick={reopen}>
<div className="nextjs-toast-errors">
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<circle cx="12" cy="12" r="10"></circle>
<line x1="12" y1="8" x2="12" y2="12"></line>
<line x1="12" y1="16" x2="12.01" y2="16"></line>
</svg>
<span>
{readyErrors.length} error{readyErrors.length > 1 ? 's' : ''}
</span>
</div>
</Toast>
)
}
const isServerError = isNodeError(activeError.error)
return (
<Overlay>
<Dialog
type="error"
aria-labelledby="nextjs__container_errors_label"
aria-describedby="nextjs__container_errors_desc"
onClose={isServerError ? undefined : minimize}
>
<DialogContent>
<DialogHeader className="nextjs-container-errors-header">
<LeftRightDialogHeader
previous={activeIdx > 0 ? previous : null}
next={activeIdx < readyErrors.length - 1 ? next : null}
close={isServerError ? undefined : minimize}
>
<small>
<span>{activeIdx + 1}</span> of{' '}
<span>{readyErrors.length}</span> unhandled error
{readyErrors.length < 2 ? '' : 's'}
</small>
</LeftRightDialogHeader>
<h1 id="nextjs__container_errors_label">
{isServerError ? 'Server Error' : 'Unhandled Runtime Error'}
</h1>
<p id="nextjs__container_errors_desc">
{activeError.error.name}:{' '}
<HotlinkedText text={activeError.error.message} />
</p>
{isServerError ? (
<div>
<small>
This error happened while generating the page. Any console
logs will be displayed in the terminal window.
</small>
</div>
) : undefined}
</DialogHeader>
<DialogBody className="nextjs-container-errors-body">
<RuntimeError key={activeError.id.toString()} error={activeError} />
</DialogBody>
</DialogContent>
</Dialog>
</Overlay>
)
}
export const styles = css`
.nextjs-container-errors-header > h1 {
font-size: var(--size-font-big);
line-height: var(--size-font-bigger);
font-weight: bold;
margin: 0;
margin-top: calc(var(--size-gap-double) + var(--size-gap-half));
}
.nextjs-container-errors-header small {
font-size: var(--size-font-small);
color: var(--color-accents-1);
margin-left: var(--size-gap-double);
}
.nextjs-container-errors-header small > span {
font-family: var(--font-stack-monospace);
}
.nextjs-container-errors-header > p {
font-family: var(--font-stack-monospace);
font-size: var(--size-font-small);
line-height: var(--size-font-big);
font-weight: bold;
margin: 0;
margin-top: var(--size-gap-half);
color: var(--color-ansi-red);
white-space: pre-wrap;
}
.nextjs-container-errors-header > div > small {
margin: 0;
margin-top: var(--size-gap-half);
}
.nextjs-container-errors-header > p > a {
color: var(--color-ansi-red);
}
.nextjs-container-errors-body > h5:not(:first-child) {
margin-top: calc(var(--size-gap-double) + var(--size-gap));
}
.nextjs-container-errors-body > h5 {
margin-bottom: var(--size-gap);
}
.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: var(--size-gap);
}
`