From 03f3c16e4c5f537eaf70300dac47b735feb2c41e Mon Sep 17 00:00:00 2001 From: Scott Shepherd Date: Fri, 10 Sep 2021 12:07:38 -0500 Subject: [PATCH 1/2] return offset 0 and local Date if there's a error fetching; option to throw errors instead of console.warn --- serverDate.js | 7 ++++--- test.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/serverDate.js b/serverDate.js index 4dc65b6..305f232 100644 --- a/serverDate.js +++ b/serverDate.js @@ -22,9 +22,9 @@ const fetchSampleImplementation = async () => { }; export const getServerDate = async ( - { fetchSample } = { fetchSample: fetchSampleImplementation } + { fetchSample, throwErrors } = { fetchSample: fetchSampleImplementation, throwErrors: false } ) => { - let best = { uncertainty: Number.MAX_VALUE }; + let best = { date: new Date(), offset: 0, uncertainty: Number.MAX_VALUE }; // Fetch 10 samples to increase the chance of getting one with low // uncertainty. @@ -46,7 +46,8 @@ export const getServerDate = async ( }; } } catch (exception) { - console.warn(exception); + if (throwErrors) throw exception; + else console.warn(exception); } } diff --git a/test.js b/test.js index 1a13d95..8f3d379 100644 --- a/test.js +++ b/test.js @@ -77,3 +77,20 @@ it(`synchronizes the time with the server`, async () => { uncertainty: 582.5, }); }); + +it(`returns offset 0 and local Date on error`, async () => { + const fetchSample = async () => { + throw new Error(`oh dang`); + }; + const { date, offset, uncertainty } = await getServerDate({ fetchSample }); + assert(offset === 0); + assert(uncertainty === Number.MAX_VALUE); + assert(Date.now() - date < 100); +}) + +it(`throws errors if you ask it to`, async () => { + const fetchSample = async () => { + throw new Error(`oh dang`); + }; + await assert.rejects(async () => getServerDate({ fetchSample, throwErrors: true })); +}) From 1bb4a701764144d022e46ee5e6e5e1cdab199aaf Mon Sep 17 00:00:00 2001 From: Scott Shepherd Date: Fri, 10 Sep 2021 12:49:06 -0500 Subject: [PATCH 2/2] option to report errors --- serverDate.js | 12 +++++++----- test.js | 6 ++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/serverDate.js b/serverDate.js index 305f232..0a3ebc2 100644 --- a/serverDate.js +++ b/serverDate.js @@ -22,13 +22,15 @@ const fetchSampleImplementation = async () => { }; export const getServerDate = async ( - { fetchSample, throwErrors } = { fetchSample: fetchSampleImplementation, throwErrors: false } + { fetchSample, withErrors } = { fetchSample: fetchSampleImplementation, withErrors: false } ) => { + const fetchCount = 10; + const errors = []; let best = { date: new Date(), offset: 0, uncertainty: Number.MAX_VALUE }; // Fetch 10 samples to increase the chance of getting one with low // uncertainty. - for (let index = 0; index < 10; index++) { + for (let index = 0; index < fetchCount; index++) { try { const { requestDate, responseDate, serverDate } = await fetchSample(); @@ -46,10 +48,10 @@ export const getServerDate = async ( }; } } catch (exception) { - if (throwErrors) throw exception; - else console.warn(exception); + errors.push(exception); + console.warn(exception); } } - return best; + return withErrors ? { ...best, errors, fetchCount } : best; }; diff --git a/test.js b/test.js index 8f3d379..3e9e44e 100644 --- a/test.js +++ b/test.js @@ -88,9 +88,11 @@ it(`returns offset 0 and local Date on error`, async () => { assert(Date.now() - date < 100); }) -it(`throws errors if you ask it to`, async () => { +it(`reports errors if you ask it to`, async () => { const fetchSample = async () => { throw new Error(`oh dang`); }; - await assert.rejects(async () => getServerDate({ fetchSample, throwErrors: true })); + const { errors, fetchCount } = await getServerDate({ fetchSample, withErrors: true }); + assert(errors.length === fetchCount); + assert(errors[0].message === `oh dang`); })