From 9376013def9e60a835ef2e3080ca880b9c689298 Mon Sep 17 00:00:00 2001 From: Adam Uhlir Date: Thu, 27 Jun 2019 13:01:52 +0200 Subject: [PATCH] feat: not found error for config values License: MIT Signed-off-by: Adam Uhlir --- src/config.js | 3 ++- src/errors/index.js | 15 +++++++++++++++ test/config-test.js | 10 ++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index e17e3aa8..2292ccb6 100644 --- a/src/config.js +++ b/src/config.js @@ -6,6 +6,7 @@ const _get = require('just-safe-get') const _set = require('just-safe-set') const _has = require('lodash.has') const errcode = require('err-code') +const errors = require('./errors') const configKey = new Key('config') @@ -27,7 +28,7 @@ module.exports = (store) => { const encodedValue = await store.get(configKey) const config = JSON.parse(encodedValue.toString()) if (key !== undefined && !_has(config, key)) { - throw new Error(`Key ${key} does not exist in config`) + throw new errors.NotFoundError(`Key ${key} does not exist in config`) } const value = key !== undefined ? _get(config, key) : config diff --git a/src/errors/index.js b/src/errors/index.js index 78c51a40..23addfbf 100644 --- a/src/errors/index.js +++ b/src/errors/index.js @@ -1,5 +1,20 @@ 'use strict' +/** + * Error raised when requested item is not found. + */ +class NotFoundError extends Error { + constructor (message) { + super(message) + this.name = 'NotFoundError' + this.code = 'ERR_NOT_FOUND' + this.message = message + } +} + +NotFoundError.code = 'ERR_NOT_FOUND' +exports.NotFoundError = NotFoundError + exports.ERR_REPO_NOT_INITIALIZED = 'ERR_REPO_NOT_INITIALIZED' exports.ERR_REPO_ALREADY_OPEN = 'ERR_REPO_ALREADY_OPEN' exports.ERR_REPO_ALREADY_CLOSED = 'ERR_REPO_ALREADY_CLOSED' diff --git a/test/config-test.js b/test/config-test.js index eac63dc7..f753de02 100644 --- a/test/config-test.js +++ b/test/config-test.js @@ -26,5 +26,15 @@ module.exports = (repo) => { } }) }) + describe('.get', () => { + it('should throw NotFoundError when key does not exist', async () => { + try { + await repo.config.get('someRandomKey') + throw new Error('Should have thrown') + } catch (err) { + expect(err.code).to.equal('ERR_NOT_FOUND') + } + }) + }) }) }