From 067a30312116c7c4352d8b62db9168dd49d86df7 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Wed, 26 Jul 2023 15:30:03 -0300 Subject: [PATCH] Typed errors --- index.js | 2 +- lib/streams.js | 5 +++-- package.json | 2 +- test/all.js | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 4b31fca..b0ea403 100644 --- a/index.js +++ b/index.js @@ -46,7 +46,7 @@ module.exports = class Hyperblobs { res.push(block) } } catch (error) { - if (error.message === 'Block not available') return null + if (error.code === 'BLOCK_NOT_AVAILABLE') return null throw error } diff --git a/lib/streams.js b/lib/streams.js index d662b39..a5622be 100644 --- a/lib/streams.js +++ b/lib/streams.js @@ -1,4 +1,5 @@ const { Readable, Writable } = require('streamx') +const { BLOCK_NOT_AVAILABLE } = require('hypercore/errors') class SparsePrefetcher { constructor (opts = {}) { @@ -121,7 +122,7 @@ class BlobReadStream extends Readable { start: this.id.blockOffset, end: this.id.blockOffset + this.id.blockLength }).then(result => { - if (!result) return cb(new Error('Block not available')) + if (!result) return cb(BLOCK_NOT_AVAILABLE()) this._index = result[0] this._relativeOffset = result[1] @@ -150,7 +151,7 @@ class BlobReadStream extends Readable { } this.core.get(this._index).then(block => { - if (!block) return cb(new Error('Block not available')) + if (!block) return cb(BLOCK_NOT_AVAILABLE()) const remainder = this._end - this._pos if (this._relativeOffset || (remainder < block.length)) { diff --git a/package.json b/package.json index 742f607..2986f7e 100644 --- a/package.json +++ b/package.json @@ -23,12 +23,12 @@ "homepage": "https://github.com/holepunchto/hyperblobs#readme", "dependencies": { "b4a": "^1.6.1", + "hypercore": "^10.18.0", "mutexify": "^1.4.0", "streamx": "^2.13.2" }, "devDependencies": { "brittle": "^3.1.0", - "hypercore": "^10.11.0", "random-access-memory": "^6.0.0", "standard": "^17.0.0" } diff --git a/test/all.js b/test/all.js index 17e284d..a2d7e43 100644 --- a/test/all.js +++ b/test/all.js @@ -264,7 +264,7 @@ test('read stream without waiting', async function (t) { t.fail('should not get any block: ' + block.toString()) } } catch (error) { - t.is(error.message, 'Block not available') + t.is(error.code, 'BLOCK_NOT_AVAILABLE') } }) @@ -281,7 +281,7 @@ test('seek stream without waiting', async function (t) { t.fail('should not get any block: ' + block.toString()) } } catch (error) { - t.is(error.message, 'Block not available') + t.is(error.code, 'BLOCK_NOT_AVAILABLE') } })