Skip to content

Commit 56790be

Browse files
chore(browser): Update browser example (#7764)
1 parent 170ffc8 commit 56790be

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

packages/browser/examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Assuming `npm@>=5.2.0` is installed and `@sentry/browser` package is built locally:
22

33
```sh
4-
$ npx serve -S
4+
$ npx serve -S examples
55
```

packages/browser/examples/app.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,35 @@ class HappyIntegration {
1818
}
1919
}
2020

21-
class HappyTransport extends Sentry.Transports.BaseTransport {
22-
sendEvent(event) {
21+
function makeHappyTransport(options) {
22+
function makeRequest(request) {
2323
console.log(
24-
`This is the place where you'd implement your own sending logic. It'd get url: ${this.url} and an event itself:`,
25-
event,
24+
`This is the place to implement your own sending logic. It'd get url: ${options.url} and a raw envelope:`,
25+
request.body,
2626
);
2727

28-
return Promise.resolve({
29-
status: 'success',
30-
});
28+
// this is where your sending logic goes
29+
const myCustomRequest = {
30+
body: request.body,
31+
method: 'POST',
32+
referrerPolicy: 'origin',
33+
headers: options.headers,
34+
...options.fetchOptions
35+
};
36+
37+
// you define how `sendMyCustomRequest` works
38+
const sendMyCustomRequest = (r) => fetch(options.url, r);
39+
return sendMyCustomRequest(myCustomRequest).then(response => ({
40+
statusCode: response.status,
41+
headers: {
42+
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
43+
'retry-after': response.headers.get('Retry-After'),
44+
},
45+
}));
3146
}
47+
48+
// `createTransport` takes care of rate limiting and flushing
49+
return Sentry.createTransport(options, makeRequest);
3250
}
3351

3452
Sentry.init({
@@ -39,7 +57,7 @@ Sentry.init({
3957
// An array of strings or regexps that'll be used to ignore specific errors based on their origin url
4058
denyUrls: ['external-lib.js'],
4159
// An array of strings or regexps that'll be used to allow specific errors based on their origin url
42-
allowUrls: ['http://localhost:5000', 'https://browser.sentry-cdn'],
60+
allowUrls: ['http://localhost:3000', 'https://browser.sentry-cdn'],
4361
// Debug mode with valuable initialization/lifecycle informations.
4462
debug: true,
4563
// Whether SDK should be enabled or not.
@@ -53,7 +71,7 @@ Sentry.init({
5371
// An environment identifier.
5472
environment: 'staging',
5573
// Custom event transport that will be used to send things to Sentry
56-
transport: HappyTransport,
74+
transport: makeHappyTransport,
5775
// Method called for every captured event
5876
async beforeSend(event, hint) {
5977
// Because beforeSend and beforeBreadcrumb are async, user can fetch some data

0 commit comments

Comments
 (0)