-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfn.js
39 lines (35 loc) · 1.45 KB
/
fn.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 struct = (status, msg, data) => ({ status, msg, data });
let production = process.env.PRODUCTION;
const debugMode = typeof production == 'string' ? JSON.parse(production) : true;
const handleThis = async ({ log = false, fn, errMsg, onSuccess, onFailed, expectedOnPass, expectedOnFailed, name, returnOnFailed, returnOnPass }) => {
let expected = expectedOnPass === undefined || expectedOnPass
try {
let response = await fn()
if (debugMode || log) {
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 || log) {
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(errMsg)
console.log(err)
}
return returnOnFailed
}
}
module.exports = handleThis