forked from crypto-pepe/DefiLlama-Adapters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleError.js
39 lines (34 loc) · 1.23 KB
/
handleError.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
const errorString = '------ ERROR ------'
function handleError(error){
console.log('\n',errorString, '\n\n')
const isGraphError = error.stack?.includes('graphql-request') && error.response?.errors?.length
if (isGraphError)
console.error(error.response.errors.map(e => e.message).join('\n'))
else
console.error(error.toString())
const axiosError = error?.response?.data?.message
if (axiosError)
console.log('Axios: ', axiosError)
const stack = getStackMessage(error.stack)
if (stack.length) {
console.log('Truncated error stack:')
console.log(stack.join('\n'))
}
process.exit(1)
}
function getStackMessage(stack) {
if (/ at (checkExportKeys)/.test(stack)) return []
const isNodeMolule = m => /node_modules/.test(m)
const isNotLoggerMessage = m => !/log/.test(m)
const isNotInternalMessage = m => !/node:internal/.test(m)
const message = []
stack = stack.split('\n')
while (stack.length && !stack[0].includes(' at '))
stack.shift()
const firstNMStackMessage = stack.filter(isNodeMolule)
.filter(isNotLoggerMessage)[0]
stack = stack.filter(m => !isNodeMolule(m)).filter(isNotInternalMessage)
message.push(firstNMStackMessage, ...stack)
return message
}
module.exports = handleError