From f1a408a515505ab1833f2f1c8fe647ee7f2102ee Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Fri, 16 Feb 2018 23:12:52 +0100 Subject: Return data without alts if alts fail --- src/server/lib/getMeetingpointData.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/server/lib/getMeetingpointData.js b/src/server/lib/getMeetingpointData.js index 12bcd28..2e8a6ea 100644 --- a/src/server/lib/getMeetingpointData.js +++ b/src/server/lib/getMeetingpointData.js @@ -63,10 +63,10 @@ function combineUsers(usersArrays) { function getAlts(users) { const requests = users.map(user => - request(getUrlOfUser('dag', user.type, user.index, 7), { encoding: null })); + request(getUrlOfUser('dag', user.type, user.index, 7), { timeout: 8000, encoding: null })); - return Promise.all(requests).then((teacherResponses) => { - return teacherResponses.map((teacherResponse, index) => { + return Promise.all(requests).then(teacherResponses => + teacherResponses.map((teacherResponse, index) => { const utf8Body = iconv.decode(teacherResponse.body, 'iso-8859-1'); const teacherResponseBody = cheerio.load(utf8Body); @@ -76,8 +76,7 @@ function getAlts(users) { ...users[index], alt: teacherName, }; - }); - }); + })); } function getMeetingpointData() { @@ -97,11 +96,19 @@ function getMeetingpointData() { const teachers = users.filter(user => user.type === 't'); - return getAlts(teachers).then(teachersWithAlts => ({ - users: combineUsers([teachersWithAlts, users]), - dailyScheduleWeeks, - basisScheduleWeeks, - })); + return getAlts(teachers) + .then(teachersWithAlts => ({ + users: combineUsers([teachersWithAlts, users]), + dailyScheduleWeeks, + basisScheduleWeeks, + })) + .catch(() => ({ + // Just return the user data without the alts if getAlts fails, since + // the alts are non-essential. + users, + dailyScheduleWeeks, + basisScheduleWeeks, + })); }); } -- cgit v1.1