Catch errors and log to sentry
Alan Pearce alan@alanpearce.eu
Thu, 14 Sep 2023 12:07:05 +0200
3 files changed, 47 insertions(+), 36 deletions(-)
M package.json → package.json
@@ -12,6 +12,7 @@ "typescript": "^5.0.0" }, "type": "module", "dependencies": { + "@sentry/node": "^7.69.0", "bun-prometheus-client": "^0.0.2", "toml": "^3.0.0" }
M src/index.ts → src/index.ts
@@ -1,9 +1,12 @@ import path from "node:path"; import fs, { Stats } from "node:fs"; import type { BunFile, Serve } from "bun"; +import * as Sentry from "@sentry/node"; import prom from "bun-prometheus-client"; import readConfig from "./config"; + +Sentry.init({}); const base = "./website/"; const publicDir = path.resolve(base, "public") + path.sep; @@ -148,44 +151,51 @@ ); const server = Bun.serve({ fetch: async function (request) { - const pathname = new URL(request.url).pathname; - const file = files.get(pathname); - counters.requestsByPath.inc({ path: pathname }); - if (file) { - if ( - parseIfModifiedSinceHeader(request.headers.get("if-modified-since")) >= - file?.mtime.getTime() - ) { - counters.requestsByStatus.inc({ status_code: 304 }); - return new Response("", { status: 304, headers: defaultHeaders }); + try { + const pathname = new URL(request.url).pathname; + const file = files.get(pathname); + counters.requestsByPath.inc({ path: pathname }); + if (file) { + if ( + parseIfModifiedSinceHeader( + request.headers.get("if-modified-since"), + ) >= file?.mtime.getTime() + ) { + counters.requestsByStatus.inc({ status_code: 304 }); + return new Response("", { status: 304, headers: defaultHeaders }); + } + const encodings = (request.headers.get("accept-encoding") || "") + .split(",") + .map((x) => x.trim().toLowerCase()); + if (encodings.includes("br") && files.has(file.relPath + ".br")) { + return serveEncodedFile(files.get(file.relPath + ".br"), 200, { + "content-encoding": "br", + "content-type": file.handle.type, + }); + } else if ( + encodings.includes("zstd") && + files.has(file.relPath + ".zst") + ) { + return serveEncodedFile(files.get(file.relPath + ".zst"), 200, { + "content-encoding": "zstd", + "content-type": file.handle.type, + }); + } else if ( + encodings.includes("gzip") && + files.has(file.relPath + ".gz") + ) { + return serveEncodedFile(files.get(file.relPath + ".gz"), 200, { + "content-encoding": "gzip", + "content-type": file.handle.type, + }); + } } - const encodings = (request.headers.get("accept-encoding") || "") - .split(",") - .map((x) => x.trim().toLowerCase()); - if (encodings.includes("br") && files.has(file.relPath + ".br")) { - return serveEncodedFile(files.get(file.relPath + ".br"), 200, { - "content-encoding": "br", - "content-type": file.handle.type, - }); - } else if ( - encodings.includes("zstd") && - files.has(file.relPath + ".zst") - ) { - return serveEncodedFile(files.get(file.relPath + ".zst"), 200, { - "content-encoding": "zstd", - "content-type": file.handle.type, - }); - } else if ( - encodings.includes("gzip") && - files.has(file.relPath + ".gz") - ) { - return serveEncodedFile(files.get(file.relPath + ".gz"), 200, { - "content-encoding": "gzip", - "content-type": file.handle.type, - }); - } + return serveFile(file); + } catch (error) { + counters.requestsByStatus.inc({ status_code: 503 }); + Sentry.captureException(error); + return new Response("Something went wrong", { status: 503 }); } - return serveFile(file); }, });