diff options
author | Determinant <[email protected]> | 2019-01-31 15:14:28 -0500 |
---|---|---|
committer | Determinant <[email protected]> | 2019-01-31 15:14:28 -0500 |
commit | 374fb92c58c503287dab35c985060916b7c86617 (patch) | |
tree | 65ba4935f71e4e0ed4317dae76505b2628e5c871 /src | |
parent | f9f537e3dd28aa543770aea158e4eff65be0b261 (diff) |
...
Diffstat (limited to 'src')
-rwxr-xr-x | src/App.js | 8 | ||||
-rw-r--r-- | src/gapi.js | 24 |
2 files changed, 21 insertions, 11 deletions
@@ -111,8 +111,12 @@ class Dashboard extends React.Component { console.log(start, end); let event_pms = []; for (let id in this.cached.calendars) - event_pms.push(this.cached.calendars[id].cal.getEvents(start, end).then( - r => { return { id, events: r } })); + event_pms.push(this.cached.calendars[id].cal.getEvents(start, end) + .then(r => { return { id, events: r }; }) + .catch(e => { + console.log(`cannot load calendar ${id}`); + return { id, events: [] }; + })); Promise.all(event_pms).then(all_events => { let events = {}; diff --git a/src/gapi.js b/src/gapi.js index c66a9ff..34fc9e9 100644 --- a/src/gapi.js +++ b/src/gapi.js @@ -1,6 +1,11 @@ /* global chrome */ const gapi_base = 'https://www.googleapis.com/calendar/v3'; +const GApiError = { + invalidSyncToken: 1, + otherError: 2, +}; + function to_params(dict) { return Object.entries(dict).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&'); } @@ -30,7 +35,7 @@ function getEvent(calId, eventId, token) { .then(response => response.json()); } -function getEvents(calId, token, syncToken, resultsPerRequest = '') { +function getEvents(calId, token, syncToken, resultsPerRequest=100) { let results = []; const singleFetch = (pageToken, syncToken) => fetch(`${gapi_base}/calendars/${calId}/events?${to_params({ access_token: token, @@ -41,15 +46,11 @@ function getEvents(calId, token, syncToken, resultsPerRequest = '') { .then(response => { if (response.status === 200) return response.json(); - else throw {}; + else if (response.status == 410) + throw GApiError.invalidSyncToken; + else throw GApiError.otherErrors; }) - .catch(e => e) .then(data => { - if (!data.items) - return ({ - nextSyncToken: '', - results: [] - }); results.push(...data.items); if (data.nextPageToken) { return singleFetch(data.nextPageToken, ''); @@ -165,7 +166,12 @@ export class GCalendar { else if (e.status === 'cancelled') this.removeEvent(e); })); - })); + })).catch(e => { + if (e == GApiError.invalidSyncToken) { + this.syncToken = ''; + this.sync(); + } else throw e; + }); } getEvents(start, end) { |