diff options
author | Alan Pearce | 2023-09-14 12:07:05 +0200 |
---|---|---|
committer | Alan Pearce | 2023-09-15 19:24:53 +0200 |
commit | 7bb417f923bdaac08d0d48ef1df6d191e5030bd6 (patch) | |
tree | d55f97948f193417ac5de028cb9b02c62da20547 /src | |
parent | a9f9c066c69f3312946cf18834ea86bd51db23fb (diff) | |
download | homestead-7bb417f923bdaac08d0d48ef1df6d191e5030bd6.tar.lz homestead-7bb417f923bdaac08d0d48ef1df6d191e5030bd6.tar.zst homestead-7bb417f923bdaac08d0d48ef1df6d191e5030bd6.zip |
Catch errors and log to sentry
Diffstat (limited to 'src')
-rw-r--r-- | src/index.ts | 82 |
1 files changed, 46 insertions, 36 deletions
diff --git a/src/index.ts b/src/index.ts index f968fda..d979f43 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,13 @@ 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 @@ console.info( 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 }); - } - 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, - }); + 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, + }); + } } + 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); }, }); |