Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always catch and emit cache write errors in promise #288

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be from when we called (reject) so why not emit it there?

This may be a nitpick.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 302

cacheStream.promise().then(cacheWriteResolve, cacheWriteReject)

the error function should emit imho.

cacheStream.promise().then(cacheWriteResolve, err => {
         if (body) {
          body.emit('error', err)
        }
   cacheWriteReject(err)
})

(typed on the website, syntax may be all wrong).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed w/ nlf. The error is the lack of an emit when rejecting, we shouldn't need a catch we should just be sure to emit when rejecting. It's also more intuitive, it's harder to reason why we're doing a blanket catch versus having an emit before we reject.

I think this also eats the rejection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with moving the emitting to line ~302.

if (body) {

We can drop the if. At line 302 we know there's a body. In fact we were called by the body's resume handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far I've learned there has to be a catch attached to the promise immediately, otherwise the cache dir creation error is unhandled. So if the catch needs to be there then we also need to emit from there. But I'm still not sold on my fix being the correct one, only that emitting from a different place does not fix the test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also dropped the if (body) statement as suggested since it was not necessary.

body.emit('error', err)
})

body = new CachingMinipassPipeline({ events: ['integrity', 'size'] }, new MinipassFlush({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to declare the error event, do we?

Suggested change
body = new CachingMinipassPipeline({ events: ['integrity', 'size'] }, new MinipassFlush({
body = new CachingMinipassPipeline({ events: ['integrity', 'size', 'error'] }, new MinipassFlush({

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't since that event is always raised on the stream and is not a new event that we need to inform the caching stream of like integrity and size.

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())
})