-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.js
51 lines (46 loc) · 1.96 KB
/
node.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
require('dotenv').config()
const fs = require('fs')
const struct = (status, msg, data) => ({ status, msg, data });
let production = process.env.PRODUCTION;
const debugMode = typeof production == 'string' ? JSON.parse(production) : true;
const handleThis = async ({ fn, errMsg, args = [], onSuccess, onFailed, expectedOnPass, expectedOnFailed, name, returnOnFailed, returnOnPass, type = 'fn', errorCode = 500, senders = {}, recording = true, }) => {
let expected = expectedOnPass === undefined || expectedOnPass
try {
let response;
if (args && args.length > 0) {
response = await fn(...args)
} else {
response = await fn()
}
if (debugMode) {
if (expected) {
if (response === expectedOnPass) {
console.log(`expected : ${name} passed with response: ${response}`)
} else {
console.log(`unexpected : ${name} passed with response: ${response}`)
}
if (onSuccess) onSuccess({ status: true })
}
}
return returnOnPass
} catch (err) {
if (debugMode) {
if (expected) {
if (err.message === expectedOnFailed) {
console.log(`expected : ${name} failed with msg: ${errMsg} and error: ${err}`)
} else {
console.log(`unexpected : ${name} failed with msg: ${errMsg} and error: ${err}`)
}
if (onFailed) onFailed({ err, status: false })
}
console.error(msg)
console.log(err)
fs.appendFileSync('error.log', `date : ${new Date().toLocaleDateString()} | time: ${new Date().toLocaleTimeString()} | ${name} failed with msg: ${errMsg} and error: ${err} `)
}
if (type === 'api') {
senders.res.status(errorCode).send(struct(false, msg, err))
}
return returnOnFailed
}
}
module.exports = handleThis