From 3b27af1f83fe765b2e3ea63270734d1c49f3329f Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Fri, 26 Jul 2019 21:52:20 +0200 Subject: [PATCH] Error handling for themoviedb api response codes that are not 200. Started with 401 and 404. See issue #116 for info. --- seasoned_api/src/tmdb/tmdb.js | 23 ++++++++++++++++--- .../webserver/controllers/info/movieInfo.js | 1 - .../controllers/request/requestTmdbId.js | 13 ++++++----- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/seasoned_api/src/tmdb/tmdb.js b/seasoned_api/src/tmdb/tmdb.js index 37011a8..8c16f40 100644 --- a/seasoned_api/src/tmdb/tmdb.js +++ b/seasoned_api/src/tmdb/tmdb.js @@ -2,6 +2,7 @@ const moviedb = require('km-moviedb'); const convertTmdbToMovie = require('src/tmdb/convertTmdbToMovie'); const convertTmdbToShow = require('src/tmdb/convertTmdbToShow'); const convertTmdbToPerson = require('src/tmdb/convertTmdbToPerson'); +const { tmdbInfo } = require('src/tmdb/types') class TMDB { constructor(cache, apiKey, tmdbLibrary) { @@ -86,7 +87,15 @@ class TMDB { return Promise.resolve() .then(() => this.cache.get(cacheKey)) .catch(() => Promise.all(requests)) - .catch((error) => { console.log(error); throw new Error('Could not find a movie with that id.'); }) + .catch(error => { + if (error.status === 401) { + throw new Error('Unathorized tmdb request, please check api key.') + } else if (error.status === 404) { + throw new Error(`Could not find a movie with id: ${identifier}`) + } + + throw new Error('Unexpected error has occured:', error.message) + }) .then(([movies, credits]) => this.cache.set(cacheKey, [movies, credits])) .then(([movies, credits]) => convertTmdbToMovie(movies, credits)) } @@ -109,8 +118,16 @@ class TMDB { return Promise.resolve() .then(() => this.cache.get(cacheKey)) - .catch(() => Promise.all(requests)) - .catch(() => { throw new Error('Could not find a show with that id.'); }) + .catch(() => Promise.all(requests)) + .catch(error => { + if (error.status === 401) { + throw new Error('Unathorized tmdb request, please check api key.') + } else if (error.status === 404) { + throw new Error(`Could not find a show with id: ${identifier}`) + } + + throw new Error('Unexpected error has occured:', error.message) + }) .then(([shows, credits]) => this.cache.set(cacheKey, [shows, credits])) .then(([shows, credits]) => convertTmdbToShow(shows, credits)) } diff --git a/seasoned_api/src/webserver/controllers/info/movieInfo.js b/seasoned_api/src/webserver/controllers/info/movieInfo.js index 896f656..8b2f565 100644 --- a/seasoned_api/src/webserver/controllers/info/movieInfo.js +++ b/seasoned_api/src/webserver/controllers/info/movieInfo.js @@ -20,7 +20,6 @@ async function movieInfoController(req, res) { plex.existsInPlex(movie) .catch((error) => { console.log('Error when searching plex'); }) .then(() => { - console.log('movie', movie) res.send(movie); }).catch((error) => { res.status(404).send({ success: false, error: error.message }); diff --git a/seasoned_api/src/webserver/controllers/request/requestTmdbId.js b/seasoned_api/src/webserver/controllers/request/requestTmdbId.js index 81092b4..5604252 100644 --- a/seasoned_api/src/webserver/controllers/request/requestTmdbId.js +++ b/seasoned_api/src/webserver/controllers/request/requestTmdbId.js @@ -22,30 +22,31 @@ const tmdbShowInfo = (id) => { */ function requestTmdbIdController(req, res) { const { id, type } = req.body - console.log('body', req.body) - console.log('id & type', id, type) const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; const user_agent = req.headers['user-agent']; const user = req.loggedInUser; + let mediaFunction = undefined + if (id === undefined || type === undefined) { + res.status(422).send({ success: false, message: "'Missing parameteres: 'id' and/or 'type'"}) + } + if (type === 'movie') { - console.log('movie') mediaFunction = tmdbMovieInfo } else if (type === 'show') { - console.log('show') mediaFunction = tmdbShowInfo } else { res.status(422).send({ success: false, error: 'Incorrect type. Allowed types: "movie" or "show"'}) } mediaFunction(id) - .catch((error) => { console.error(error); res.status(404).send({ success: false, error: 'Id not found' }) }) + // .catch((error) => { console.error(error); res.status(404).send({ success: false, error: 'Id not found' }) }) .then((tmdbMedia) => request.requestFromTmdb(tmdbMedia, ip, user_agent, user)) .then(() => res.send({success: true, message: 'Request has been submitted.'})) .catch((error) => { - res.status(501).send({ success: false, error: error.message }); + res.send({ success: false, error: error.message }); }) }