Fix: Hiredis parser traps pubsub event exceptions #139
Merged
Conversation
While using this module with the hiredis parser, I noticed that exceptions
thrown inside of subscribe/message/etc. callbacks would be reported as
parser error exceptions, which was confusing.
Example:
```
var client = require('./index').createClient();
client.on('subscribe', function() {
throw new Error('My Error');
});
client.subscribe('channel');
```
I would expect the above to throw 'My Error' as a simple exception, however,
when using the hiredis parser I get:
```
events.js:45
throw arguments[1]; // Unhandled 'error' event
^
Error: Redis reply parser error: Error: My Error
at RedisClient.<anonymous> (/Users/felix/code/node_redis/hiredis.js:4:9)
at RedisClient.emit (events.js:67:17)
at RedisClient.return_reply (/Users/felix/code/node_redis/index.js:457:22)
at HiredisReplyParser.<anonymous> (/Users/felix/code/node_redis/index.js:220:14)
at HiredisReplyParser.emit (events.js:64:17)
at HiredisReplyParser.execute (/Users/felix/code/node_redis/lib/parser/hiredis.js:35:22)
at RedisClient.on_data (/Users/felix/code/node_redis/index.js:365:27)
at Socket.<anonymous> (/Users/felix/code/node_redis/index.js:58:14)
...
```
The attached patch fixes this problem by changing the way the HiredisReplyParser
traps exceptions, and also provides a unit test for it.
Please let me know if you need any tweaks on the patch for landing it, I was
not quite sure where to put my test / how to structure it, since most test
cases seem to be integration tests at this point.
Also, if you would prefer an integration test for this, here you go:
``` javascript
var client = require('./index').createClient();
var assert = require('assert');
process.on('uncaughtException', function listener(err) {
process.removeListener('uncaughtException', listener);
// Drop the stack, otherwise the assert gets trapped by node
process.nextTick(function() {
assert.ok(!/parser error/.test(err.message), err.message);
client.quit();
});
});
client.on('subscribe', function() {
throw new Error('My Error');
});
client.subscribe('channel');
```
PS: If you wonder why this only affects pubsub events: This is because
RedisClient#return_reply does exception trapping differently for events than
it does for callbacks.
See: https://github.com/mranney/node_redis/blob/3e95c55a0301104c9cd6451d71fa5527b7797792/index.js#L431
Vs: https://github.com/mranney/node_redis/blob/3e95c55a0301104c9cd6451d71fa5527b7797792/index.js#L447
Contributor
Author
|
Just noticed that I used 2 spaces instead of 4 in some places, I'll fix that if you confirm the overall patch to be good. |
|
+1. Makes it way easier to debug applications that use node-redis. |
Contributor
|
This is a win, thanks. This will go into the next version that I'm working on now. |
mranney
added a commit
that referenced
this pull request
Nov 14, 2011
Fix: Hiredis parser traps pubsub event exceptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While using this module with the hiredis parser, I noticed that exceptions
thrown inside of subscribe/message/etc. callbacks would be reported as
parser error exceptions, which was confusing.
Example:
I would expect the above to throw 'My Error' as a simple exception, however,
when using the hiredis parser I get:
The attached patch fixes this problem by changing the way the HiredisReplyParser
traps exceptions, and also provides a unit test for it.
Please let me know if you need any tweaks on the patch for landing it, I was
not quite sure where to put my test / how to structure it, since most test
cases seem to be integration tests at this point.
Also, if you would prefer an integration test for this, here you go:
PS: If you wonder why this only affects pubsub events: This is because
RedisClient#return_reply does exception trapping differently for events than
it does for callbacks.
See: https://github.com/mranney/node_redis/blob/3e95c55a0301104c9cd6451d71fa5527b7797792/index.js#L431
Vs: https://github.com/mranney/node_redis/blob/3e95c55a0301104c9cd6451d71fa5527b7797792/index.js#L447