From 19bb1942d85e332e7919cbdc62c98e94478206a7 Mon Sep 17 00:00:00 2001 From: leca Date: Wed, 23 Jul 2025 18:09:54 +0300 Subject: [PATCH] error handling --- src/controllers/user.js | 49 ++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/controllers/user.js b/src/controllers/user.js index 9f19858..d77cfdb 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -30,20 +30,20 @@ class UserController { utils.removeFromFile('./inviteTokens.txt', req.body.inviteToken); } - const token = jwt.sign({ username }, config.secret, {expiresIn: "1y"}); + const token = jwt.sign({ username }, config.secret, { expiresIn: "1y" }); res.cookie("jwt", token); return res.status(200).send("Ok"); } async login(req, res) { - const {username, password} = req.body; + const { username, password } = req.body; const storedPassword = await UserService.getPassword(username); if (!bcrypt.compareSync(password, storedPassword)) { return res.status(403).send("Password is not correct"); } - const token = jwt.sign({ username }, config.secret, {expiresIn: "1y"}); + const token = jwt.sign({ username }, config.secret, { expiresIn: "1y" }); res.cookie("jwt", token); return res.status(200).send("Ok"); } @@ -81,19 +81,23 @@ class UserController { if (path.extname(req.file.originalname).toLowerCase() !== ".png") { return res.status(400).send("Only .png files are allowed!"); } - - const image = await Jimp.read(tempPath); - const allowedWidths = [32, 64, 128]; - const allowedHeights = [32, 64]; + try { + const image = await Jimp.read(tempPath); + } catch (err) { + console.log(`Error during cape installation, ${err}`) + return res.status(500).send("Internal server error. Please, report to the developer.") + } + const allowedWidths = [32, 64, 128]; + const allowedHeights = [32, 64]; if (!allowedWidths.includes(image.bitmap.width) || !allowedHeights.includes(image.bitmap.height)) { return res.status(400).send('This does not look like a minecraft skin.'); } - try { - await fs.promises.cp(tempPath, targetPath); - await fs.promises.unlink(tempPath); - } catch (err) { - return res.status(500).send("Ooops! Something went wrong! Please, report to the developer."); - } + try { + await fs.promises.cp(tempPath, targetPath); + await fs.promises.unlink(tempPath); + } catch (err) { + return res.status(500).send("Ooops! Something went wrong! Please, report to the developer."); + } return res.status(200).send("Skin uploaded!"); } @@ -106,18 +110,23 @@ class UserController { if (path.extname(req.file.originalname).toLowerCase() !== ".png") { return res.status(400).send("Only .png files are allowed!"); } - - const image = await Jimp.read(tempPath); + try { + const image = await Jimp.read(tempPath); + } + catch (err) { + console.log(`Error during cape installation, ${err}`) + return res.status(500).send("Internal server error. Please, report to the developer.") + } if ((image.bitmap.width != 64 || image.bitmap.height != 32) && (image.bitmap.width != 128 || image.bitmap.height != 64)) { return res.status(400).send('This does not look like a minecraft cape.'); } try { - await fs.promises.cp(tempPath, targetPath); - await fs.promises.unlink(tempPath); - } catch (err) { - return res.status(500).send("Ooops! Something went wrong! Please, report to the developer."); - } + await fs.promises.cp(tempPath, targetPath); + await fs.promises.unlink(tempPath); + } catch (err) { + return res.status(500).send("Ooops! Something went wrong! Please, report to the developer."); + } return res.status(200).send("Cape uploaded!"); }