Error handling for themoviedb api response codes that are not 200. Started with 401 and 404. See issue #116 for info.

This commit is contained in:
2019-07-26 21:52:20 +02:00
parent afb7af46b8
commit 3b27af1f83
3 changed files with 27 additions and 10 deletions

View File

@@ -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))
}

View File

@@ -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 });

View File

@@ -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 });
})
}