Skip to content

Commit

Permalink
fix: always catch and emit cache write errors in promise (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys authored Jan 29, 2024
1 parent f5135ba commit ed73ef5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/cache/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ class CacheEntry {
const cacheWritePromise = new Promise((resolve, reject) => {
cacheWriteResolve = resolve
cacheWriteReject = reject
}).catch((err) => {
body.emit('error', err)
})

body = new CachingMinipassPipeline({ events: ['integrity', 'size'] }, new MinipassFlush({
Expand Down
24 changes: 24 additions & 0 deletions test/inaccessible-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const nock = require('nock')
const t = require('tap')
const path = require('path')

const fetch = require('../')
nock.disableNetConnect()

t.beforeEach(() => nock.cleanAll())
t.test('catches error for inaccessible cache', async t => {
// a file for the cache which wont work
const cache = t.testdir({
file: '',
})
const req = nock('http://localhost')
.get('/foo')
.reply(() => [200, Buffer.from('text')])

const res = await fetch('http://localhost/foo', {
cachePath: path.resolve(cache, 'file'),
})

await t.rejects(res.text(), { code: 'ENOTDIR' })
t.ok(req.isDone())
})

0 comments on commit ed73ef5

Please sign in to comment.