-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
72 lines (60 loc) · 1.86 KB
/
index.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
/*
* Copyright 2023 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const newrelic = require('newrelic')
const express = require('express')
const { v4: uuidv4 } = require('uuid')
const { interactWithAPI, interactWithDatabase } = require('./helpers')
const app = express()
newrelic.setErrorGroupCallback(function groupErrors(errMetadata) {
let errorGroup
if (errMetadata['error.expected']) {
if (errMetadata.error?.metadata?.component === 'api') {
errorGroup = 'Expected API Error'
}
if (errMetadata.error?.metadata?.component === 'database') {
errorGroup = 'Expected Database Error'
}
} else {
if (errMetadata.error?.metadata?.component === 'api') {
errorGroup = 'Unexpected API Error'
}
if (errMetadata.error?.metadata?.component === 'database') {
errorGroup = 'Unexpected Database Error'
}
}
return errorGroup
})
app.use(function setUser(req, res, next) {
const id = req.query.user_id || uuidv4()
newrelic.setUserID(id)
next()
})
app.get('/error/expected', function expectedController(_req, res) {
return Promise.all([interactWithAPI(), interactWithDatabase()])
.catch((err) => {
if (err.isExpected) {
newrelic.noticeError(err, err.metadata, true)
} else {
newrelic.noticeError(err, err.metadata, false)
}
})
.finally(() => {
return res.status(200).json({ message: 'OK' })
})
})
app.get('/error/unexpected', function unexpectedController(_req, res) {
return Promise.all([interactWithAPI(), interactWithDatabase()])
.then(() => {
return res.status(200).json({ message: 'OK' })
})
.catch((err) => {
return res.status(500).json({ message: err.message })
})
})
app.listen(3000, () => {
// eslint-disable-next-line no-console
console.log('Server listening on port 3000')
})