-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (85 loc) · 2.61 KB
/
index.js
File metadata and controls
95 lines (85 loc) · 2.61 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const Sentry = require('@sentry/node')
const FormData = require('form-data')
const axios = require('axios')
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
environment: process.env.SENTRY_ENVIRONMENT
})
Sentry.configureScope((scope) => {
scope.setTag('app', 'sentry_attachment_issue')
})
const attachmentUrlFromDsn = (dsn, eventId) => {
const { host, path, projectId, port, protocol, user } = dsn
return `${protocol}://${host}${port !== '' ? `:${port}` : ''}${
path !== '' ? `/${path}` : ''
}/api/${projectId}/events/${eventId}/attachments/?sentry_key=${user}&sentry_version=7&sentry_client=custom-javascript`
}
const attachAsJson = async (
attachments,
event,
client
) => {
const dsn = client.getDsn()
if (!dsn) {
return
}
const endpoint = attachmentUrlFromDsn(dsn, event.event_id)
const formData = new FormData()
for (let attachment of attachments) {
formData.append('json-attachment', JSON.stringify(attachment))
}
try {
const res = await axios.request({
method: 'POST',
url: endpoint,
data: formData,
headers: {
'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`,
},
})
console.log(res.status, res.statusText)
} catch (err) {
console.warn(err)
throw new Error('Unexpected error sending attachments to sentry')
}
}
const captureException = async (e, ctx) => {
const p = new Promise(
(resolve) => {
Sentry.withScope(function (scope) {
// Setup attachments to include with the report
if (ctx.attachments && ctx.attachments.length) {
scope.addEventProcessor(async (event) => {
try {
await attachAsJson(
ctx.attachments,
event,
Sentry.getCurrentHub().getClient()
)
} catch (ex) {
console.error(ex)
}
return event
})
}
Sentry.captureException(e)
resolve()
})
}
)
await p
await Sentry.flush()
}
(async () => {
try {
const error = new Error('this is an error ' + Date.now())
await captureException(error, {
attachments: [{
foo: 'bar'
}]
})
} catch (e) {
console.error(e)
}
})();