|
1 | | -To add an attachment, create an event processor that uploads files to the |
2 | | -attachment endpoint in a multipart form data request. This requires associating |
3 | | -the attachment with the event using its ID: |
| 1 | +Attachments live on the `Scope` and will be sent with all events. |
4 | 2 |
|
5 | 3 | ```javascript |
6 | | -public attachmentUrlFromDsn(dsn: Dsn, eventId: string) { |
7 | | - const { host, path, projectId, port, protocol, user } = dsn; |
8 | | - return `${protocol}://${host}${port !== '' ? `:${port}` : ''}${ |
9 | | - path !== '' ? `/${path}` : '' |
10 | | - }/api/${projectId}/events/${eventId}/attachments/?sentry_key=${user}&sentry_version=7&sentry_client=custom-javascript`; |
11 | | -} |
12 | | - |
13 | | -Sentry.addGlobalEventProcessor((event: Event) => { |
14 | | - try { |
15 | | - const client = Sentry.getCurrentHub().getClient(); |
16 | | - const endpoint = attachmentUrlFromDsn( |
17 | | - client.getDsn(), |
18 | | - event.event_id |
19 | | - ); |
20 | | - const formData = new FormData(); |
21 | | - formData.append( |
22 | | - 'my-attachment', |
23 | | - new Blob([JSON.stringify({ logEntries: ["my log"] })], { |
24 | | - type: 'application/json', |
25 | | - }), |
26 | | - 'logs.json' |
27 | | - ); |
28 | | - fetch(endpoint, { |
29 | | - method: 'POST', |
30 | | - body: formData, |
31 | | - }).catch((ex) => { |
32 | | - // we have to catch this otherwise it throws an infinite loop in Sentry |
33 | | - console.error(ex); |
34 | | - }); |
| 4 | +// Add an attachment |
| 5 | +Sentry.configureScope(scope => { |
| 6 | + scope.addAttachment({ filename: "attachment.txt", data: "Some content" }); |
| 7 | +}); |
| 8 | + |
| 9 | +// Clear attachments |
| 10 | +Sentry.configureScope(scope => { |
| 11 | + scope.clearAttachments(); |
| 12 | +}); |
| 13 | +``` |
| 14 | + |
| 15 | +An attachment has the following fields: |
| 16 | + |
| 17 | +`filename` |
| 18 | + |
| 19 | +: The filename is required and will be displayed in [sentry.io](https://sentry.io). |
| 20 | + |
| 21 | +`data` |
| 22 | + |
| 23 | +: The content of the attachment is required and is either a `string` or `Uint8Array`. |
| 24 | + |
| 25 | +`contentType` |
| 26 | + |
| 27 | +: The type of content stored in this attachment. Any [MIME |
| 28 | +type](https://www.iana.org/assignments/media-types/media-types.xhtml) may be |
| 29 | +used; the default is `application/octet-stream`. |
| 30 | + |
| 31 | +## Add or Modify Attachments Before Sending |
| 32 | + |
| 33 | +It's possible to add, remove, or modify attachments before an event is sent by way of |
| 34 | +the <PlatformLink to="/configuration/options/#before-send"><PlatformIdentifier name="before-send" /></PlatformLink> |
| 35 | +hook or a global event processor. |
| 36 | + |
| 37 | +```javascript |
| 38 | +Sentry.init({ |
| 39 | + dsn: "___PUBLIC_DSN___", |
| 40 | + beforeSend: (event, hint) => { |
| 41 | + hint.attachments = [{ filename: "screenshot.png", data: captureScreen() }]; |
35 | 42 | return event; |
36 | | - } catch (ex) { |
37 | | - console.error(ex); |
38 | | - } |
| 43 | + }, |
| 44 | +}); |
| 45 | + |
| 46 | +Sentry.addGlobalEventProcessor((event, hint) => { |
| 47 | + hint.attachments = [{ filename: "log.txt", data: readLogFile() }]; |
| 48 | + return event; |
39 | 49 | }); |
40 | 50 | ``` |
0 commit comments