-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorContext.js
85 lines (72 loc) · 1.85 KB
/
errorContext.js
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
/**
* Error handling utilities
*
* @package: Svelte universal component compiler
* @since: 2020-08-24
*/
function createErrorContext () {
let errors = []
function addError (messagePrefix, err) {
if (messagePrefix) {
err.message = `${messagePrefix}: ${err.message}`
}
errors.push(err)
}
function addCompilerWarnings (path, warnings) {
if (warnings && warnings.length) {
warnings.forEach(w => {
w.message = `Warning: ${w.message}`
})
errors = errors.concat(warnings)
}
}
function outputErrors () {
if (errors.length) {
console.log('Finished with errors.')
console.error('')
errors.forEach(renderError)
} else {
console.log('Compiled successfully.')
}
}
return {
addError,
addCompilerWarnings,
outputErrors,
}
}
const RESET = '\x1b[0m'
const BRIGHT = '\x1b[1m'
const DIM = '\x1b[2m'
const BG_RED = '\x1b[41m'
const FG_WHITE = '\x1b[37m'
const FG_YELLOW = '\x1b[33m'
const STYL_LBL = `${BRIGHT}${BG_RED}${FG_WHITE}`
const STYL_PATH = `${DIM}${FG_WHITE}`
const STYL_MSG = `${FG_WHITE}`
const STYL_FRAME = `${FG_YELLOW}`
function renderError (error) {
const stack = error.stack
const filename = error.filename
const message = error.message
const frame = error.frame
const line = error.start ? error.start.line : ''
let kind
if (error.kind) {
kind = error.kind
} else if (typeof stack === 'string') {
const m = stack.match(/^([a-zA-Z0-9_$]+): /)
if (m) {
kind = m[1]
}
}
console.error(`${STYL_LBL}${kind || 'Error'}${RESET}: ${STYL_PATH}${filename}${line ? `:${line}` : ''}${RESET}`)
console.error(`\t${STYL_MSG}${message}${RESET}`)
if (frame) {
console.error(`${STYL_FRAME}${frame}${RESET}`)
} else {
console.error(`${STYL_FRAME}${stack}${RESET}`)
}
console.error()
}
module.exports = createErrorContext