diff --git a/get.js b/get.js index f7575cd..c9a3835 100644 --- a/get.js +++ b/get.js @@ -160,7 +160,7 @@ function getStream (cache, key, opts) { memoStream, stream ) - }).catch(err => stream.emit('error', err)) + }).catch((err) => stream.emit('error', err)) return stream } diff --git a/lib/content/read.js b/lib/content/read.js index 0c668d0..542b20b 100644 --- a/lib/content/read.js +++ b/lib/content/read.js @@ -64,7 +64,7 @@ function readStream (cache, integrity, opts) { }), stream ) - }).catch(err => { + }).catch((err) => { stream.emit('error', err) }) return stream @@ -96,7 +96,7 @@ function hasContent (cache, integrity) { if (!integrity) { return BB.resolve(false) } return withContentSri(cache, integrity, (cpath, sri) => { return lstatAsync(cpath).then(stat => ({ size: stat.size, sri, stat })) - }).catch(err => { + }).catch((err) => { if (err.code === 'ENOENT') { return false } if (err.code === 'EPERM') { if (process.platform !== 'win32') { @@ -142,7 +142,7 @@ function withContentSri (cache, integrity, fn) { return BB.any(sri[sri.pickAlgorithm()].map(meta => { return withContentSri(cache, meta, fn) }, { concurrency: 1 })) - .catch(err => { + .catch((err) => { if ([].some.call(err, e => e.code === 'ENOENT')) { throw Object.assign( new Error('No matching content found for ' + sri.toString()), diff --git a/lib/content/write.js b/lib/content/write.js index 83ca3a5..ad59592 100644 --- a/lib/content/write.js +++ b/lib/content/write.js @@ -119,7 +119,7 @@ function pipeToTmp (inputStream, cache, tmpTarget, opts, errCheck) { errCheck() return pipe(inputStream, hashStream, outStream).then(() => { return { integrity, size } - }).catch(err => { + }).catch((err) => { return rimraf(tmpTarget).then(() => { throw err }) }) }) diff --git a/lib/entry-index.js b/lib/entry-index.js index 09cb885..cd690f2 100644 --- a/lib/entry-index.js +++ b/lib/entry-index.js @@ -62,7 +62,11 @@ function insert (cache, key, integrity, opts) { ) }).then( () => fixOwner.chownr(cache, bucket) - ).catch({ code: 'ENOENT' }, () => { + ).catch((err) => { + if (err.code === 'ENOENT') { + return undefined + } + throw err // There's a class of race conditions that happen when things get deleted // during fixOwner, or between the two mkdirfix/chownr calls. // @@ -110,7 +114,7 @@ function find (cache, key) { return latest } }, null) - }).catch(err => { + }).catch((err) => { if (err.code === 'ENOENT') { return null } else { @@ -176,7 +180,7 @@ function lsStream (cache) { const formatted = formatEntry(cache, entry) formatted && stream.push(formatted) } - }).catch({ code: 'ENOENT' }, nop) + }).catch((err) => { if (err.code === 'ENOENT') { return undefined } throw err }) }) }) }).then(() => { @@ -280,9 +284,11 @@ function formatEntry (cache, entry) { function readdirOrEmpty (dir) { return readdirAsync(dir) - .catch({ code: 'ENOENT' }, () => []) - .catch({ code: 'ENOTDIR' }, () => []) -} + .catch((err) => { + if (err.code === 'ENOENT' || err.code === 'ENOTDIR') { + return [] + } -function nop () { + throw err + }) } diff --git a/lib/util/fix-owner.js b/lib/util/fix-owner.js index f5c33db..89a11c6 100644 --- a/lib/util/fix-owner.js +++ b/lib/util/fix-owner.js @@ -58,7 +58,12 @@ function fixOwner (cache, filepath) { filepath, typeof uid === 'number' ? uid : self.uid, typeof gid === 'number' ? gid : self.gid - ).catch({ code: 'ENOENT' }, () => null) + ).catch((err) => { + if (err.code === 'ENOENT') { + return null + } + throw err + }) ) }) } @@ -101,9 +106,11 @@ function mkdirfix (cache, p, cb) { if (made) { return fixOwner(cache, made).then(() => made) } - }).catch({ code: 'EEXIST' }, () => { - // There's a race in mkdirp! - return fixOwner(cache, p).then(() => null) + }).catch((err) => { + if (err.code === 'EEXIST') { + return fixOwner(cache, p).then(() => null) + } + throw err }) }) } diff --git a/lib/util/move-file.js b/lib/util/move-file.js index af6572f..7d9c8d4 100644 --- a/lib/util/move-file.js +++ b/lib/util/move-file.js @@ -4,8 +4,9 @@ const fs = require('graceful-fs') const BB = require('bluebird') const chmod = BB.promisify(fs.chmod) const unlink = BB.promisify(fs.unlink) -let move -let pinflight +const stat = BB.promisify(fs.stat) +const move = require('move-concurrently') +const pinflight = require('promise-inflight') module.exports = moveFile function moveFile (src, dest) { @@ -35,15 +36,13 @@ function moveFile (src, dest) { // content should never change for any reason, so make it read-only return Promise.all([unlink(src), process.platform !== 'win32' && chmod(dest, '0444')]) }).catch(() => { - if (!pinflight) { pinflight = require('promise-inflight') } return pinflight('cacache-move-file:' + dest, () => { - return BB.promisify(fs.stat)(dest).catch(err => { + return stat(dest).catch(err => { if (err.code !== 'ENOENT') { // Something else is wrong here. Bail bail bail throw err } // file doesn't already exist! let's try a rename -> copy fallback - if (!move) { move = require('move-concurrently') } return move(src, dest, { BB, fs }) }) }) diff --git a/lib/verify.js b/lib/verify.js index 1f1c929..9e2bd99 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -145,13 +145,18 @@ function verifyContent (filepath, sri) { return ssri.checkStream( fs.createReadStream(filepath), sri - ).catch(err => { + ).catch((err) => { if (err.code !== 'EINTEGRITY') { throw err } return rimraf(filepath).then(() => { contentInfo.valid = false }) }).then(() => contentInfo) - }).catch({ code: 'ENOENT' }, () => ({ size: 0, valid: false })) + }).catch((err) => { + if (err.code === 'ENOENT') { + return { size: 0, valid: false } + } + throw err + }) } function rebuildIndex (cache, opts) { @@ -199,9 +204,13 @@ function rebuildBucket (cache, bucket, stats, opts) { metadata: entry.metadata, size: entry.size }).then(() => { stats.totalEntries++ }) - }).catch({ code: 'ENOENT' }, () => { - stats.rejectedEntries++ - stats.missingContent++ + }).catch((err) => { + if (err.code === 'ENOENT') { + stats.rejectedEntries++ + stats.missingContent++ + return + } + throw err }) }) }) diff --git a/test/content.rm.js b/test/content.rm.js index 264ed42..8c2cbe2 100644 --- a/test/content.rm.js +++ b/test/content.rm.js @@ -23,7 +23,7 @@ test('removes a content entry', function (t) { fs.statAsync(contentPath(CACHE, 'sha1-deadbeef')) )).then(() => { throw new Error('expected an error') - }).catch(err => { + }).catch((err) => { t.ok(err, 'fs.stat failed on rmed content') t.equal('ENOENT', err.code, 'file does not exist anymore') }) @@ -36,7 +36,7 @@ test('works fine if entry missing', function (t) { fs.statAsync(contentPath(CACHE, 'sha1-deadbeef')) )).then(() => { throw new Error('expected an error') - }).catch(err => { + }).catch((err) => { t.ok(err, 'fs.stat failed on rmed content') t.equal('ENOENT', err.code, 'file does not exist anymore') }) diff --git a/test/get.js b/test/get.js index 06fb279..465e403 100644 --- a/test/get.js +++ b/test/get.js @@ -154,11 +154,11 @@ test('get.copy', t => { test('ENOENT if not found', t => { return get(CACHE, KEY).then(() => { throw new Error('lookup should fail') - }).catch(err => { + }).catch((err) => { t.ok(err, 'got an error') t.equal(err.code, 'ENOENT', 'error code is ENOENT') return get.info(CACHE, KEY) - }).catch(err => { + }).catch((err) => { t.ok(err, 'got an error') t.equal(err.code, 'ENOENT', 'error code is ENOENT') }) @@ -205,7 +205,7 @@ test('memoizes data on bulk read', t => { }, 'memoized data fetched by default') return get(CACHE, KEY, { memoize: false }).then(() => { throw new Error('expected get to fail') - }).catch(err => { + }).catch((err) => { t.ok(err, 'got an error from unmemoized get') t.equal(err.code, 'ENOENT', 'cached content not found') t.deepEqual(memo.get(CACHE, KEY), { @@ -299,10 +299,10 @@ test('memoizes data on stream read', t => { return Promise.all([ streamGet(false, CACHE, KEY, { memoize: false - }).catch(err => err), + }).catch((err) => err), streamGet(true, CACHE, INTEGRITY, { memoize: false - }).catch(err => err) + }).catch((err) => err) ]).then(([keyErr, digestErr]) => { t.equal(keyErr.code, 'ENOENT', 'key get memoization bypassed') t.equal(keyErr.code, 'ENOENT', 'digest get memoization bypassed') diff --git a/test/index.find.js b/test/index.find.js index bfb4e4a..e1044af 100644 --- a/test/index.find.js +++ b/test/index.find.js @@ -57,7 +57,7 @@ test('index.find cache miss', function (t) { test('index.find no cache', function (t) { return fs.statAsync(CACHE).then(() => { throw new Error('expected cache directory') - }).catch(err => { + }).catch((err) => { t.assert(err, 'cache directory does not exist') return index.find(CACHE, 'whatever') }).then(info => { diff --git a/test/put.js b/test/put.js index 45d6ac8..ea421b3 100644 --- a/test/put.js +++ b/test/put.js @@ -105,7 +105,7 @@ test('optionally memoizes data on stream insertion', t => { test('errors if integrity errors', t => { return put(CACHE, KEY, CONTENT, { integrity: 'sha1-BaDDigEST' - }).catch(err => { + }).catch((err) => { t.equal(err.code, 'EINTEGRITY', 'got error from bad integrity') }) }) @@ -116,12 +116,12 @@ test('signals error if error writing to cache', t => { size: 2 }).then(() => { throw new Error('expected error') - }).catch(err => err), + }).catch((err) => err), pipe(fromString(CONTENT), put.stream(CACHE, KEY, { size: 2 })).then(() => { throw new Error('expected error') - }).catch(err => err) + }).catch((err) => err) ]).then(([bulkErr, streamErr]) => { t.equal(bulkErr.code, 'EBADSIZE', 'got error from bulk write') t.equal(streamErr.code, 'EBADSIZE', 'got error from stream write') @@ -138,7 +138,7 @@ test('errors if input stream errors', t => { stream, putter ).then(() => { throw new Error('expected error') - }).catch(err => { + }).catch((err) => { t.ok(err, 'got an error') t.ok(!int, 'no integrity returned') t.match( diff --git a/test/rm.js b/test/rm.js index 6d32e2f..a9ab5a7 100644 --- a/test/rm.js +++ b/test/rm.js @@ -36,8 +36,12 @@ test('rm.entry removes entries, not content', t => { return get(CACHE, KEY) }).then(res => { throw new Error('unexpected success') - }).catch({ code: 'ENOENT' }, err => { - t.match(err.message, KEY, 'entry no longer accessible') + }).catch((err) => { + if (err.code === 'ENOENT') { + t.match(err.message, KEY, 'entry no longer accessible') + return + } + throw err }).then(() => { return fs.readFileAsync(contentPath(CACHE, INTEGRITY)) }).then(data => { @@ -58,14 +62,22 @@ test('rm.content removes content, not entries', t => { return get(CACHE, KEY) }).then(res => { throw new Error('unexpected success') - }).catch({ code: 'ENOENT' }, err => { - t.match(err.message, /no such file/, 'entry no longer accessible') + }).catch((err) => { + if (err.code === 'ENOENT') { + t.match(err.message, /no such file/, 'entry no longer accessible') + return + } + throw err }).then(() => { return fs.readFileAsync(contentPath(CACHE, INTEGRITY)) }).then(() => { throw new Error('unexpected success') - }).catch({ code: 'ENOENT' }, err => { - t.match(err.message, /no such file/, 'content gone') + }).catch((err) => { + if (err.code === 'ENOENT') { + t.match(err.message, /no such file/, 'content gone') + return + } + throw err }) }) diff --git a/test/util.move-file.js b/test/util.move-file.js index 8a97eb8..cef63a8 100644 --- a/test/util.move-file.js +++ b/test/util.move-file.js @@ -23,7 +23,7 @@ test('move a file', function (t) { return fs.readFileAsync('dest', 'utf8') }).then(data => { t.equal(data, 'foo', 'file data correct') - return fs.statAsync('src').catch(err => { + return fs.statAsync('src').catch((err) => { t.ok(err, 'src read error') t.equal(err.code, 'ENOENT', 'src does not exist') }) @@ -40,7 +40,7 @@ test('does not clobber existing files', function (t) { return fs.readFileAsync('dest', 'utf8') }).then(data => { t.equal(data, 'bar', 'conflicting file left intact') - return fs.statAsync('src').catch(err => { + return fs.statAsync('src').catch((err) => { t.ok(err, 'src read error') t.equal(err.code, 'ENOENT', 'src file still deleted') }) @@ -74,7 +74,7 @@ test('does not error if destination file is open', function (t) { return fs.readFileAsync('dest', 'utf8') }).then(data => { t.equal(data, 'bar', 'destination left intact') - return fs.statAsync('src').catch(err => { + return fs.statAsync('src').catch((err) => { t.ok(err, 'src read error') t.equal(err.code, 'ENOENT', 'src does not exist') }) @@ -93,7 +93,7 @@ test('errors if dest is not writable', { return fs.chmodAsync('dest', parseInt('400', 8)).then(() => { return moveFile('src', path.join('dest', 'file')).then(() => { throw new Error('move succeeded and should not have') - }).catch(err => { + }).catch((err) => { t.ok(err, 'error was returned') t.equal(err.code, 'EACCES', 'error is about permissions') return fs.readFileAsync('src', 'utf8') diff --git a/test/verify.js b/test/verify.js index 66953dc..6db6c94 100644 --- a/test/verify.js +++ b/test/verify.js @@ -142,8 +142,12 @@ test('removes corrupted content', t => { }, 'reported correct collection counts') return fs.statAsync(cpath).then(() => { throw new Error('expected a failure') - }).catch({ code: 'ENOENT' }, err => { - t.match(err.message, /no such file/, 'content no longer in cache') + }).catch((err) => { + if (err.code === 'ENOENT') { + t.match(err.message, /no such file/, 'content no longer in cache') + return + } + throw err }) }) })