-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.js
157 lines (144 loc) · 3.33 KB
/
utils.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const errorStackParser = require('error-stack-parser')
/**
* @function
* @public
*
* Check if the variable is a string/Array and contains
* the request search term
*
* @param {string | Array} data The variable to be searched
* @param {*} search The search term to be found
*
* @returns {boolean} Whether the variable contains the search term
*/
function contains (data, search) {
return data && data.includes && data.includes(search)
}
/**
* @function
* @public
*
* Check if the variable is a JSON-like string.
* Have to be surrounded with a combination of
* brackets and curly braces.
*
* @param {string} data The string to be searched
*
* @returns {boolean} Whether the variable is JSON-alike
*/
function isJSON (data) {
return data && data.startsWith('{') && data.endsWith('}')
}
/**
* @function
* @public
*
* No operation. Pass the variable through.
*
* @param {*} data The data to be passed through
* @returns {*} The passed through data
*/
function noop (data) {
return data
}
/**
* @function
* @private
*
* Wrapper for the `pino`/`hapi` server log event.
*
* @param {Hapi.Server} server The created server instance
* @param {string} level The logging level
* @param {Array} data The data to be logged
*/
function wrapper (server, level, ...data) {
server.log(level, data.length === 1 ? data[0] : data)
}
/**
* @function
* @public
*
* Override several `console` methods with `pino` ones
* to enable logging everywhere.
*
* @param {Hapi.Server} server The created server instance
*/
function override (server) {
['trace', 'log', 'info', 'warn', 'error', 'debug'].forEach(method => {
const target = method === 'log' ? 'debug' : method
console[method] = wrapper.bind(undefined, server, target)
})
}
/**
* @function
* @public
*
* Stringify the data, surround data with quotes
* and escape inner quotes if necessary.
*
* @param {*} data The data to be stringified
* @returns {str} The stringified data
*/
function stringify (data) {
return String(JSON.stringify(data))
}
/**
* @function
* @public
*
* Objectify the object/error and its properties.
*
* @param {Object|Error} obj The object to be parsed
* @returns {string} The parsed error
*/
function objectify (obj) {
return JSON.parse(JSON.stringify(obj, Object.getOwnPropertyNames(obj)))
}
/**
* @function
* @public
*
* Get source line of error stack.
*
*
* @param {Error} err The error to be analyzed
* @returns {string} The composed source line
*/
function getErrorSource (err) {
let result
try {
const { fileName, lineNumber, columnNumber } = errorStackParser.parse(err).shift()
result = `${fileName}:${lineNumber}:${columnNumber}`
} catch (err) {}
return result
}
/**
* @function
* @public
*
* Handle uncaught exception if enabled per option.
* Exit the process and log with `error` level.
*
* @param {Hapi.Server} server The related server instance
* @param {boolean} enabled If uncaught exception should be handled
*/
function handleUncaught (server, enabled) {
if (!enabled) {
return
}
process.once('uncaughtException', (err) => {
err = Object.assign({ source: getErrorSource(err) }, objectify(err))
server.log(['uncaught', 'error'], { err })
process.exit(1)
})
}
module.exports = {
contains,
isJSON,
noop,
override,
stringify,
objectify,
handleUncaught,
getErrorSource
}