From 9a99195e90d838106ac3fecf350a8e81c7deb8bb Mon Sep 17 00:00:00 2001 From: Tariq Soliman Date: Mon, 25 Mar 2024 09:56:49 -0700 Subject: [PATCH] Bugfix: Catch errors in api/shorten/expand --- API/Backend/Shortener/routes/shortener.js | 40 ++++++++++++++++------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/API/Backend/Shortener/routes/shortener.js b/API/Backend/Shortener/routes/shortener.js index 4134a65c..4fef8def 100644 --- a/API/Backend/Shortener/routes/shortener.js +++ b/API/Backend/Shortener/routes/shortener.js @@ -71,26 +71,42 @@ router.post("/shorten", function (req, res, next) { }); router.post("/expand", function (req, res, next) { + if (req.body.short == null) { + res.send({ + status: "failure", + message: "Short URL not defined in body.", + body: {}, + }); + return; + } UrlShortener.findOne({ where: { short: req.body.short, }, - }).then((url) => { - if (!url) { - logger("error", "Failed to find short URL.", req.originalUrl, req); + }) + .then((url) => { + if (!url) { + logger("error", "Failed to find short URL.", req.originalUrl, req); + res.send({ + status: "failure", + message: "Failure to find URL.", + body: {}, + }); + } else { + res.send({ + status: "success", + message: "Successfully shortened URL.", + body: { url: decodeURIComponent(url.full) }, + }); + } + }) + .catch((err) => { res.send({ status: "failure", - message: "Failure to find URL.", + message: "Failed to expand URL.", body: {}, }); - } else { - res.send({ - status: "success", - message: "Successfully shortened URL.", - body: { url: decodeURIComponent(url.full) }, - }); - } - }); + }); }); module.exports = router;