From 702794c8feab387772a8d9913df847c55e8842d5 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Wed, 6 Sep 2017 18:37:42 +0200 Subject: Improve error handling --- lib/getMeetingpointData.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/getMeetingpointData.js b/lib/getMeetingpointData.js index 9a51cfb..94cf36c 100644 --- a/lib/getMeetingpointData.js +++ b/lib/getMeetingpointData.js @@ -59,7 +59,7 @@ function getValidWeekNumbers(page) { function requestData() { lastUpdate = new Date() - return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`).then((response) => { + return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`, { timeout: 5000 }).then((response) => { const page = cheerio.load(response.body) const users = getUsers(page) const validWeekNumbers = getValidWeekNumbers(page) @@ -71,17 +71,10 @@ function requestData() { } function getMeetingpointData () { - if (lastUpdate == null) { + if (lastUpdate == null || new Date() - lastUpdate > 10 * 60 * 1000) { // 10 minutes return requestData() } else if (!meetingpointData) { - // The first request hasn't resolved yet, throw an error. return Promise.reject() - } else if (new Date() - lastUpdate > 10 * 60 * 1000) { // 10 minutes - return requestData().catch(function () { - console.warn('Unable to update userIndex, using cached.') - - return meetingpointData - }) } else { return Promise.resolve(meetingpointData) } -- cgit v1.1