From d658007d5c7d16a0a1b9d35adaf2e94541988b4e Mon Sep 17 00:00:00 2001 From: Spencer T Brody Date: Fri, 7 Jan 2022 15:23:53 -0500 Subject: [PATCH] use error codes --- src/errors.js | 3 ++- src/fetch/index.js | 8 +++++--- test/fetch/fetch.spec.js | 18 +++++++++--------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/errors.js b/src/errors.js index 61f308667e..cedb0cfcd0 100644 --- a/src/errors.js +++ b/src/errors.js @@ -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' } diff --git a/src/fetch/index.js b/src/fetch/index.js index 86e1e7d429..5829afda6c 100644 --- a/src/fetch/index.js +++ b/src/fetch/index.js @@ -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 @@ -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) } } } @@ -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) } diff --git a/test/fetch/fetch.spec.js b/test/fetch/fetch.spec.js index 4a37c091fa..aa7cb00d41 100644 --- a/test/fetch/fetch.spec.js +++ b/test/fetch/fetch.spec.js @@ -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({ @@ -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) + } }) })