From a6bfae83b922b012b1ae3a5df9cdd1a87b35db4c Mon Sep 17 00:00:00 2001 From: Matt Ranney Date: Thu, 23 Sep 2010 14:41:34 -0700 Subject: [PATCH] Docs and example of listening for "error" event. --- README.md | 21 +++++++++++++++++++-- example.js | 4 ++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e5cce90d81..050f94dbf0 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ Simple example, included as `example.js`: var redis = require("redis"), client = redis.createClient(); + client.on("error", function (err) { + console.log("Redis connection error to " + client.host + ":" + client.port + " - " + err); + }); + client.set("string key", "string val", redis.print); client.hset("hash key", "hashtest 1", "some value", redis.print); client.hset(["hash key", "hashtest 2", "some other value"], redis.print); @@ -87,8 +91,21 @@ Commands issued before the `connect` event are queued, then replayed when a conn `client` will emit `error` when encountering an error connecting to the Redis server. -_This may change at some point, because it would be nice to send back error events for -things in the reply parser._ +Note that "error" is a special event type in node. If there are no listeners for an +"error" event, node will exit. This is usually what you want, but it can lead to some +cryptic error messages like this: + + mjr:~/work/node_redis (master)$ node example.js + + node.js:50 + throw e; + ^ + Error: ECONNREFUSED, Connection refused + at IOWatcher.callback (net:870:22) + at node.js:607:9 + +Not very useful in diagnosing the problem, but if your program isn't ready to handle this, +it is probably the right thing to just exit. ### "end" diff --git a/example.js b/example.js index 40c1746578..b93c557d2b 100644 --- a/example.js +++ b/example.js @@ -1,6 +1,10 @@ var redis = require("redis"), client = redis.createClient(); +client.on("error", function (err) { + console.log("Redis connection error to " + client.host + ":" + client.port + " - " + err); +}); + client.set("string key", "string val", redis.print); client.hset("hash key", "hashtest 1", "some value", redis.print); client.hset(["hash key", "hashtest 2", "some other value"], redis.print);