UNPKG

1.44 kBJavaScriptView Raw
1const isBrowser = typeof window === 'object'
2
3const red = isBrowser ? '' : '\u001b[38;5;160m'
4const yellow = isBrowser ? '' : '\u001b[38;5;226m'
5const dgrey = isBrowser ? '' : '\u001b[38;5;244m'
6const lgrey = isBrowser ? '' : '\u001b[38;5;250m'
7const azure = isBrowser ? '' : '\u001b[38;5;39m'
8const uncolor = isBrowser ? '' : '\u001b[m'
9
10class FSError extends Error {
11 constructor (message, parser) {
12 super(message)
13 }
14
15 toString () {
16 const stack = this.stack.split('\n')
17 const reg = /at\s+(.+)\s+\((.+?):(\d+):(\d+)\)/
18 const msg = stack.shift()
19 const colorizedStack = stack.map((line) => {
20 const match = line.match(reg)
21
22 if (!match) {
23 return line
24 }
25
26 const cap = {
27 method: match[1],
28 file: match[2],
29 line: match[3],
30 column: match[4]
31 }
32
33 let str = ` at ${azure}${cap.method}${uncolor} `
34 let source = ''
35 let lineNum = parseInt(cap.line)
36 let columnNum = parseInt(cap.column)
37
38 if (cap.file.indexOf('/node_modules/') !== -1) {
39 str += `${dgrey}${cap.file}${uncolor}`
40 } else {
41 str += `${lgrey}${cap.file}${uncolor}`
42 }
43
44 str += ` ${lineNum}:${columnNum}`
45 return source + str
46 }).join('\n')
47
48 const nl = '\n'
49 this.stackOrig = this.stack
50 this.stack = `${msg}${nl}${colorizedStack}`
51 return `${red}Firescript Error${uncolor} ${msg}`
52 }
53}
54
55module.exports.FSError = FSError