-
-
Notifications
You must be signed in to change notification settings - Fork 782
/
errors.ts
72 lines (54 loc) · 1.83 KB
/
errors.ts
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
import {log} from '@blitzjs/display'
import {through} from './streams'
import {Writable} from 'stream'
import {ERROR_THROWN} from './events'
export type Event<T> = {type: string; payload: T}
type Error = DuplicatePathError | NestedRouteError
/**
* Returns an object with a stream that takes operational errors and prepares them for the console.
*/
export function createErrorsStream(reporter: Writable) {
const stream = through({objectMode: true}, (err: Error, _, next) => {
reporter.write({type: ERROR_THROWN, payload: err})
if (err instanceof DuplicatePathError) {
renderDuplicatePathError(err)
return next()
}
if (err instanceof NestedRouteError) {
renderNestedRouteError(err)
return next()
}
next(err)
})
return {stream}
}
export class DuplicatePathError extends Error {
name = 'DuplicatePathError'
constructor(public message: string, public pathType: string, public paths: string[][]) {
super(message)
}
}
const removeCwd = (path: string) => path.replace(process.cwd(), '')
const renderErrorMessage = (path: string, type: string) =>
`- ${path.split(`${type}/`)[0]}${type}/${log.variable(path.split(`${type}/`)[1])}`
export function renderDuplicatePathError(err: DuplicatePathError) {
log.error(err.message)
err.paths.forEach((page) => {
page.forEach((path) => console.log(renderErrorMessage(removeCwd(path), err.pathType)))
})
}
export class NestedRouteError extends Error {
name = 'NestedRouteError'
constructor(public message: string, public secondary: string, public routes: string[]) {
super(message)
}
}
export function renderNestedRouteError(err: NestedRouteError) {
log.error(err.message)
process.stdout.write('\n')
err.routes.forEach((route) => {
console.log(`- ${removeCwd(route)}`)
})
process.stdout.write('\n')
log.error(err.secondary)
}