This commit is contained in:
2025-09-13 21:21:17 +02:00
parent 40be8b803e
commit 84bc7b5853
2 changed files with 14 additions and 10 deletions

View File

@@ -4,9 +4,10 @@ import Joi from 'joi';
import j2s from "joi-to-swagger";
export class ApiError extends Error {
constructor(status, message) {
constructor(status, message, cause) {
super(message);
this.status = status;
this.cause = cause;
}
}
@@ -234,9 +235,9 @@ router.post("/games", asyncHandler(async (req, res) => {
await conn.commit();
res.status(201).json({ game: gameRow[0], players });
} catch (_err) {
} catch (err) {
await conn.rollback();
throw new ApiError(500, "Failed to create game");
throw new ApiError(500, "Failed to create game", err);
} finally {
conn.release();
}
@@ -299,9 +300,9 @@ router.post("/games/:id/players", asyncHandler(async (req, res) => {
await conn.commit();
res.status(201).json({ game: gameId, players });
} catch (_err) {
} catch (err) {
await conn.rollback();
throw new ApiError(500, "Failed to add player");
throw new ApiError(500, "Failed to add player", err);
} finally {
conn.release();
}
@@ -372,9 +373,9 @@ router.patch("/games/:id/lock", asyncHandler(async (req, res) => {
await conn.commit();
res.json({ message: "Game locked", turnOrder });
} catch (_err) {
} catch (err) {
await conn.rollback();
throw new ApiError(500, "Failed to lock game");
throw new ApiError(500, "Failed to lock game", err);
} finally {
conn.release();
}
@@ -484,9 +485,9 @@ router.post("/games/:id/turns", asyncHandler(async (req, res) => {
await conn.commit();
res.status(201).json({ turnId: result.insertId, nextPlayer });
} catch (_err) {
} catch (err) {
await conn.rollback();
throw new ApiError(500, "Failed to record turn");
throw new ApiError(500, "Failed to record turn", err);
} finally {
conn.release();
}

View File

@@ -48,11 +48,14 @@ app.use("/api/docs", swaggerUi.serve, swaggerUi.setup(specs));
app.use("/api", apiRouter);
app.use((err, req, res, next) => {
console.error(err);
if (err instanceof ApiError) {
if (err.cause != null) {
console.error(err.cause);
}
return res.status(err.status).json({ status: "error", error: err.message });
}
console.error(err);
res.status(500).json({ status: "error", error: "Internal server error" });
});