-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
errors.ts
129 lines (108 loc) · 3.51 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
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
import PrettyError from "pretty-error"
import stackTrace from "stack-trace"
import { prepareStackTrace, ErrorWithCodeFrame } from "./prepare-stack-trace"
import { isNodeInternalModulePath } from "gatsby-core-utils"
import { IStructuredStackFrame } from "../structured-errors/types"
const packagesToSkip = [`core-js`, `bluebird`, `regenerator-runtime`, `graphql`]
const packagesToSkipTest = new RegExp(
`node_modules[\\/](${packagesToSkip.join(`|`)})`
)
// TO-DO: move this this out of this file (and probably delete this file completely)
// it's here because it re-implements similar thing as `pretty-error` already does
export const sanitizeStructuredStackTrace = (
stack: Array<stackTrace.StackFrame>
): Array<IStructuredStackFrame> => {
// first filter out not useful call sites
stack = stack.filter(callSite => {
if (!callSite.getFileName()) {
return false
}
if (packagesToSkipTest.test(callSite.getFileName())) {
return false
}
if (callSite.getFileName().includes(`asyncToGenerator.js`)) {
return false
}
if (isNodeInternalModulePath(callSite.getFileName())) {
return false
}
return true
})
// then sanitize individual call site objects to make sure we don't
// emit objects with extra fields that won't be handled by consumers
return stack.map(callSite => {
return {
fileName: callSite.getFileName(),
functionName: callSite.getFunctionName(),
columnNumber: callSite.getColumnNumber(),
lineNumber: callSite.getLineNumber(),
}
})
}
type PrettyRenderError = Error | ErrorWithCodeFrame
export function getErrorFormatter(): PrettyError {
const prettyError = new PrettyError()
const baseRender = prettyError.render
prettyError.skipNodeFiles()
prettyError.skipPackage(
`regenerator-runtime`,
`graphql`,
`core-js`
// `static-site-generator-webpack-plugin`,
// `tapable`, // webpack
)
// @ts-ignore the type defs in prettyError are wrong
prettyError.skip((traceLine: any) => {
if (traceLine && traceLine.file === `asyncToGenerator.js`) return true
return false
})
prettyError.appendStyle({
"pretty-error": {
marginTop: 1,
},
"pretty-error > header": {
background: `red`,
},
"pretty-error > header > colon": {
color: `white`,
},
})
if (process.env.FORCE_COLOR === `0`) {
prettyError.withoutColors()
}
prettyError.render = (
err: PrettyRenderError | Array<PrettyRenderError>
): string => {
if (Array.isArray(err)) {
return err.map(e => prettyError.render(e)).join(`\n`)
}
let rendered = baseRender.call(prettyError, err)
if (`codeFrame` in err) rendered = `\n${err.codeFrame}\n${rendered}`
return rendered
}
return prettyError
}
/**
* Convert a stringified webpack compilation error back into
* an Error instance so it can be formatted properly
*/
export function createErrorFromString(
errorStr: string = ``,
sourceMapFile: string
): ErrorWithCodeFrame {
let [message, ...rest] = errorStr.split(/\r\n|[\n\r]/g)
// pull the message from the first line then remove the `Error:` prefix
// FIXME: when https://github.com/AriaMinaei/pretty-error/pull/49 is merged
message = message.replace(/^(Error:)/, ``)
const error = new Error(message)
error.stack = [message, rest.join(`\n`)].join(`\n`)
error.name = `WebpackError`
try {
if (sourceMapFile) {
return prepareStackTrace(error, sourceMapFile)
}
} catch (err) {
// don't shadow a real error because of a parsing issue
}
return error
}