Browse Source

[feat] better error handling

master
Muthu Kumar 4 years ago
parent
commit
42f675d129
Signed by: mkrhere GPG Key ID: 3FD688398897097E
  1. 61
      index.js

61
index.js

@ -28,15 +28,22 @@ const tmdb = axios.create({
}, },
}); });
(async function main() { const getProot = () =>
const posterRoot = await tmdb tmdb
.get("/configuration", { .get("/configuration", {
params: { params: {
api_key: process.env.TMDB_KEY, api_key: process.env.TMDB_KEY,
}, },
}) })
.then(res => res.data) .then(res => res.data)
.then(data => data.images.base_url + "w500"); .then(data => data.images.base_url + "w500")
.catch(e => {
console.error("ERR on tmdb configuration", e.message);
return getProot();
});
(async function main() {
const posterRoot = await getProot();
app.use(bodyParser.json()); app.use(bodyParser.json());
@ -69,7 +76,7 @@ const tmdb = axios.create({
res.send({ success: true, msg: "Successfully created user." }); res.send({ success: true, msg: "Successfully created user." });
} catch (e) { } catch (e) {
console.log(e); console.error("ERR on /register", e.message);
res.statusCode = 500; res.statusCode = 500;
res.send({ res.send({
@ -103,8 +110,6 @@ const tmdb = axios.create({
const verifyUser = async (req, res, next) => { const verifyUser = async (req, res, next) => {
if (!req.headers["authentication"]) { if (!req.headers["authentication"]) {
console.log(req.headers);
res.statusCode = 401; res.statusCode = 401;
return res.send({ return res.send({
success: false, success: false,
@ -129,6 +134,7 @@ const tmdb = axios.create({
}; };
app.get("/search/:query", verifyUser, async (req, res) => { app.get("/search/:query", verifyUser, async (req, res) => {
try {
const results = ( const results = (
await tmdb.get(`/search/movie`, { await tmdb.get(`/search/movie`, {
params: { params: {
@ -152,15 +158,27 @@ const tmdb = axios.create({
); );
res.send({ success: true, results: movies }); res.send({ success: true, results: movies });
} catch (e) {
console.error("ERR on /search", e.message);
res.statusCode = 500;
res.send({
success: false,
msg: "An error occured, please try again",
});
}
}); });
app.get("/movie/:id", verifyUser, async (req, res) => { app.get("/movie/:id", verifyUser, async (req, res) => {
try {
const fromDB = await Movie.findOne({ movieId: req.params.id }); const fromDB = await Movie.findOne({ movieId: req.params.id });
if (!fromDB) { if (!fromDB) {
const result = await tmdb.get(`/movie/${req.params.id}`, { const result = (
api_key: process.env.TMDB_KEY, await tmdb.get(`/movie/${req.params.id}`, {
}); params: { api_key: process.env.TMDB_KEY },
})
).data;
const movie = { const movie = {
poster: posterRoot + result.poster_path, poster: posterRoot + result.poster_path,
@ -193,9 +211,19 @@ const tmdb = axios.create({
res.send(movie); res.send(movie);
} }
} catch (e) {
console.error("ERR on /movie", e.message);
res.statusCode = 500;
res.send({
success: false,
msg: "An error occured, please try again",
});
}
}); });
app.post("/movie/:id", verifyUser, async (req, res) => { app.post("/movie/:id", verifyUser, async (req, res) => {
try {
const userId = req["Container"].user._id; const userId = req["Container"].user._id;
const movieId = req.params.id; const movieId = req.params.id;
@ -203,14 +231,18 @@ const tmdb = axios.create({
if (!movieId) { if (!movieId) {
res.statusCode = 400; res.statusCode = 400;
return res.send({ success: false, msg: "movieId was not sent." }); return res.send({
success: false,
msg: "movieId was not sent.",
});
} }
if (watchStatus && !WATCH_STATUS.includes(watchStatus)) { if (watchStatus && !WATCH_STATUS.includes(watchStatus)) {
res.statusCode = 400; res.statusCode = 400;
return res.send({ return res.send({
success: false, success: false,
msg: "watchStatus must be one of " + WATCH_STATUS.join(", "), msg:
"watchStatus must be one of " + WATCH_STATUS.join(", "),
}); });
} }
@ -236,6 +268,13 @@ const tmdb = axios.create({
); );
res.send({ success: true }); res.send({ success: true });
} catch {
res.statusCode = 500;
res.send({
success: false,
msg: "An error occured, please try again",
});
}
}); });
app.listen(process.env.PORT, () => app.listen(process.env.PORT, () =>

Loading…
Cancel
Save