Skip to content

Commit

Permalink
Cleanup sentry (#3537)
Browse files Browse the repository at this point in the history
* Cleanup sentry

* Address PR comments
  • Loading branch information
penalosa authored Jul 5, 2023
1 parent 5a00f25 commit 4ac684b
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions packages/edge-preview-authenticated-proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import cookie from "cookie";
import { Toucan } from "toucan-js";

class HttpError extends Error {
constructor(message: string, readonly status: number) {
constructor(
message: string,
readonly status: number,
// Only report errors to sentry when they represent actionable errors
readonly reportable: boolean
) {
super(message);
Object.setPrototypeOf(this, new.target.prototype);
}
Expand All @@ -12,7 +17,13 @@ class HttpError extends Error {
error: this.name,
message: this.message,
},
{ status: this.status }
{
status: this.status,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Method": "GET,PUT,POST",
},
}
);
}

Expand All @@ -23,7 +34,7 @@ class HttpError extends Error {

class NoExchangeUrl extends HttpError {
constructor() {
super("No exchange_url provided", 400);
super("No exchange_url provided", 400, false);
}
}

Expand All @@ -33,7 +44,7 @@ class ExchangeFailed extends HttpError {
readonly exchangeStatus: number,
readonly body: string
) {
super("Exchange failed", 400);
super("Exchange failed", 400, true);
}

get data(): { url: string; status: number; body: string } {
Expand All @@ -43,19 +54,22 @@ class ExchangeFailed extends HttpError {

class TokenUpdateFailed extends HttpError {
constructor() {
super("Provide token, prewarmUrl and remote", 400);
super("Provide token, prewarmUrl and remote", 400, false);
}
}

class RawHttpFailed extends HttpError {
constructor() {
super("Provide token, and remote", 400);
super("Provide token, and remote", 400, false);
}
}

class PreviewRequestFailed extends HttpError {
constructor() {
super("Provide token, and remote", 400);
constructor(private tokenId: string, reportable: boolean) {
super("Token and remote not found", 400, reportable);
}
get data(): { tokenId: string } {
return { tokenId: this.tokenId };
}
}

Expand Down Expand Up @@ -121,7 +135,8 @@ async function handleRequest(
(await env.TOKEN_LOOKUP.get(tokenId)) ?? "{}"
);
if (!token || !remote) {
throw new PreviewRequestFailed();
// Report this error if a tokenId was provided
throw new PreviewRequestFailed(tokenId, !!tokenId);
}

const original = await fetch(
Expand Down Expand Up @@ -341,9 +356,8 @@ export default {
} catch (e) {
console.error(e);
if (e instanceof HttpError) {
sentry.captureException(e, {
data: { ...e.data },
});
sentry.setContext("extra_details", e.data);
sentry.captureException(e);
return e.toResponse();
} else {
sentry.captureException(e);
Expand Down

0 comments on commit 4ac684b

Please sign in to comment.