Skip to content

Commit 0c984a6

Browse files
author
Alan Shaw
authored
feat: disambiguate errors (#9)
1 parent 09165c5 commit 0c984a6

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/index.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,37 @@ export const hashMap = new Map(
3232
].map(hash => [hash.code, hash])
3333
)
3434

35+
/** The multihash hasher is not supported by this library. */
36+
export class UnsupportedHashError extends Error {
37+
/** @param {number} code */
38+
constructor (code) {
39+
super(`multihash code ${code} is not supported`)
40+
}
41+
}
42+
43+
/** The bytes did not hash to the same value as the passed multihash. */
44+
export class HashMismatchError extends Error {
45+
constructor () {
46+
super('CID hash does not match bytes')
47+
}
48+
}
49+
3550
/**
3651
* Validates IPLD block bytes.
3752
* @param {Block} block
3853
*/
3954
export function validateBlock (block) {
4055
const hasher = hashMap.get(block.cid.multihash.code)
4156
if (!hasher) {
42-
throw new Error(`multihash code ${block.cid.multihash.code} is not supported`)
57+
throw new UnsupportedHashError(block.cid.multihash.code)
4358
}
4459

4560
const result = hasher.digest(block.bytes)
4661

4762
/** @param {import('multiformats/hashes/interface').MultihashDigest} h */
4863
const compareDigests = h => {
4964
if (!equals(h.digest, block.cid.multihash.digest)) {
50-
throw new Error('CID hash does not match bytes')
65+
throw new HashMismatchError()
5166
}
5267
}
5368

test/test.spec.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import test from 'ava'
22
import * as pb from '@ipld/dag-pb'
33
import { CID } from 'multiformats/cid'
44
import { CarWriter, CarBlockIterator } from '@ipld/car'
5+
import { blake2b512 } from '@multiformats/blake2/blake2b'
56

6-
import { validateBlock, hashMap } from '../src/index.js'
7+
import { validateBlock, hashMap, HashMismatchError, UnsupportedHashError } from '../src/index.js'
78

89
const bytes = pb.encode({ Data: new Uint8Array([1, 2, 3]), Links: [] })
910

@@ -37,7 +38,24 @@ for (const [code, hasher] of hashMap) {
3738
const reader = await CarBlockIterator.fromIterable(out)
3839

3940
for await (const block of reader) {
40-
t.throwsAsync(async () => validateBlock(block))
41+
const err = await t.throwsAsync(async () => validateBlock(block))
42+
t.true(err instanceof HashMismatchError)
4143
}
4244
})
4345
}
46+
47+
test('throws when validating blocks with unsupported hashers', async (t) => {
48+
const hash = await blake2b512.digest(bytes)
49+
const cid = CID.create(1, pb.code, hash)
50+
51+
const { writer, out } = CarWriter.create([cid])
52+
writer.put({ cid, bytes })
53+
writer.close()
54+
55+
const reader = await CarBlockIterator.fromIterable(out)
56+
57+
for await (const block of reader) {
58+
const err = await t.throwsAsync(async () => validateBlock(block))
59+
t.true(err instanceof UnsupportedHashError)
60+
}
61+
})

0 commit comments

Comments
 (0)