Skip to content

Commit

Permalink
Introducing not found error for config values
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau committed Jun 27, 2019
1 parent 62cc749 commit cfcd0b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/errors/index.js
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit cfcd0b0

Please sign in to comment.