-
Notifications
You must be signed in to change notification settings - Fork 12
/
custom-request-data.mjs
36 lines (32 loc) · 1.03 KB
/
custom-request-data.mjs
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
import plugin from '../index.js';
import { tryToExtractBody } from '../utils.js'; // @immobiliarelabs/fastify-sentry/utils
import fastify from 'fastify';
const app = fastify({ logger: true });
app.register(plugin, {
environment: 'Fastify Tests',
extractRequestData(request, keys) {
const data = {};
/**
* For each request this function is called twice.
* The first time it extract the data without the body to attach it
* to the Sentry transaction. The second one when an exception is captured
* in order to try to extract the body from the request.
*
* This is because the `onRequest` hook of fastify (used to initialize the transaction)
* doesn't provide the request body yet.
*/
if (keys.includes('data')) {
data.data = tryToExtractBody(request);
} else {
data.headers = request.headers;
}
return data;
},
});
app.get('/', async () => {
return { hello: true };
});
app.get('/error', async () => {
throw new Error('Fastify Error');
});
app.listen({ port: 3000 });