Skip to content

Slight improvement to example code of the javascript option for tunnel FAQ #93490

Closed as not planned
@shivan-eyespace

Description

@shivan-eyespace

Sorry I didn't know what template to use so apologies in advance.

I want to make a request to improvement documentation - https://docs.sentry.io/platforms/javascript/troubleshooting/#using-the-tunnel-option

I wanted to create an issue first to discuss and I am happy to submit a PR as well.

In the code snippet it below:

const SENTRY_HOST = "oXXXXXX.ingest.sentry.io";
const SENTRY_PROJECT_IDS = ["123456"];

export const postAction = async ({ request }) => {
  try {
    const envelopeBytes = await req.arrayBuffer();
    const envelope = new TextDecoder().decode(envelopeBytes);
    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: envelopeBytes,
    });

    return json({}, { status: 200 });
  } catch (e) {
    console.error("error tunneling to sentry", e);
    return json({ error: "error tunneling to sentry" }, { status: 500 });
  }
};

Would it be useful to check if piece is an empty string '' other wise you will get not useful Uncaught SyntaxError: Unexpected end of JSON input errors.

Keen to see what others think unless I am doing something wrong, thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Waiting for: Support

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions