From cf16fb3bde1f039aa5338a7b52a262cefb873139 Mon Sep 17 00:00:00 2001 From: "mike.rode" Date: Thu, 28 Feb 2019 00:01:47 -0600 Subject: [PATCH] Add error handling for plexApi --- package.json | 1 + server/services/plex/index.js | 10 +++++--- server/services/plex/plexApi.js | 45 +++++++++++++++++++++++++-------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index cb4ad60..7223f3f 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "custom-env": "^1.0.0", "dotenv": "^6.2.0", "express": "^4.16.4", + "express-validator": "^5.3.1", "lodash": "^4.17.11", "morgan": "^1.9.1", "onchange": "^5.2.0", diff --git a/server/services/plex/index.js b/server/services/plex/index.js index 0f88444..ad72f02 100644 --- a/server/services/plex/index.js +++ b/server/services/plex/index.js @@ -17,9 +17,13 @@ const getUsers = async (req, res) => { }; const getMostWatched = async (req, res) => { - const options = req.query; - const mostWatched = await plexApi.getMostWatched(options); - res.json(mostWatched); + try { + const options = req.query; + const mostWatched = await plexApi.getMostWatched(options); + res.json(mostWatched); + } catch (error) { + res.json(error); + } }; const getSections = async (req, res) => { diff --git a/server/services/plex/plexApi.js b/server/services/plex/plexApi.js index df0e2ea..6c97c8e 100644 --- a/server/services/plex/plexApi.js +++ b/server/services/plex/plexApi.js @@ -46,17 +46,34 @@ const getLibraryDataBySectionUrlParams = function(options) { }; const getUsers = async function() { - const urlParams = getUsersUrlParams(); - const getUsersUrl = helpers.buildUrl(urlParams); - const response = await helpers.request(getUsersUrl); - return response.MediaContainer.User; + try { + const urlParams = getUsersUrlParams(); + const getUsersUrl = helpers.buildUrl(urlParams); + const response = await helpers.request(getUsersUrl); + return response.MediaContainer.User; + } catch (error) { + return { + code: error.status, + message: error.statusText, + url: error.config.url, + }; + } }; const getMostWatched = async function(options) { - const urlParams = mostWatchedUrlParams(options); - const mostWatchedUrl = helpers.buildUrl(urlParams); - const response = await helpers.request(mostWatchedUrl); - return response.MediaContainer.Metadata; + try { + const urlParams = mostWatchedUrlParams(options); + const mostWatchedUrl = helpers.buildUrl(urlParams); + const response = await helpers.request(mostWatchedUrl); + return response.MediaContainer.Metadata; + } catch (error) { + console.log(error); + return { + code: error.status, + message: error.statusText, + url: error.config.url, + }; + } }; const getSections = async function() { @@ -66,7 +83,11 @@ const getSections = async function() { const response = await helpers.request(getSectionsUrl); return response.MediaContainer.Directory; } catch (error) { - return error; + return { + code: error.status, + message: error.statusText, + url: error.config.url, + }; } }; @@ -77,7 +98,11 @@ const getLibraryDataBySection = async function(options) { const response = await helpers.request(getLibraryDataBySectionUrl); return response.MediaContainer.Metadata; } catch (error) { - return error; + return { + code: error.status, + message: error.statusText, + url: error.config.url, + }; } };