Skip to content

Commit 1a8aa12

Browse files
committed
Fix storage.get(..., defaultValue)
1 parent 36b088a commit 1a8aa12

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/storage/backing/fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ class FSBacking extends StorageInterface {
103103
}
104104
}
105105

106-
async get(ns, key, defaultValue) {
106+
async get(ns, key) {
107107
try {
108108
return await freadfile(path.join(this.root, ns, key));
109109
} catch(e) {
110110
if (e.code === 'ENOENT') {
111-
return defaultValue;
111+
throw new ReferenceError(key);
112112
} else {
113113
throw e;
114114
}

src/storage/backing/interface.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class StorageInterface {
99
throw new Error("Not Implemented");
1010
}
1111

12-
async get(ns, key, defaultValue) {
12+
async get(ns, key) {
13+
/* If key not found should throw ReferenceError */
1314
throw new Error("Not Implemented");
1415
}
1516

src/storage/backing/redis.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ class RedisBacking extends StorageInterface {
6565
await this.client.set(this.label + '-' + ns, key, value);
6666
}
6767

68-
async get(ns, key, defaultValue) {
68+
async get(ns, key) {
6969
if (await this.client.exists(this.label + '-' + ns, key)) {
7070
return await this.client.get(this.label + '-' + ns, key);
7171
} else {
72-
return defaultValue;
72+
throw new ReferenceError(key);
7373
}
7474
}
7575

src/storage/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,17 @@ function decode(obj) {
5252
}
5353
}
5454

55-
exports.get = async (ns, key) => {
56-
const data = await _backing.get(ns, key);
55+
exports.get = async (ns, key, defaultValue) => {
56+
let data;
57+
try {
58+
data = await _backing.get(ns, key);
59+
} catch(e) {
60+
if (e instanceof ReferenceError) {
61+
return defaultValue;
62+
} else {
63+
throw e;
64+
}
65+
}
5766
return data && decode(data);
5867
};
5968
exports.set = (ns, key, value) => _backing.set(ns, key, encode(value));

0 commit comments

Comments
 (0)