Add error handling for plexApi

This commit is contained in:
mike.rode
2019-02-28 00:01:47 -06:00
parent 73552b09a4
commit cf16fb3bde
3 changed files with 43 additions and 13 deletions

View File

@@ -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",

View File

@@ -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) => {

View File

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