Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@
},
"homepage": "https://github.com/ipfs/interface-datastore#readme",
"devDependencies": {
"aegir": "^12.2.0",
"aegir": "^15.1.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"flow-bin": "^0.60.1"
"flow-bin": "~0.60.1"
},
"dependencies": {
"async": "^2.6.0",
"pull-defer": "^0.2.2",
"pull-stream": "^3.6.1",
"uuid": "^3.1.0"
"async": "^2.6.1",
"err-code": "^1.1.2",
"pull-defer": "~0.2.3",
"pull-stream": "^3.6.9",
"uuid": "^3.3.2"
},
"engines": {
"node": ">=6.0.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')
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERR_DB_OPEN_FAILED for consistency with the others?


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')
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better for these factory functions to be named more like dbDeleteError(), notFoundError()? All caps var names are by popular convention constant values and not functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the function names and the open failure code

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