Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing not found error for config values #201

Merged
merged 1 commit into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
10 changes: 10 additions & 0 deletions test/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
})
})
})
}