diff --git a/server/index.js b/server/index.js index f002758f..3c37ff80 100755 --- a/server/index.js +++ b/server/index.js @@ -14,6 +14,7 @@ process.on('uncaughtException', err => require('./config/errorHandler')(err)); // Register middlewares app.use(express.json()); +app.use(require('./middlewares/error')); // Register routes app.use("/api/config", require('./routes/config')); diff --git a/server/middlewares/error.js b/server/middlewares/error.js new file mode 100644 index 00000000..4e6ad600 --- /dev/null +++ b/server/middlewares/error.js @@ -0,0 +1,5 @@ +module.exports = (err, req, res, next) => { + if (!(err instanceof SyntaxError)) return next(); + + res.status(400).json({message: "You need to provide a valid JSON body"}); +} \ No newline at end of file diff --git a/server/routes/config.js b/server/routes/config.js index 1aa15caa..4395e9b6 100644 --- a/server/routes/config.js +++ b/server/routes/config.js @@ -19,7 +19,7 @@ app.get("/", password(true), async (req, res) => { // Updates a specific config entry app.patch("/:key", password(false), async (req, res) => { - if (!req.body.value.toString()) return res.status(400).json({message: "You need to provide the new value"}); + if (!req.body.value?.toString()) return res.status(400).json({message: "You need to provide the new value"}); if ((req.params.key === "ping" || req.params.key === "download" || req.params.key === "upload") && isNaN(req.body.value)) return res.status(400).json({message: "You need to provide a number in order to change this"});