Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Mar 4, 2019
1 parent 4a4c0e3 commit c6030e5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ const bestRecord = (selectors, k, records) => {
if (records.length === 0) {
const errMsg = `No records given`

throw errcode(new Error(errMsg), 'ERR_NO_RECORDS_RECEIVED')
throw errcode(errMsg, 'ERR_NO_RECORDS_RECEIVED')
}

const parts = bsplit(k, Buffer.from('/'))

if (parts.length < 3) {
const errMsg = `Record key does not have a selector function`

throw errcode(new Error(errMsg), 'ERR_NO_SELECTOR_FUNCTION_FOR_RECORD_KEY')
throw errcode(errMsg, 'ERR_NO_SELECTOR_FUNCTION_FOR_RECORD_KEY')
}

const selector = selectors[parts[1].toString()]

if (!selector) {
const errMsg = `Unrecognized key prefix: ${parts[1]}`

throw errcode(new Error(errMsg), 'ERR_UNRECOGNIZED_KEY_PREFIX')
throw errcode(errMsg, 'ERR_UNRECOGNIZED_KEY_PREFIX')
}

return selector(k, records)
Expand Down
2 changes: 1 addition & 1 deletion src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const verifyRecord = (validators, record) => {
if (!validator) {
const errMsg = `Invalid record keytype`

throw errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE')
throw errcode(errMsg, 'ERR_INVALID_RECORD_KEY_TYPE')
}

return validator.func(key, record.value)
Expand Down
11 changes: 6 additions & 5 deletions src/validators/public-key.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const multihashing = require('multihashing-async')
const errcode = require('err-code')

/**
* Validator for publick key records.
Expand All @@ -14,25 +15,25 @@ const multihashing = require('multihashing-async')
*/
const validatePublicKeyRecord = async (key, publicKey) => {
if (!Buffer.isBuffer(key)) {
throw new Error('"key" must be a Buffer')
throw errcode('"key" must be a Buffer', 'ERR_INVALID_RECORD_KEY_NOT_BUFFER')
}

if (key.length < 3) {
throw new Error('invalid public key record')
if (key.length < 5) {
throw errcode('invalid public key record', 'ERR_INVALID_RECORD_KEY_TOO_SHORT')
}

const prefix = key.slice(0, 4).toString()

if (prefix !== '/pk/') {
throw new Error('key was not prefixed with /pk/')
throw errcode('key was not prefixed with /pk/', 'ERR_INVALID_RECORD_KEY_BAD_PREFIX')
}

const keyhash = key.slice(4)

const publicKeyHash = await multihashing(publicKey, 'sha2-256')

if (!keyhash.equals(publicKeyHash)) {
throw new Error('public key does not match passed in key')
throw errcode('public key does not match passed in key', 'ERR_INVALID_RECORD_HASH_MISMATCH')
}
}

Expand Down
28 changes: 17 additions & 11 deletions test/validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ const generateCases = (hash) => {
invalid: {
publicKey: [
// missing hashkey
Buffer.from('/pk/'),
[Buffer.from('/pk/'), 'ERR_INVALID_RECORD_KEY_TOO_SHORT'],
// not the hash of a key
Buffer.concat([
[Buffer.concat([
Buffer.from('/pk/'),
Buffer.from('random')
]),
]), 'ERR_INVALID_RECORD_HASH_MISMATCH'],
// missing prefix
hash
[hash, 'ERR_INVALID_RECORD_KEY_BAD_PREFIX'],
// not a buffer
['not a buffer', 'ERR_INVALID_RECORD_KEY_NOT_BUFFER']
]
}
}
Expand Down Expand Up @@ -103,17 +105,21 @@ describe('validator', () => {
})

it('does not error on valid record', () => {
return Promise.all(cases.valid.publicKey, (k) => {
return Promise.all(cases.valid.publicKey.map((k) => {
return validator.validators.pk.func(k, key.public.bytes)
})
}))
})

it('throws on invalid records', () => {
return Promise.all(cases.invalid.publicKey, (k) => {
return expect(
() => validator.validators.pk.func(k, key.public.bytes)
).to.throw()
})
return Promise.all(cases.invalid.publicKey.map(async ([k, errCode]) => {
try {
await validator.validators.pk.func(k, key.public.bytes)
} catch (err) {
expect(err.code).to.eql(errCode)
return
}
expect.fail('did not throw an error with code ' + errCode)
}))
})
})
})
Expand Down

0 comments on commit c6030e5

Please sign in to comment.