Skip to content

fix(troubleshooting): Update deprecated Hub code #9607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions docs/platforms/javascript/common/troubleshooting/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -198,26 +198,26 @@ end
```javascript
const SENTRY_HOST = "oXXXXXX.ingest.sentry.io"
const SENTRY_PROJECT_IDS = ["123456"]

export const postAction = async ({ request }) => {
try {
const envelope = await request.text();
const piece = envelope.split("\n")[0];
const header = JSON.parse(piece);
const dsn = new URL(header["dsn"]);
const project_id = dsn.pathname?.replace("/", "");

if (dsn.hostname !== SENTRY_HOST) {
throw new Error(`Invalid sentry hostname: ${dsn.hostname}`);
}

if (!project_id || !SENTRY_PROJECT_IDS.includes(project_id)) {
throw new Error(`Invalid sentry project id: ${project_id}`);
}

const upstream_sentry_url = `https://${SENTRY_HOST}/api/${project_id}/envelope/`;
await fetch(upstream_sentry_url, { method: "POST", body: envelope });

return json({}, { status: 200 });
} catch (e) {
console.error("error tunneling to sentry", e);
Expand Down Expand Up @@ -298,7 +298,7 @@ import {
defaultStackParser,
defaultIntegrations,
makeFetchTransport,
Hub,
Scope,
} from "@sentry/browser";

const client = new BrowserClient({
Expand All @@ -308,12 +308,13 @@ const client = new BrowserClient({
integrations: defaultIntegrations,
});

// You have to bind the client to a hub, otherwise integrations will not run!
const hub = new Hub(client);
// Or, if you already have a hub you want to re-use, you can also do:
// hub.bindClient(client);
const scope = new Scope();
scope.setClient(client);

client.init() // initializing has to be done after setting the client on the scope

hub.captureException(new Error("example"));
// You can capture exceptions manually for this client like this:
scope.captureException(new Error("example"));
```

You can now customize the hub to your liking, without affecting other hubs/clients.
Expand Down