Skip to content

Commit ffb25e8

Browse files
authored
Typed errors (#18)
1 parent 60774a0 commit ffb25e8

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = class Hyperblobs {
4646
res.push(block)
4747
}
4848
} catch (error) {
49-
if (error.message === 'Block not available') return null
49+
if (error.code === 'BLOCK_NOT_AVAILABLE') return null
5050
throw error
5151
}
5252

lib/streams.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { Readable, Writable } = require('streamx')
2+
const { BLOCK_NOT_AVAILABLE } = require('hypercore/errors')
23

34
class SparsePrefetcher {
45
constructor (opts = {}) {
@@ -121,7 +122,7 @@ class BlobReadStream extends Readable {
121122
start: this.id.blockOffset,
122123
end: this.id.blockOffset + this.id.blockLength
123124
}).then(result => {
124-
if (!result) return cb(new Error('Block not available'))
125+
if (!result) return cb(BLOCK_NOT_AVAILABLE())
125126

126127
this._index = result[0]
127128
this._relativeOffset = result[1]
@@ -150,7 +151,7 @@ class BlobReadStream extends Readable {
150151
}
151152

152153
this.core.get(this._index).then(block => {
153-
if (!block) return cb(new Error('Block not available'))
154+
if (!block) return cb(BLOCK_NOT_AVAILABLE())
154155

155156
const remainder = this._end - this._pos
156157
if (this._relativeOffset || (remainder < block.length)) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
"homepage": "https://github.com/holepunchto/hyperblobs#readme",
2424
"dependencies": {
2525
"b4a": "^1.6.1",
26+
"hypercore": "^10.18.0",
2627
"mutexify": "^1.4.0",
2728
"streamx": "^2.13.2"
2829
},
2930
"devDependencies": {
3031
"brittle": "^3.1.0",
31-
"hypercore": "^10.11.0",
3232
"random-access-memory": "^6.0.0",
3333
"standard": "^17.0.0"
3434
}

test/all.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ test('read stream without waiting', async function (t) {
264264
t.fail('should not get any block: ' + block.toString())
265265
}
266266
} catch (error) {
267-
t.is(error.message, 'Block not available')
267+
t.is(error.code, 'BLOCK_NOT_AVAILABLE')
268268
}
269269
})
270270

@@ -281,7 +281,7 @@ test('seek stream without waiting', async function (t) {
281281
t.fail('should not get any block: ' + block.toString())
282282
}
283283
} catch (error) {
284-
t.is(error.message, 'Block not available')
284+
t.is(error.code, 'BLOCK_NOT_AVAILABLE')
285285
}
286286
})
287287

0 commit comments

Comments
 (0)