Skip to content

Commit a8e5860

Browse files
AuHaujacobheun
authored andcommitted
feat: not found error for config values (#201)
License: MIT Signed-off-by: Adam Uhlir <[email protected]>
1 parent c90cd62 commit a8e5860

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const _get = require('just-safe-get')
66
const _set = require('just-safe-set')
77
const _has = require('lodash.has')
88
const errcode = require('err-code')
9+
const errors = require('./errors')
910

1011
const configKey = new Key('config')
1112

@@ -27,7 +28,7 @@ module.exports = (store) => {
2728
const encodedValue = await store.get(configKey)
2829
const config = JSON.parse(encodedValue.toString())
2930
if (key !== undefined && !_has(config, key)) {
30-
throw new Error(`Key ${key} does not exist in config`)
31+
throw new errors.NotFoundError(`Key ${key} does not exist in config`)
3132
}
3233

3334
const value = key !== undefined ? _get(config, key) : config

src/errors/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
'use strict'
22

3+
/**
4+
* Error raised when requested item is not found.
5+
*/
6+
class NotFoundError extends Error {
7+
constructor (message) {
8+
super(message)
9+
this.name = 'NotFoundError'
10+
this.code = 'ERR_NOT_FOUND'
11+
this.message = message
12+
}
13+
}
14+
15+
NotFoundError.code = 'ERR_NOT_FOUND'
16+
exports.NotFoundError = NotFoundError
17+
318
exports.ERR_REPO_NOT_INITIALIZED = 'ERR_REPO_NOT_INITIALIZED'
419
exports.ERR_REPO_ALREADY_OPEN = 'ERR_REPO_ALREADY_OPEN'
520
exports.ERR_REPO_ALREADY_CLOSED = 'ERR_REPO_ALREADY_CLOSED'

test/config-test.js

+10
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,15 @@ module.exports = (repo) => {
2626
}
2727
})
2828
})
29+
describe('.get', () => {
30+
it('should throw NotFoundError when key does not exist', async () => {
31+
try {
32+
await repo.config.get('someRandomKey')
33+
throw new Error('Should have thrown')
34+
} catch (err) {
35+
expect(err.code).to.equal('ERR_NOT_FOUND')
36+
}
37+
})
38+
})
2939
})
3040
}

0 commit comments

Comments
 (0)