add error boundary

This commit is contained in:
2025-07-02 18:11:59 +00:00
parent 82f6b4eebe
commit 8bffb568e8
7 changed files with 64 additions and 408 deletions

View File

@@ -20,18 +20,6 @@ getStructure({ refresh: true }).catch(console.error);
const app = express();
const server = createServer(app);
// Socket.IO setup with CORS
const ORIGIN_URL =
process.env.NODE_ENV === "production"
? "https://med.tomastm.com"
: "http://localhost:5173";
const io = new Server(server, {
cors: {
origin: ORIGIN_URL,
methods: ["GET", "POST"],
},
});
const corsOptions = {
methods: ["GET", "POST", "PUT", "DELETE"],
credentials: true,
@@ -57,51 +45,28 @@ app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan("tiny"));
// Global
let data = null;
let selectedOptionId = null;
let lookingAtIdx = 0;
// Socket connection handling
io.on("connection", (socket) => {
console.log("Client connected:", socket.id);
socket.emit("dataChanged", data);
socket.emit("selectedOptionChanged", selectedOptionId);
socket.emit("lookingAtIdxChanged", lookingAtIdx);
// Handle array updates from client
socket.on("setData", (newData) => {
data = newData;
console.log("Data changed");
io.emit("dataChanged", data);
});
socket.on("setSelectedOptionId", (newId) => {
selectedOptionId = newId;
console.log("Selected option changed to ", newId);
io.emit("selectedOptionChanged", newId);
});
socket.on("setLookingAtIdx", (newIdx) => {
lookingAtIdx = 0;
console.log("Looking at id changed to ", newIdx);
io.emit("lookingAtIdxChanged", newIdx);
});
socket.on("disconnect", () => {
console.log("Client disconnected:", socket.id);
});
});
app.get("/_health", (req, res) => {
res.json({ healthy: true });
});
app.get(
"/resources-status",
let errors = [];
app.post(
"/log-error",
asyncHandler((req, res) => {
res.json({ array: booleanArray });
const { error, clear } = req.body;
if (clear) {
errors = [];
}
errors.push(error);
res.json({ success: true });
}),
);
app.get(
"/log-error",
asyncHandler((req, res) => {
res.json({ errors });
}),
);
@@ -304,8 +269,14 @@ function errorRequestHandler(error, _req, res, next) {
function isOriginAllowed(origin) {
const url = new URL(origin);
if (url.hostname === "localhost" || url.hostname === "127.0.0.1") {
if (
[
"localhost",
"127.0.0.1",
"personal.workstation.lan",
"personal.utm.local",
].includes(url.hostname)
) {
return true;
}