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

Commit

Permalink
feat: add basic error codes
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jacob Heun <[email protected]>
  • Loading branch information
jacobheun authored and daviddias committed Sep 17, 2018
1 parent 9874560 commit bbf5f70
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"async": "^2.6.0",
"err-code": "^1.1.2",
"pull-defer": "^0.2.2",
"pull-stream": "^3.6.1",
"uuid": "^3.1.0"
Expand Down
23 changes: 23 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const errcode = require('err-code')

module.exports.ERR_DB_CANNOT_OPEN = (err) => {
err = err || new Error('Cannot open database')
return errcode(err, 'ERR_CANNOT_OPEN_DB')
}

module.exports.ERR_DB_DELETE_FAILED = (err) => {
err = err || new Error('Delete failed')
return errcode(err, 'ERR_DB_DELETE_FAILED')
}

module.exports.ERR_DB_WRITE_FAILED = (err) => {
err = err || new Error('Write failed')
return errcode(err, 'ERR_DB_WRITE_FAILED')
}

module.exports.ERR_NOT_FOUND = (err) => {
err = err || new Error('Not Found')
return errcode(err, 'ERR_NOT_FOUND')
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
const Key = require('./key')
const MemoryDatastore = require('./memory')
const utils = require('./utils')
const Errors = require('./errors')

exports.Key = Key
exports.MemoryDatastore = MemoryDatastore
exports.utils = utils
exports.Errors = Errors

/* ::
// -- Basics
Expand Down
6 changes: 5 additions & 1 deletion src/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const asyncFilter = require('./utils').asyncFilter
const asyncSort = require('./utils').asyncSort
const Key = require('./key')

// Errors
const Errors = require('./errors')
const ERR_NOT_FOUND = Errors.ERR_NOT_FOUND

class MemoryDatastore {
/* :: data: {[key: string]: Buffer} */

Expand All @@ -34,7 +38,7 @@ class MemoryDatastore {
}

if (!exists) {
return callback(new Error('No value'))
return callback(ERR_NOT_FOUND())
}

callback(null, this.data[key.toString()])
Expand Down
9 changes: 9 additions & 0 deletions src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ module.exports = (test/* : Test */) => {
})
], done)
})

it('should return error with missing key', (done) => {
const k = new Key('/does/not/exist')
check(store).get(k, (err) => {
expect(err).to.exist()
expect(err).to.have.property('code', 'ERR_NOT_FOUND')
done()
})
})
})

describe('delete', () => {
Expand Down

0 comments on commit bbf5f70

Please sign in to comment.