Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try catch error in 'get' #37

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/connect-redis.js
Expand Up @@ -85,20 +85,21 @@ module.exports = function(connect){
* @param {Function} fn * @param {Function} fn
* @api public * @api public
*/ */

RedisStore.prototype.get = function(sid, fn){ RedisStore.prototype.get = function(sid, fn){
sid = this.prefix + sid; sid = this.prefix + sid;
debug('GET "%s"', sid); debug('GET "%s"', sid);
this.client.get(sid, function(err, data){ this.client.get(sid, function(err, data){
if (err) return fn(err); if (err) return fn(err);
if (!data) return fn();
var result;
data = data.toString();
debug('GOT %s', data);
try { try {
if (!data) return fn(); result = JSON.parse(data);
data = data.toString();
debug('GOT %s', data);
fn(null, JSON.parse(data));
} catch (err) { } catch (err) {
fn(err); return fn(err);
} }
return fn(null, result);
}); });
}; };


Expand Down
11 changes: 8 additions & 3 deletions test.js
Expand Up @@ -15,12 +15,12 @@ store.client.on('connect', function(){
store.set('123', { cookie: { maxAge: 2000 }, name: 'tj' }, function(err, ok){ store.set('123', { cookie: { maxAge: 2000 }, name: 'tj' }, function(err, ok){
assert.ok(!err, '#set() got an error'); assert.ok(!err, '#set() got an error');
assert.ok(ok, '#set() is not ok'); assert.ok(ok, '#set() is not ok');

// #get() // #get()
store.get('123', function(err, data){ store.get('123', function(err, data){
assert.ok(!err, '#get() got an error'); assert.ok(!err, '#get() got an error');
assert.deepEqual({ cookie: { maxAge: 2000 }, name: 'tj' }, data); assert.deepEqual({ cookie: { maxAge: 2000 }, name: 'tj' }, data);

// #set null // #set null
store.set('123', { cookie: { maxAge: 2000 }, name: 'tj' }, function(){ store.set('123', { cookie: { maxAge: 2000 }, name: 'tj' }, function(){
store.destroy('123', function(){ store.destroy('123', function(){
Expand All @@ -29,6 +29,11 @@ store.client.on('connect', function(){
store_alt.client.end(); store_alt.client.end();
}); });
}); });
}) throw new Error('Error in fn');
});
}); });
}); });

process.once('uncaughtException', function (err) {
assert.ok(err.message === 'Error in fn', '#get() catch wrong error');
});