Zpracování chyb z API
This commit is contained in:
@@ -7,7 +7,7 @@ import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
import { getQr } from "./qr";
|
||||
import { generateToken, getLogin, getTrusted, verify } from "./auth";
|
||||
import { getDayOfWeekIndex } from "./utils";
|
||||
import { InsufficientPermissions, getDayOfWeekIndex } from "./utils";
|
||||
|
||||
const ENVIRONMENT = process.env.NODE_ENV || 'production';
|
||||
dotenv.config({ path: path.resolve(__dirname, `./.env.${ENVIRONMENT}`) });
|
||||
@@ -219,7 +219,7 @@ app.post("/api/finishDelivery", async (req, res) => {
|
||||
res.status(200).json({});
|
||||
});
|
||||
|
||||
app.post("/api/addChoice", async (req, res) => {
|
||||
app.post("/api/addChoice", async (req, res, next) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
const trusted = getTrusted(parseToken(req));
|
||||
if (req.body.locationIndex > -1) {
|
||||
@@ -233,15 +233,18 @@ app.post("/api/addChoice", async (req, res) => {
|
||||
}
|
||||
date = getDateForWeekIndex(dayIndex);
|
||||
}
|
||||
const data = await addChoice(login, trusted, req.body.locationIndex, req.body.foodIndex, date);
|
||||
io.emit("message", data);
|
||||
return res.status(200).json(data);
|
||||
try {
|
||||
const data = await addChoice(login, trusted, req.body.locationIndex, req.body.foodIndex, date);
|
||||
io.emit("message", data);
|
||||
return res.status(200).json(data);
|
||||
} catch (e: any) { next(e) }
|
||||
}
|
||||
return res.status(400); // TODO přidat popis chyby
|
||||
});
|
||||
|
||||
app.post("/api/removeChoices", async (req, res) => {
|
||||
app.post("/api/removeChoices", async (req, res, next) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
const trusted = getTrusted(parseToken(req));
|
||||
let date = undefined;
|
||||
if (req.body.dayIndex != null) {
|
||||
let dayIndex;
|
||||
@@ -252,13 +255,16 @@ app.post("/api/removeChoices", async (req, res) => {
|
||||
}
|
||||
date = getDateForWeekIndex(dayIndex);
|
||||
}
|
||||
const data = await removeChoices(login, req.body.locationIndex, date);
|
||||
io.emit("message", data);
|
||||
res.status(200).json(data);
|
||||
try {
|
||||
const data = await removeChoices(login, trusted, req.body.locationIndex, date);
|
||||
io.emit("message", data);
|
||||
res.status(200).json(data);
|
||||
} catch (e: any) { next(e) }
|
||||
});
|
||||
|
||||
app.post("/api/removeChoice", async (req, res) => {
|
||||
app.post("/api/removeChoice", async (req, res, next) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
const trusted = getTrusted(parseToken(req));
|
||||
let date = undefined;
|
||||
if (req.body.dayIndex != null) {
|
||||
let dayIndex;
|
||||
@@ -269,9 +275,11 @@ app.post("/api/removeChoice", async (req, res) => {
|
||||
}
|
||||
date = getDateForWeekIndex(dayIndex);
|
||||
}
|
||||
const data = await removeChoice(login, req.body.locationIndex, req.body.foodIndex, date);
|
||||
io.emit("message", data);
|
||||
res.status(200).json(data);
|
||||
try {
|
||||
const data = await removeChoice(login, trusted, req.body.locationIndex, req.body.foodIndex, date);
|
||||
io.emit("message", data);
|
||||
res.status(200).json(data);
|
||||
} catch (e: any) { next(e) }
|
||||
});
|
||||
|
||||
// TODO přejmenovat, ať je jasné, že to patří k Pizza day
|
||||
@@ -302,6 +310,16 @@ app.post("/api/changeDepartureTime", async (req, res) => {
|
||||
res.status(200).json(data);
|
||||
});
|
||||
|
||||
// Middleware pro zpracování chyb
|
||||
app.use((err: any, req: any, res: any, next: any) => {
|
||||
if (err instanceof InsufficientPermissions) {
|
||||
res.status(403).send({ error: err.message })
|
||||
} else {
|
||||
res.status(500).send({ error: err.message })
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
console.log(`New client connected: ${socket.id}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user