Skip to content

Commit 28aeeac

Browse files
billatnpmisaacs
authored andcommitted
feat(promise): removed bluebird specific .catch calls
1 parent 1d56da1 commit 28aeeac

File tree

14 files changed

+87
-50
lines changed

14 files changed

+87
-50
lines changed

get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function getStream (cache, key, opts) {
160160
memoStream,
161161
stream
162162
)
163-
}).catch(err => stream.emit('error', err))
163+
}).catch((err) => stream.emit('error', err))
164164
return stream
165165
}
166166

lib/content/read.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function readStream (cache, integrity, opts) {
6464
}),
6565
stream
6666
)
67-
}).catch(err => {
67+
}).catch((err) => {
6868
stream.emit('error', err)
6969
})
7070
return stream
@@ -96,7 +96,7 @@ function hasContent (cache, integrity) {
9696
if (!integrity) { return BB.resolve(false) }
9797
return withContentSri(cache, integrity, (cpath, sri) => {
9898
return lstatAsync(cpath).then(stat => ({ size: stat.size, sri, stat }))
99-
}).catch(err => {
99+
}).catch((err) => {
100100
if (err.code === 'ENOENT') { return false }
101101
if (err.code === 'EPERM') {
102102
if (process.platform !== 'win32') {
@@ -142,7 +142,7 @@ function withContentSri (cache, integrity, fn) {
142142
return BB.any(sri[sri.pickAlgorithm()].map(meta => {
143143
return withContentSri(cache, meta, fn)
144144
}, { concurrency: 1 }))
145-
.catch(err => {
145+
.catch((err) => {
146146
if ([].some.call(err, e => e.code === 'ENOENT')) {
147147
throw Object.assign(
148148
new Error('No matching content found for ' + sri.toString()),

lib/content/write.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function pipeToTmp (inputStream, cache, tmpTarget, opts, errCheck) {
119119
errCheck()
120120
return pipe(inputStream, hashStream, outStream).then(() => {
121121
return { integrity, size }
122-
}).catch(err => {
122+
}).catch((err) => {
123123
return rimraf(tmpTarget).then(() => { throw err })
124124
})
125125
})

lib/entry-index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ function insert (cache, key, integrity, opts) {
6262
)
6363
}).then(
6464
() => fixOwner.chownr(cache, bucket)
65-
).catch({ code: 'ENOENT' }, () => {
65+
).catch((err) => {
66+
if (err.code === 'ENOENT') {
67+
return undefined
68+
}
69+
throw err
6670
// There's a class of race conditions that happen when things get deleted
6771
// during fixOwner, or between the two mkdirfix/chownr calls.
6872
//
@@ -110,7 +114,7 @@ function find (cache, key) {
110114
return latest
111115
}
112116
}, null)
113-
}).catch(err => {
117+
}).catch((err) => {
114118
if (err.code === 'ENOENT') {
115119
return null
116120
} else {
@@ -176,7 +180,7 @@ function lsStream (cache) {
176180
const formatted = formatEntry(cache, entry)
177181
formatted && stream.push(formatted)
178182
}
179-
}).catch({ code: 'ENOENT' }, nop)
183+
}).catch((err) => { if (err.code === 'ENOENT') { return undefined } throw err })
180184
})
181185
})
182186
}).then(() => {
@@ -280,9 +284,11 @@ function formatEntry (cache, entry) {
280284

281285
function readdirOrEmpty (dir) {
282286
return readdirAsync(dir)
283-
.catch({ code: 'ENOENT' }, () => [])
284-
.catch({ code: 'ENOTDIR' }, () => [])
285-
}
287+
.catch((err) => {
288+
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
289+
return []
290+
}
286291

287-
function nop () {
292+
throw err
293+
})
288294
}

lib/util/fix-owner.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ function fixOwner (cache, filepath) {
5858
filepath,
5959
typeof uid === 'number' ? uid : self.uid,
6060
typeof gid === 'number' ? gid : self.gid
61-
).catch({ code: 'ENOENT' }, () => null)
61+
).catch((err) => {
62+
if (err.code === 'ENOENT') {
63+
return null
64+
}
65+
throw err
66+
})
6267
)
6368
})
6469
}
@@ -101,9 +106,11 @@ function mkdirfix (cache, p, cb) {
101106
if (made) {
102107
return fixOwner(cache, made).then(() => made)
103108
}
104-
}).catch({ code: 'EEXIST' }, () => {
105-
// There's a race in mkdirp!
106-
return fixOwner(cache, p).then(() => null)
109+
}).catch((err) => {
110+
if (err.code === 'EEXIST') {
111+
return fixOwner(cache, p).then(() => null)
112+
}
113+
throw err
107114
})
108115
})
109116
}

lib/util/move-file.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ const fs = require('graceful-fs')
44
const BB = require('bluebird')
55
const chmod = BB.promisify(fs.chmod)
66
const unlink = BB.promisify(fs.unlink)
7-
let move
8-
let pinflight
7+
const stat = BB.promisify(fs.stat)
8+
const move = require('move-concurrently')
9+
const pinflight = require('promise-inflight')
910

1011
module.exports = moveFile
1112
function moveFile (src, dest) {
@@ -35,15 +36,13 @@ function moveFile (src, dest) {
3536
// content should never change for any reason, so make it read-only
3637
return Promise.all([unlink(src), process.platform !== 'win32' && chmod(dest, '0444')])
3738
}).catch(() => {
38-
if (!pinflight) { pinflight = require('promise-inflight') }
3939
return pinflight('cacache-move-file:' + dest, () => {
40-
return BB.promisify(fs.stat)(dest).catch(err => {
40+
return stat(dest).catch(err => {
4141
if (err.code !== 'ENOENT') {
4242
// Something else is wrong here. Bail bail bail
4343
throw err
4444
}
4545
// file doesn't already exist! let's try a rename -> copy fallback
46-
if (!move) { move = require('move-concurrently') }
4746
return move(src, dest, { BB, fs })
4847
})
4948
})

lib/verify.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,18 @@ function verifyContent (filepath, sri) {
145145
return ssri.checkStream(
146146
fs.createReadStream(filepath),
147147
sri
148-
).catch(err => {
148+
).catch((err) => {
149149
if (err.code !== 'EINTEGRITY') { throw err }
150150
return rimraf(filepath).then(() => {
151151
contentInfo.valid = false
152152
})
153153
}).then(() => contentInfo)
154-
}).catch({ code: 'ENOENT' }, () => ({ size: 0, valid: false }))
154+
}).catch((err) => {
155+
if (err.code === 'ENOENT') {
156+
return { size: 0, valid: false }
157+
}
158+
throw err
159+
})
155160
}
156161

157162
function rebuildIndex (cache, opts) {
@@ -199,9 +204,13 @@ function rebuildBucket (cache, bucket, stats, opts) {
199204
metadata: entry.metadata,
200205
size: entry.size
201206
}).then(() => { stats.totalEntries++ })
202-
}).catch({ code: 'ENOENT' }, () => {
203-
stats.rejectedEntries++
204-
stats.missingContent++
207+
}).catch((err) => {
208+
if (err.code === 'ENOENT') {
209+
stats.rejectedEntries++
210+
stats.missingContent++
211+
return
212+
}
213+
throw err
205214
})
206215
})
207216
})

test/content.rm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test('removes a content entry', function (t) {
2323
fs.statAsync(contentPath(CACHE, 'sha1-deadbeef'))
2424
)).then(() => {
2525
throw new Error('expected an error')
26-
}).catch(err => {
26+
}).catch((err) => {
2727
t.ok(err, 'fs.stat failed on rmed content')
2828
t.equal('ENOENT', err.code, 'file does not exist anymore')
2929
})
@@ -36,7 +36,7 @@ test('works fine if entry missing', function (t) {
3636
fs.statAsync(contentPath(CACHE, 'sha1-deadbeef'))
3737
)).then(() => {
3838
throw new Error('expected an error')
39-
}).catch(err => {
39+
}).catch((err) => {
4040
t.ok(err, 'fs.stat failed on rmed content')
4141
t.equal('ENOENT', err.code, 'file does not exist anymore')
4242
})

test/get.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ test('get.copy', t => {
154154
test('ENOENT if not found', t => {
155155
return get(CACHE, KEY).then(() => {
156156
throw new Error('lookup should fail')
157-
}).catch(err => {
157+
}).catch((err) => {
158158
t.ok(err, 'got an error')
159159
t.equal(err.code, 'ENOENT', 'error code is ENOENT')
160160
return get.info(CACHE, KEY)
161-
}).catch(err => {
161+
}).catch((err) => {
162162
t.ok(err, 'got an error')
163163
t.equal(err.code, 'ENOENT', 'error code is ENOENT')
164164
})
@@ -205,7 +205,7 @@ test('memoizes data on bulk read', t => {
205205
}, 'memoized data fetched by default')
206206
return get(CACHE, KEY, { memoize: false }).then(() => {
207207
throw new Error('expected get to fail')
208-
}).catch(err => {
208+
}).catch((err) => {
209209
t.ok(err, 'got an error from unmemoized get')
210210
t.equal(err.code, 'ENOENT', 'cached content not found')
211211
t.deepEqual(memo.get(CACHE, KEY), {
@@ -299,10 +299,10 @@ test('memoizes data on stream read', t => {
299299
return Promise.all([
300300
streamGet(false, CACHE, KEY, {
301301
memoize: false
302-
}).catch(err => err),
302+
}).catch((err) => err),
303303
streamGet(true, CACHE, INTEGRITY, {
304304
memoize: false
305-
}).catch(err => err)
305+
}).catch((err) => err)
306306
]).then(([keyErr, digestErr]) => {
307307
t.equal(keyErr.code, 'ENOENT', 'key get memoization bypassed')
308308
t.equal(keyErr.code, 'ENOENT', 'digest get memoization bypassed')

test/index.find.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test('index.find cache miss', function (t) {
5757
test('index.find no cache', function (t) {
5858
return fs.statAsync(CACHE).then(() => {
5959
throw new Error('expected cache directory')
60-
}).catch(err => {
60+
}).catch((err) => {
6161
t.assert(err, 'cache directory does not exist')
6262
return index.find(CACHE, 'whatever')
6363
}).then(info => {

0 commit comments

Comments
 (0)