Skip to content

Commit

Permalink
use error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrody committed Jan 7, 2022
1 parent 5882a0e commit d658007
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ exports.codes = {
ERR_INVALID_NEW_PASS_TYPE: 'ERR_INVALID_NEW_PASS_TYPE',
ERR_INVALID_PASS_LENGTH: 'ERR_INVALID_PASS_LENGTH',
ERR_NOT_IMPLEMENTED: 'ERR_NOT_IMPLEMENTED',
ERR_WRONG_PING_ACK: 'ERR_WRONG_PING_ACK'
ERR_WRONG_PING_ACK: 'ERR_WRONG_PING_ACK',
ERR_UNHANDLED_CASE: 'ERR_UNHANDLED_CASE'
}
8 changes: 5 additions & 3 deletions src/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const debug = require('debug')
const log = Object.assign(debug('libp2p:fetch'), {
error: debug('libp2p:fetch:err')
})
const errCode = require('err-code')
const { codes } = require('../errors')
const lp = require('it-length-prefixed')
const { FetchRequest, FetchResponse } = require('./proto')
// @ts-ignore it-handshake does not export types
Expand Down Expand Up @@ -62,10 +64,10 @@ class FetchProtocol {
}
case (FetchResponse.StatusCode.ERROR): {
const errmsg = (new TextDecoder()).decode(response.data)
throw new Error('Error in fetch protocol response: ' + errmsg)
throw errCode(new Error('Error in fetch protocol response: ' + errmsg), codes.ERR_INVALID_PARAMETERS)
}
default: {
throw new Error('Unreachable case')
throw errCode(new Error('Unreachable case'), codes.ERR_UNHANDLED_CASE)
}
}
}
Expand Down Expand Up @@ -122,7 +124,7 @@ class FetchProtocol {
*/
registerLookupFunction (prefix, lookup) {
if (this._lookupFunctions.has(prefix)) {
throw new Error("Fetch protocol handler for key prefix '" + prefix + "' already registered")
throw errCode(new Error("Fetch protocol handler for key prefix '" + prefix + "' already registered"), codes.ERR_KEY_ALREADY_EXISTS)
}
this._lookupFunctions.set(prefix, lookup)
}
Expand Down
18 changes: 9 additions & 9 deletions test/fetch/fetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { NOISE } = require('@chainsafe/libp2p-noise')
const MDNS = require('libp2p-mdns')
const { createPeerId } = require('../utils/creators/peer')
const Fetch = require('../../src/fetch')
const { codes } = require('../../src/errors')

async function createLibp2pNode (peerId) {
return await Libp2p.create({
Expand Down Expand Up @@ -99,20 +100,19 @@ describe('Fetch Protocol', () => {
fetch.registerLookupFunction(PREFIX_A, generateLookupFunction(PREFIX_A, DATA_A))
fetch.mount(receiver)

try {
await Fetch.fetch(sender, receiver.peerId, '/moduleUNKNOWN/foobar')
expect.fail("didn't throw")
} catch (err) {
expect(err).to.be.an('Error')
expect(err.message).to.equal('Error in fetch protocol response: No lookup function registered for key: /moduleUNKNOWN/foobar')
}
await expect(Fetch.fetch(sender, receiver.peerId, '/moduleUNKNOWN/foobar')).to.eventually.be.rejected.with.property('code', codes.ERR_INVALID_PARAMETERS)
})

it('Registering multiple handlers for same prefix errors', async () => {
const fetch = new Fetch()
fetch.registerLookupFunction(PREFIX_A, generateLookupFunction(PREFIX_A, DATA_A))
expect(function () {

try {
fetch.registerLookupFunction(PREFIX_A, generateLookupFunction(PREFIX_A, DATA_B))
}).to.throw('already registered')
expect.fail("didn't throw")
} catch (err) {
expect(err).to.be.an('Error')
expect(err.code).to.equal(codes.ERR_KEY_ALREADY_EXISTS)
}
})
})

0 comments on commit d658007

Please sign in to comment.