Skip to content

Commit 2447919

Browse files
committed
Fix setting undefined so it returns aan error and add tests.
1 parent e958989 commit 2447919

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ function redisStore(args) {
7171
}
7272

7373
if (opts.parse) {
74-
result = JSON.parse(result);
74+
75+
try {
76+
result = JSON.parse(result);
77+
} catch (e) {
78+
return cb && cb(e);
79+
}
7580
}
7681

7782
if (cb) {
@@ -113,10 +118,16 @@ function redisStore(args) {
113118
* @param {Function} [cb] - A callback that returns a potential error, otherwise null
114119
*/
115120
self.set = function(key, value, options, cb) {
121+
116122
if (typeof options === 'function') {
117123
cb = options;
118124
options = {};
119125
}
126+
127+
if (value === undefined) {
128+
return cb && cb(new Error('value cannot be undefined'));
129+
}
130+
120131
options = options || {};
121132

122133
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : redisOptions.ttl;
@@ -126,7 +137,6 @@ function redisStore(args) {
126137
return cb && cb(err);
127138
}
128139
var val = JSON.stringify(value);
129-
130140
if (ttl) {
131141
conn.setex(key, ttl, val, handleResponse(conn, cb));
132142
} else {

spec/lib/redis-store-spec.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,30 @@ describe('set', function() {
5050
});
5151

5252
it('should not store an invalid value', function(done) {
53-
redisCache.set('foo1', undefined, function(){
54-
redisCache.get('foo1', function(err, value) {
53+
redisCache.set('foo1', undefined, function(err){
54+
try {
55+
expect(err).notToBe(null);
56+
expect(err.message).toEqual('value cannot be undefined');
57+
done();
58+
} catch (e) {
59+
done(e);
60+
}
61+
});
62+
});
63+
});
64+
65+
it('should store a null value without error', function(done) {
66+
redisCache.set('foo2', null, function(err){
67+
try {
68+
expect(err).toBe(null);
69+
redisCache.get('foo2', function(err, value) {
5570
expect(err).toBe(null);
5671
expect(value).toBe(null);
5772
done();
5873
});
59-
});
74+
} catch (e) {
75+
done(e);
76+
}
6077
});
6178
});
6279

0 commit comments

Comments
 (0)