about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2023-09-14 12:07:05 +0200
committerAlan Pearce2023-09-15 19:24:53 +0200
commit7bb417f923bdaac08d0d48ef1df6d191e5030bd6 (patch)
treed55f97948f193417ac5de028cb9b02c62da20547
parenta9f9c066c69f3312946cf18834ea86bd51db23fb (diff)
downloadhomestead-7bb417f923bdaac08d0d48ef1df6d191e5030bd6.tar.lz
homestead-7bb417f923bdaac08d0d48ef1df6d191e5030bd6.tar.zst
homestead-7bb417f923bdaac08d0d48ef1df6d191e5030bd6.zip
Catch errors and log to sentry
-rwxr-xr-xbun.lockbbin2683 -> 7240 bytes
-rw-r--r--package.json1
-rw-r--r--src/index.ts82
3 files changed, 47 insertions, 36 deletions
diff --git a/bun.lockb b/bun.lockb
index bea9f9d..2608647 100755
--- a/bun.lockb
+++ b/bun.lockb
Binary files differdiff --git a/package.json b/package.json
index f51b68d..acb0224 100644
--- a/package.json
+++ b/package.json
@@ -12,6 +12,7 @@
   },
   "type": "module",
   "dependencies": {
+    "@sentry/node": "^7.69.0",
     "bun-prometheus-client": "^0.0.2",
     "toml": "^3.0.0"
   }
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);
   },
 });