Skip to content

Commit

Permalink
Bugfix: Catch errors in api/shorten/expand
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqksoliman committed Mar 25, 2024
1 parent 3b57a2b commit 9a99195
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions API/Backend/Shortener/routes/shortener.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 9a99195

Please sign in to comment.