From 8e1b57382a9342a0d25bcda8d6f3bd0ad53de817 Mon Sep 17 00:00:00 2001 From: Randall Schmidt Date: Fri, 27 Nov 2020 23:31:39 -0500 Subject: [PATCH] dont error if ejectFromCache is called when the response is already not cached --- classes/response.js | 10 ++++++++-- test/tests.js | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/classes/response.js b/classes/response.js index a746fe1..70a354a 100644 --- a/classes/response.js +++ b/classes/response.js @@ -25,8 +25,14 @@ class Response { return this.bodyBuffer; } - ejectFromCache() { - return fs.promises.unlink(this.cacheFilePath); + async ejectFromCache() { + try { + await fs.promises.unlink(this.cacheFilePath); + } catch (err) { + if (err.code !== 'ENOENT') { + throw err; + } + } } } diff --git a/test/tests.js b/test/tests.js index f45ef12..798bb01 100644 --- a/test/tests.js +++ b/test/tests.js @@ -96,6 +96,14 @@ describe('Cache tests', function() { res = await fetch(TWO_HUNDRED_URL); assert.strictEqual(res.fromCache, true); }); + + it('Does not error if rejecting from cache twice', async function() { + res = await fetch(TWO_HUNDRED_URL); + assert.strictEqual(res.fromCache, false); + + await res.ejectFromCache(); + await res.ejectFromCache(); + }); }).timeout(10000); describe('Data tests', function() {