Skip to content
This repository

How to correctly auth to server; what error to look for if you're doing it wrong; emit Error objects not string #183

Merged
merged 3 commits into from over 2 years ago

2 participants

David Trejo Matt Ranney
David Trejo
Collaborator

This is a documentation & correct error throwing fix for #155 & #172

Super short summary of what this does: explains how to correctly auth to server; what error to look for if you're doing it wrong; and code to emit Error objects not strings.

Tests are still passing :)

PS Yes, by re-making the Error object you do lose one stack frame from the error, but it's ok since it is not relevant (from what I saw).

I looked through all of index.js and corrected all instances of strings being thrown/emitted instead of Errors.

Matt Ranney mranney merged commit a02e058 into from
Matt Ranney mranney closed this
Matt Ranney
Owner

This looks great, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 8 additions and 4 deletions. Show diff stats Hide diff stats

  1. +5 1 README.md
  2. +3 3 index.js
6 README.md
Source Rendered
@@ -186,7 +186,7 @@ be loading the database from disk. While loading, the server not respond to any
186 186 indicates whether the server is ready for more commands. When ready, `node_redis` emits a `ready` event.
187 187 Setting `no_ready_check` to `true` will inhibit this check.
188 188
189   -
  189 +```js
190 190 var redis = require("redis"),
191 191 client = redis.createClient(null, null, {detect_buffers: true});
192 192
@@ -202,6 +202,7 @@ Setting `no_ready_check` to `true` will inhibit this check.
202 202 console.log(reply.toString()); // Will print `<Buffer 4f 4b>`
203 203 });
204 204 client.end();
  205 +```
205 206
206 207 `createClient()` returns a `RedisClient` object that is named `client` in all of the examples here.
207 208
@@ -212,6 +213,9 @@ first command after connecting. This can be tricky to coordinate with reconnect
212 213 etc. To make this easier, `client.auth()` stashes `password` and will send it after each connection,
213 214 including reconnections. `callback` is invoked only once, after the response to the very first
214 215 `AUTH` command sent.
  216 +NOTE: Your call to `client.auth()` should not be inside the ready handler. If
  217 +you are doing this wrong, `client` will emit an error that looks
  218 +something like this `Error: Ready check failed: ERR operation not permitted`.
215 219
216 220 ## client.end()
217 221
6 index.js
@@ -160,11 +160,11 @@ RedisClient.prototype.do_auth = function () {
160 160 }, 2000); // TODO - magic number alert
161 161 return;
162 162 } else {
163   - return self.emit("error", "Auth error: " + err);
  163 + return self.emit("error", new Error("Auth error: " + err.message));
164 164 }
165 165 }
166 166 if (res.toString() !== "OK") {
167   - return self.emit("error", "Auth failed: " + res.toString());
  167 + return self.emit("error", new Error("Auth failed: " + res.toString()));
168 168 }
169 169 if (exports.debug_mode) {
170 170 console.log("Auth succeeded " + self.host + ":" + self.port + " id " + self.connection_id);
@@ -290,7 +290,7 @@ RedisClient.prototype.on_info_cmd = function (err, res) {
290 290 var self = this, obj = {}, lines, retry_time;
291 291
292 292 if (err) {
293   - return self.emit("error", "Ready check failed: " + err);
  293 + return self.emit("error", new Error("Ready check failed: " + err.message));
294 294 }
295 295
296 296 lines = res.toString().split("\r\n");

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.