Skip to content

Commit

Permalink
add crappy global catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Govorunb committed Jun 13, 2024
1 parent 1a73969 commit ab0e8c4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ebs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dotenv();
const port = 3000;

export const { app } = expressWs(express());
app.use(cors({ origin: "*" }))
app.use(cors({ origin: "*" }));
app.use(bodyParser.json());
app.use("/public/*", publicApiAuth);
app.use("/private/*", privateApiAuth);
Expand Down
9 changes: 5 additions & 4 deletions ebs/src/modules/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { app } from "../index";
import { sendPubSubMessage } from "../util/pubsub";
import { strToU8, compressSync, strFromU8 } from "fflate";
import { getBannedUsers } from "../util/db";
import { asyncCatch } from "../util/middleware";

let activeConfig: Config | undefined;
let configData: Config | undefined;
Expand Down Expand Up @@ -76,17 +77,17 @@ async function refreshConfig() {
activeConfig = processConfig(configData);
}

app.get("/private/refresh", async (_, res) => {
app.get("/private/refresh", asyncCatch(async (_, res, next) => {
await refreshConfig();
console.log("Refreshed config, new config version is ", activeConfig!.version);
await broadcastConfigRefresh(activeConfig!);
res.sendStatus(200);
});
}));

app.get("/public/config", async (req, res) => {
app.get("/public/config", asyncCatch(async (req, res) => {
const config = await getConfig();
res.send(JSON.stringify(config));
});
}));

(async () => {
const config = await getConfig();
Expand Down
2 changes: 1 addition & 1 deletion ebs/src/modules/game/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class GameConnection {
console.log(`Game socket error\n${error}`);
})
}
public async processMessage(msg: GameMessage) {
public processMessage(msg: GameMessage) {
switch (msg.messageType) {
case MessageType.Hello:
this.handshake = true;
Expand Down
17 changes: 9 additions & 8 deletions ebs/src/modules/game/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { app } from "../../index";
import { asyncCatch } from "../../util/middleware";
import { GameConnection } from "./connection";
import { MessageType } from "./messages";
import { ResultMessage } from "./messages.game";
Expand All @@ -7,11 +8,11 @@ import { StressTestRequest, isStressTesting, startStressTest } from "./stresstes

export let connection: GameConnection = new GameConnection();

app.ws("/private/socket", async (ws, req) => {
app.ws("/private/socket", (ws, req) => {
connection.setSocket(ws);
})
});

app.post("/private/redeem", async (req, res) => {
app.post("/private/redeem", asyncCatch(async (req, res) => {
//console.log(req.body);
const msg = {
...connection.makeMessage(MessageType.Redeem),
Expand All @@ -29,9 +30,9 @@ app.post("/private/redeem", async (req, res) => {
} catch (e) {
res.status(500).send(e);
}
})
}));

app.post("/private/setresult", async (req, res) => {
app.post("/private/setresult", (req, res) => {
//console.log(req.body);
const msg = {
...connection.makeMessage(MessageType.Result),
Expand All @@ -46,7 +47,7 @@ app.post("/private/setresult", async (req, res) => {
res.sendStatus(200);
});

app.post("/private/stress", async (req, res) => {
app.post("/private/stress", (req, res) => {
if (!process.env.ENABLE_STRESS_TEST) {
res.status(501).send("Disabled unless you set the ENABLE_STRESS_TEST env var\nREMEMBER TO REMOVE IT FROM PROD");
return;
Expand All @@ -72,12 +73,12 @@ app.post("/private/stress", async (req, res) => {
res.sendStatus(200);
})

app.get("/private/unsent", async (req, res) => {
app.get("/private/unsent", (req, res) => {
const unsent = connection.getUnsent();
res.send(JSON.stringify(unsent));
})

app.get("/private/outstanding", async (req, res) => {
app.get("/private/outstanding", (req, res) => {
const outstanding = connection.getOutstanding();
res.send(JSON.stringify(outstanding));
})
13 changes: 7 additions & 6 deletions ebs/src/modules/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { logToDiscord } from "../util/logger";
import { connection } from "./game";
import { TwitchUser } from "./game/messages";
import { getHelixUser } from "../util/twitch";
import { asyncCatch } from "../util/middleware";

app.post("/public/prepurchase", async (req, res) => {
app.post("/public/prepurchase", asyncCatch(async (req, res) => {
const cart = req.body as Cart;
const idCart = { ...cart, userId: req.twitchAuthorization!.user_id! };

Expand Down Expand Up @@ -88,9 +89,9 @@ app.post("/public/prepurchase", async (req, res) => {
}).then();

res.status(200).send(token);
});
}));

app.post("/public/transaction", async (req, res) => {
app.post("/public/transaction", asyncCatch(async (req, res) => {
const transaction = req.body as Transaction;

if (!transaction.receipt) {
Expand Down Expand Up @@ -297,9 +298,9 @@ app.post("/public/transaction", async (req, res) => {
}).then();
res.status(500).send(`Failed to process redeem - ${error}`);
}
});
}));

app.post("/public/transaction/cancel", async (req, res) => {
app.post("/public/transaction/cancel", asyncCatch(async (req, res) => {
const token = req.body.token as string;

// remove transaction from db
Expand All @@ -324,7 +325,7 @@ app.post("/public/transaction/cancel", async (req, res) => {

res.sendStatus(404);
}
});
}));

async function getTwitchUser(id: string): Promise<TwitchUser | null> {
const user = await getHelixUser(id);
Expand Down
10 changes: 10 additions & 0 deletions ebs/src/util/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ declare global {
}
}
}

export function asyncCatch(fn: (req: Request, res: Response, next: NextFunction) => Promise<void>) {
return async (req: Request, res: Response, next: NextFunction) => {
try {
await fn(req, res, next);
} catch (err) {
next(err);
}
};
}

0 comments on commit ab0e8c4

Please sign in to comment.