Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
fix: Throw errors, do not return them
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 12, 2016
1 parent 81fd48e commit 1215fa6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ Returns a boolean.

- `multihash: Buffer`

Check if the given buffer is a valid multihash.

Returns `false` or an `Error`.
Check if the given buffer is a valid multihash. Throws an error if it is not valid.

## License

Expand Down
17 changes: 6 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ exports.fromB58String = function fromB58String (s) {

// Decode a hash from the given Multihash.
exports.decode = function decode (buf) {
const err = exports.validate(buf)
if (err) {
throw err
}
exports.validate(buf)

const code = buf[0]

Expand Down Expand Up @@ -121,26 +118,24 @@ exports.isValidCode = function validCode (code) {

exports.validate = function validate (multihash) {
if (!(Buffer.isBuffer(multihash))) {
return new Error('multihash must be a Buffer')
throw new Error('multihash must be a Buffer')
}

if (multihash.length < 3) {
return new Error('multihash too short. must be > 3 bytes.')
throw new Error('multihash too short. must be > 3 bytes.')
}

if (multihash.length > 129) {
return new Error('multihash too long. must be < 129 bytes.')
throw new Error('multihash too long. must be < 129 bytes.')
}

let code = multihash[0]

if (!exports.isValidCode(code)) {
return new Error(`multihash unknown function code: 0x${code.toString(16)}`)
throw new Error(`multihash unknown function code: 0x${code.toString(16)}`)
}

if (multihash.slice(2).length !== multihash[1]) {
return new Error(`multihash length inconsistent: 0x${multihash.toString('hex')}`)
throw new Error(`multihash length inconsistent: 0x${multihash.toString('hex')}`)
}

return false
}
16 changes: 6 additions & 10 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,23 @@ describe('mh', () => {
it('valid', () => {
validCases.forEach((test) => {
expect(
mh.validate(sample(test.encoding.code, test.size, test.hex))
).to.be.equal(
false
)
() => mh.validate(sample(test.encoding.code, test.size, test.hex))
).to.not.throw()
})
})

it('invalid', () => {
invalidCases.forEach((test) => {
expect(
mh.validate(sample(test.code, test.size, test.hex))
).to.be.an('error')
() => mh.validate(sample(test.code, test.size, test.hex))
).to.throw()
})

const longBuffer = new Buffer(150)
longBuffer.fill('a')
expect(
mh.validate(longBuffer)
).to.be.an(
'error'
)
() => mh.validate(longBuffer)
).to.throw()
})
})

Expand Down

0 comments on commit 1215fa6

Please sign in to comment.