From a2255d7fe2e156a221925dbaf3623672bcbcab96 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 9 Mar 2017 19:13:04 -0300 Subject: [PATCH] Fix error messages not being visible in the stack trace of AbortErrors --- index.js | 2 ++ lib/customErrors.js | 25 +++++++++++++++++++------ test/node_redis.spec.js | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 23c11ef333..909ebb1a91 100644 --- a/index.js +++ b/index.js @@ -1087,6 +1087,8 @@ exports.RedisClient = RedisClient; exports.print = utils.print; exports.Multi = require('./lib/multi'); exports.AbortError = errorClasses.AbortError; +exports.RedisError = Parser.RedisError; +exports.ParserError = Parser.ParserError; exports.ReplyError = Parser.ReplyError; exports.AggregateError = errorClasses.AggregateError; diff --git a/lib/customErrors.js b/lib/customErrors.js index bdf2feda0c..0ffdff400f 100644 --- a/lib/customErrors.js +++ b/lib/customErrors.js @@ -1,42 +1,55 @@ 'use strict'; var util = require('util'); +var assert = require('assert') +var RedisError = require('redis-parser').RedisError +var ADD_STACKTRACE = false -function AbortError (obj) { - Error.captureStackTrace(this, this.constructor); +function AbortError (obj, stack) { + assert(obj, 'The options argument is required') + assert.strictEqual(typeof obj, 'object', 'The options argument has to be of type object') + + RedisError.call(this, obj.message, ADD_STACKTRACE) Object.defineProperty(this, 'message', { value: obj.message || '', configurable: true, writable: true }); + if (stack || stack === undefined) { + Error.captureStackTrace(this, AbortError) + } for (var keys = Object.keys(obj), key = keys.pop(); key; key = keys.pop()) { this[key] = obj[key]; } } function AggregateError (obj) { - Error.captureStackTrace(this, this.constructor); + assert(obj, 'The options argument is required') + assert.strictEqual(typeof obj, 'object', 'The options argument has to be of type object') + + AbortError.call(this, obj, ADD_STACKTRACE) Object.defineProperty(this, 'message', { value: obj.message || '', configurable: true, writable: true }); + Error.captureStackTrace(this, AggregateError); for (var keys = Object.keys(obj), key = keys.pop(); key; key = keys.pop()) { this[key] = obj[key]; } } -util.inherits(AbortError, Error); +util.inherits(AbortError, RedisError); util.inherits(AggregateError, AbortError); Object.defineProperty(AbortError.prototype, 'name', { value: 'AbortError', - // configurable: true, + configurable: true, writable: true }); Object.defineProperty(AggregateError.prototype, 'name', { value: 'AggregateError', - // configurable: true, + configurable: true, writable: true }); diff --git a/test/node_redis.spec.js b/test/node_redis.spec.js index dfb744354b..d9fbc09728 100644 --- a/test/node_redis.spec.js +++ b/test/node_redis.spec.js @@ -870,7 +870,7 @@ describe('The node_redis client', function () { client.on('error', function (err) { assert.strictEqual(err.message, 'Protocol error, got "a" as reply type byte. Please report this.'); assert.strictEqual(err, error); - assert(err instanceof redis.ReplyError); + assert(err instanceof redis.ParserError); // After the hard failure work properly again. The set should have been processed properly too client.get('foo', function (err, res) { assert.strictEqual(res, 'bar');