-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathhelpers.js
44 lines (35 loc) · 934 Bytes
/
helpers.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
/*
* Copyright 2023 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
async function sleep(ms) {
await new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
async function randomlyFail(type) {
await sleep(1000)
const randomNumber = Math.round(Math.random() * 100)
if (randomNumber >= 90) {
let error
if (randomNumber % 2 === 0) {
error = new Error(`Encountered expected application error`)
error.isExpected = true
} else {
error = new Error(`Encountered unexpected application error`)
error.isExpected = false
}
if (type === 'database') {
error.metadata = { component: 'database' }
}
if (type === 'api') {
error.metadata = { component: 'api' }
}
throw error
}
}
module.exports = {
interactWithDatabase: () => randomlyFail('database'),
interactWithAPI: () => randomlyFail('api')
}