Skip to content

Commit

Permalink
Merge pull request #65 from dead-horse/try-catch
Browse files Browse the repository at this point in the history
fix try catch bug in get & update test
  • Loading branch information
tj committed Oct 25, 2012
2 parents 0c6155d + 3bb605c commit 806b0ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
14 changes: 8 additions & 6 deletions lib/connect-redis.js
Expand Up @@ -91,14 +91,16 @@ module.exports = function(connect){
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');
});

0 comments on commit 806b0ef

Please sign in to comment.