Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Commit 5870831

Browse files
committed
feat: add basic error codes
License: MIT Signed-off-by: Jacob Heun <[email protected]>
1 parent 9874560 commit 5870831

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"dependencies": {
4343
"async": "^2.6.0",
44+
"err-code": "^1.1.2",
4445
"pull-defer": "^0.2.2",
4546
"pull-stream": "^3.6.1",
4647
"uuid": "^3.1.0"

src/errors.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const errcode = require('err-code')
4+
5+
module.exports.ERR_DB_CANNOT_OPEN = (err) => {
6+
err = err || new Error('Cannot open database')
7+
return errcode(err, 'ERR_CANNOT_OPEN_DB')
8+
}
9+
10+
module.exports.ERR_DB_DELETE_FAILED = (err) => {
11+
err = err || new Error('Delete failed')
12+
return errcode(err, 'ERR_DB_DELETE_FAILED')
13+
}
14+
15+
module.exports.ERR_DB_WRITE_FAILED = (err) => {
16+
err = err || new Error('Write failed')
17+
return errcode(err, 'ERR_DB_WRITE_FAILED')
18+
}
19+
20+
module.exports.ERR_NOT_FOUND = (err) => {
21+
err = err || new Error('Not Found')
22+
return errcode(err, 'ERR_NOT_FOUND')
23+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
const Key = require('./key')
55
const MemoryDatastore = require('./memory')
66
const utils = require('./utils')
7+
const Errors = require('./errors')
78

89
exports.Key = Key
910
exports.MemoryDatastore = MemoryDatastore
1011
exports.utils = utils
12+
exports.Errors = Errors
1113

1214
/* ::
1315
// -- Basics

src/memory.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const asyncFilter = require('./utils').asyncFilter
1010
const asyncSort = require('./utils').asyncSort
1111
const Key = require('./key')
1212

13+
// Errors
14+
const Errors = require('./errors')
15+
const ERR_NOT_FOUND = Errors.ERR_NOT_FOUND
16+
1317
class MemoryDatastore {
1418
/* :: data: {[key: string]: Buffer} */
1519

@@ -34,7 +38,7 @@ class MemoryDatastore {
3438
}
3539

3640
if (!exists) {
37-
return callback(new Error('No value'))
41+
return callback(ERR_NOT_FOUND())
3842
}
3943

4044
callback(null, this.data[key.toString()])

src/tests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ module.exports = (test/* : Test */) => {
111111
})
112112
], done)
113113
})
114+
115+
it('should return error with missing key', (done) => {
116+
const k = new Key('/does/not/exist')
117+
check(store).get(k, (err) => {
118+
expect(err).to.exist()
119+
expect(err).to.have.property('code', 'ERR_NOT_FOUND')
120+
done()
121+
})
122+
})
114123
})
115124

116125
describe('delete', () => {

0 commit comments

Comments
 (0)