Skip to content
This repository has been archived by the owner on Mar 23, 2023. 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 <jacobheun@gmail.com>
  • Loading branch information
jacobheun authored and daviddias committed Sep 19, 2018
1 parent 0c1d508 commit 02a5146
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const levelup = require('levelup')
const asyncFilter = require('interface-datastore').utils.asyncFilter
const asyncSort = require('interface-datastore').utils.asyncSort
const Key = require('interface-datastore').Key
const Errors = require('interface-datastore').Errors
const encode = require('encoding-down')

/**
Expand Down Expand Up @@ -50,15 +51,30 @@ class LevelDatastore {
}

open (callback /* : Callback<void> */) /* : void */ {
this.db.open(callback)
this.db.open((err) => {
if (err) {
return callback(Errors.dbOpenFailedError(err))
}
callback()
})
}

put (key /* : Key */, value /* : Buffer */, callback /* : Callback<void> */) /* : void */ {
this.db.put(key.toString(), value, callback)
this.db.put(key.toString(), value, (err) => {
if (err) {
return callback(Errors.dbWriteFailedError(err))
}
callback()
})
}

get (key /* : Key */, callback /* : Callback<Buffer> */) /* : void */ {
this.db.get(key.toString(), callback)
this.db.get(key.toString(), (err, data) => {
if (err) {
return callback(Errors.notFoundError(err))
}
callback(null, data)
})
}

has (key /* : Key */, callback /* : Callback<bool> */) /* : void */ {
Expand All @@ -78,8 +94,10 @@ class LevelDatastore {

delete (key /* : Key */, callback /* : Callback<void> */) /* : void */ {
this.db.del(key.toString(), (err) => {
// Avoid level passing additional arguments to callback, we dont need them
callback(err)
if (err) {
return callback(Errors.dbDeleteFailedError(err))
}
callback()
})
}

Expand Down

0 comments on commit 02a5146

Please sign in to comment.