-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (59 loc) · 1.68 KB
/
index.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
'use strict'
const { flow, isNil, castArray, first } = require('lodash')
const PrettyError = require('pretty-error')
const ensureError = require('ensure-error')
const cleanStack = require('clean-stack')
const isIterable = require('is-iterable')
const DEFAULT_PRETTY = {
// this is a simple selector to the element that says 'Error'
'pretty-error > header > title > kind': {
background: 'none',
color: 'bright-red',
marginRight: 1
},
'pretty-error > header > colon': {
display: 'none'
},
'pretty-error > header > message': {
color: 'grey'
},
'pretty-error > trace > item > header > pointer > file': {
color: 'grey'
},
'pretty-error > trace > item > header > pointer > colon': {
color: 'grey'
},
'pretty-error > trace > item > header > pointer > line': {
color: 'grey'
},
'pretty-error > trace > item > header > what': {
color: 'grey'
}
}
const createPretty = opts => {
const pretty = new PrettyError()
pretty.appendStyle(opts)
return pretty
}
const pretty = createPretty(DEFAULT_PRETTY)
const getPretty = opts => (isNil(opts) ? pretty : createPretty(opts))
const cleanError = error => {
if (error.stack) error.stack = cleanStack(error.stack)
return error
}
const beautyError = (error, opts) => {
const pretty = getPretty(opts)
return pretty.render(error)
}
const getError = rawError => {
const genericError = ensureError(rawError)
return first(
isIterable(genericError)
? Array.from(genericError)
: castArray(genericError)
)
}
module.exports = flow([getError, cleanError, beautyError])
module.exports.beautyError = beautyError
module.exports.cleanError = cleanError
module.exports.getError = getError