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 posterRoot = await tmdb
const getProot = () =>
tmdb
.get("/configuration", {
params: {
api_key: process.env.TMDB_KEY,
},
})
.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());
@ -69,7 +76,7 @@ const tmdb = axios.create({
res.send({ success: true, msg: "Successfully created user." });
} catch (e) {
console.log(e);
console.error("ERR on /register", e.message);
res.statusCode = 500;
res.send({
@ -103,8 +110,6 @@ const tmdb = axios.create({
const verifyUser = async (req, res, next) => {
if (!req.headers["authentication"]) {
console.log(req.headers);
res.statusCode = 401;
return res.send({
success: false,
@ -129,6 +134,7 @@ const tmdb = axios.create({
};
app.get("/search/:query", verifyUser, async (req, res) => {
try {
const results = (
await tmdb.get(`/search/movie`, {
params: {
@ -152,15 +158,27 @@ const tmdb = axios.create({
);
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) => {
try {
const fromDB = await Movie.findOne({ movieId: req.params.id });
if (!fromDB) {
const result = await tmdb.get(`/movie/${req.params.id}`, {
api_key: process.env.TMDB_KEY,
});
const result = (
await tmdb.get(`/movie/${req.params.id}`, {
params: { api_key: process.env.TMDB_KEY },
})
).data;
const movie = {
poster: posterRoot + result.poster_path,
@ -193,9 +211,19 @@ const tmdb = axios.create({
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) => {
try {
const userId = req["Container"].user._id;
const movieId = req.params.id;
@ -203,14 +231,18 @@ const tmdb = axios.create({
if (!movieId) {
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)) {
res.statusCode = 400;
return res.send({
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 });
} catch {
res.statusCode = 500;
res.send({
success: false,
msg: "An error occured, please try again",
});
}
});
app.listen(process.env.PORT, () =>

Loading…
Cancel
Save