Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(errors): use subclassing for MongoNetworkError
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Hallum Clarke authored and mbroadst committed Aug 6, 2017
1 parent 2cec239 commit a132830
Show file tree
Hide file tree
Showing 29 changed files with 164 additions and 115 deletions.
1 change: 0 additions & 1 deletion conf.json
Expand Up @@ -13,7 +13,6 @@
"lib/connection/connection.js",
"lib/cursor.js",
"lib/error.js",
"lib/network_error.js",
"node_modules/bson/lib/bson/binary.js",
"node_modules/bson/lib/bson/code.js",
"node_modules/bson/lib/bson/db_ref.js",
Expand Down
1 change: 0 additions & 1 deletion index.js
Expand Up @@ -13,7 +13,6 @@ try {

module.exports = {
MongoError: require('./lib/error')
, MongoNetworkError: require('./lib/network_error')
, Connection: require('./lib/connection/connection')
, Server: require('./lib/topologies/server')
, ReplSet: require('./lib/topologies/replset')
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/gssapi.js
Expand Up @@ -3,7 +3,7 @@
var f = require('util').format
, require_optional = require('require_optional')
, Query = require('../connection/commands').Query
, MongoError = require('../error');
, MongoError = require('../error').MongoError;

var AuthSession = function(db, username, password, options) {
this.db = db;
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/mongocr.js
Expand Up @@ -3,7 +3,7 @@
var f = require('util').format
, crypto = require('crypto')
, Query = require('../connection/commands').Query
, MongoError = require('../error');
, MongoError = require('../error').MongoError;

var AuthSession = function(db, username, password) {
this.db = db;
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/plain.js
Expand Up @@ -6,7 +6,7 @@ var f = require('util').format
, Binary = BSON.Binary
, retrieveBSON = require('../connection/utils').retrieveBSON
, Query = require('../connection/commands').Query
, MongoError = require('../error');
, MongoError = require('../error').MongoError;

var BSON = retrieveBSON();

Expand Down
2 changes: 1 addition & 1 deletion lib/auth/scram.js
Expand Up @@ -4,7 +4,7 @@ var f = require('util').format
, crypto = require('crypto')
, retrieveBSON = require('../connection/utils').retrieveBSON
, Query = require('../connection/commands').Query
, MongoError = require('../error');
, MongoError = require('../error').MongoError;

var BSON = retrieveBSON(),
Binary = BSON.Binary;
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/sspi.js
Expand Up @@ -3,7 +3,7 @@
var f = require('util').format
, require_optional = require('require_optional')
, Query = require('../connection/commands').Query
, MongoError = require('../error');
, MongoError = require('../error').MongoError;

var AuthSession = function(db, username, password, options) {
this.db = db;
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/x509.js
Expand Up @@ -2,7 +2,7 @@

var f = require('util').format
, Query = require('../connection/commands').Query
, MongoError = require('../error');
, MongoError = require('../error').MongoError;

var AuthSession = function(db, username, password) {
this.db = db;
Expand Down
8 changes: 4 additions & 4 deletions lib/connection/connection.js
Expand Up @@ -10,7 +10,7 @@ var inherits = require('util').inherits
, parseHeader = require('../wireprotocol/shared').parseHeader
, decompress = require('../wireprotocol/compression').decompress
, Response = require('./commands').Response
, MongoNetworkError = require('../network_error')
, MongoNetworkError = require('../error').MongoNetworkError
, Logger = require('./logger')
, zlib = require('zlib')
, Snappy = require('./utils').retrieveSnappy()
Expand Down Expand Up @@ -190,7 +190,7 @@ var errorHandler = function(self) {
// Debug information
if(self.logger.isDebug()) self.logger.debug(f('connection %s for [%s:%s] errored out with [%s]', self.id, self.host, self.port, JSON.stringify(err)));
// Emit the error
if(self.listeners('error').length > 0) self.emit("error", MongoNetworkError.create(err), self);
if(self.listeners('error').length > 0) self.emit("error", new MongoNetworkError(err), self);
}
}

Expand All @@ -201,7 +201,7 @@ var timeoutHandler = function(self) {
if(self.logger.isDebug()) self.logger.debug(f('connection %s for [%s:%s] timed out', self.id, self.host, self.port));
// Emit timeout error
self.emit("timeout"
, MongoNetworkError.create(f("connection %s to %s:%s timed out", self.id, self.host, self.port))
, new MongoNetworkError(f("connection %s to %s:%s timed out", self.id, self.host, self.port))
, self);
}
}
Expand All @@ -215,7 +215,7 @@ var closeHandler = function(self) {
// Emit close event
if(!hadError) {
self.emit("close"
, MongoNetworkError.create(f("connection %s to %s:%s closed", self.id, self.host, self.port))
, new MongoNetworkError(f("connection %s to %s:%s closed", self.id, self.host, self.port))
, self);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/connection/logger.js
@@ -1,7 +1,7 @@
"use strict";

var f = require('util').format
, MongoError = require('../error');
, MongoError = require('../error').MongoError;

// Filters for classes
var classFilters = {};
Expand Down
8 changes: 4 additions & 4 deletions lib/connection/pool.js
Expand Up @@ -3,8 +3,8 @@
var inherits = require('util').inherits,
EventEmitter = require('events').EventEmitter,
Connection = require('./connection'),
MongoError = require('../error'),
MongoNetworkError = require('../network_error'),
MongoError = require('../error').MongoError,
MongoNetworkError = require('../error').MongoNetworkError,
Logger = require('./logger'),
f = require('util').format,
Query = require('./commands').Query,
Expand Down Expand Up @@ -496,13 +496,13 @@ function messageHandler(self) {
// Parse the message according to the provided options
message.parse(workItem);
} catch(err) {
return handleOperationCallback(self, workItem.cb, MongoError.create(err));
return handleOperationCallback(self, workItem.cb, new MongoError(err));
}

// Establish if we have an error
if(workItem.command && message.documents[0] && (message.documents[0].ok == 0 || message.documents[0]['$err']
|| message.documents[0]['errmsg'] || message.documents[0]['code'])) {
return handleOperationCallback(self, workItem.cb, MongoError.create(message.documents[0]));
return handleOperationCallback(self, workItem.cb, new MongoError(message.documents[0]));
}

// Add the connection details
Expand Down
16 changes: 8 additions & 8 deletions lib/cursor.js
Expand Up @@ -2,8 +2,8 @@

var Logger = require('./connection/logger')
, retrieveBSON = require('./connection/utils').retrieveBSON
, MongoError = require('./error')
, MongoNetworkError = require('./network_error')
, MongoError = require('./error').MongoError
, MongoNetworkError = require('./error').MongoNetworkError
, f = require('util').format;

var BSON = retrieveBSON(),
Expand Down Expand Up @@ -195,7 +195,7 @@ Cursor.prototype._find = function(callback) {

// Query failure bit set
if(result.queryFailure) {
return callback(MongoError.create(result.documents[0]), null);
return callback(new MongoError(result.documents[0]), null);
}

// Check if we have a command cursor
Expand All @@ -210,7 +210,7 @@ Cursor.prototype._find = function(callback) {
// We have a an error document return the error
if(result.documents[0]['$err']
|| result.documents[0]['errmsg']) {
return callback(MongoError.create(result.documents[0]), null);
return callback(new MongoError(result.documents[0]), null);
}

// We have a cursor document
Expand Down Expand Up @@ -444,7 +444,7 @@ var isConnectionDead = function(self, callback) {
self.cursorState.killed = true;
self.cursorState.documents = [];
self.cursorState.cursorIndex = 0;
callback(MongoNetworkError.create(f('connection to host %s:%s was destroyed', self.pool.host, self.pool.port)))
callback(new MongoNetworkError(f('connection to host %s:%s was destroyed', self.pool.host, self.pool.port)))
return true;
}

Expand Down Expand Up @@ -473,7 +473,7 @@ var isCursorDeadButNotkilled = function(self, callback) {
*/
var isCursorDeadAndKilled = function(self, callback) {
if(self.cursorState.dead && self.cursorState.killed) {
handleCallback(callback, MongoError.create('cursor is dead'));
handleCallback(callback, new MongoError('cursor is dead'));
return true;
}

Expand Down Expand Up @@ -624,7 +624,7 @@ var nextFunction = function(self, callback) {
if(self.cursorState.documents.length == 0
&& self.cmd.tailable && Long.ZERO.equals(self.cursorState.cursorId)) {
// No more documents in the tailed cursor
return handleCallback(callback, MongoError.create({
return handleCallback(callback, new MongoError({
message: 'No more documents in tailed cursor'
, tailable: self.cmd.tailable
, awaitData: self.cmd.awaitData
Expand All @@ -642,7 +642,7 @@ var nextFunction = function(self, callback) {
});
} else if(self.cursorState.documents.length == self.cursorState.cursorIndex
&& self.cmd.tailable && Long.ZERO.equals(self.cursorState.cursorId)) {
return handleCallback(callback, MongoError.create({
return handleCallback(callback, new MongoError({
message: 'No more documents in tailed cursor'
, tailable: self.cmd.tailable
, awaitData: self.cmd.awaitData
Expand Down
61 changes: 41 additions & 20 deletions lib/error.js
@@ -1,44 +1,65 @@
"use strict";

var util = require('util');

/**
* Creates a new MongoError
* @class
* @augments Error
* @param {string} message The error message
* @param {Error|string|object} message The error message
* @property {string} message The error message
* @property {string} stack The error call stack
* @return {MongoError} A MongoError instance
*/
function MongoError(message) {
this.name = 'MongoError';
this.message = message;
Error.captureStackTrace(this, MongoError);

if (message instanceof Error) {
this.message = message.message;
this.stack = message.stack;
} else {
if (typeof message === 'string') {
this.message = message;
} else {
this.message = message.message || message.errmsg || message.$err || 'n/a';
for (var name in message) {
this[name] = message[name];
}
}
Error.captureStackTrace(this, MongoError);
}
}

/**
* Creates a new MongoError object
* @method
* @param {Error|string|object} options The options used to create the error.
* @return {MongoError} A MongoError instance
* @deprecated Use new MongoError() instead.
*/
MongoError.create = function(options) {
var err = null;

if(options instanceof Error) {
err = new MongoError(options.message);
err.stack = options.stack;
} else if(typeof options == 'string') {
err = new MongoError(options);
} else {
err = new MongoError(options.message || options.errmsg || options.$err || "n/a");
// Other options
for(var name in options) {
err[name] = options[name];
}
}

return err;
return new MongoError(options);
}

// Extend JavaScript error
MongoError.prototype = new Error;

module.exports = MongoError;
/**
* Creates a new MongoNetworkError
* @class
* @param {Error|string|object} message The error message
* @property {string} message The error message
* @property {string} stack The error call stack
* @return {MongoNetworkError} A MongoNetworkError instance
* @extends {MongoError}
*/
var MongoNetworkError = function(message) {
MongoError.call(this, message);
this.name = 'MongoNetworkError';
};
util.inherits(MongoNetworkError, MongoError);

module.exports = {
MongoError: MongoError,
MongoNetworkError: MongoNetworkError
};
44 changes: 0 additions & 44 deletions lib/network_error.js

This file was deleted.

6 changes: 3 additions & 3 deletions lib/topologies/mongos.js
Expand Up @@ -6,7 +6,7 @@ var inherits = require('util').inherits,
BasicCursor = require('../cursor'),
Logger = require('../connection/logger'),
retrieveBSON = require('../connection/utils').retrieveBSON,
MongoError = require('../error'),
MongoError = require('../error').MongoError,
Server = require('./server'),
assign = require('../utils').assign,
clone = require('./shared').clone,
Expand Down Expand Up @@ -1022,7 +1022,7 @@ Mongos.prototype.auth = function(mechanism, db) {
// Remove the entry from the stored authentication contexts
self.s.authenticationContexts.splice(currentContextIndex, 0);
// Return error
return callback(MongoError.create({
return callback(new MongoError({
message: 'authentication fail', errors: errors
}), false);
}
Expand Down Expand Up @@ -1095,7 +1095,7 @@ Mongos.prototype.logout = function(dbName, callback) {
// Do not block new operations
self.authenticating = false;
// If we have one or more errors
if(errors.length) return callback(MongoError.create({
if(errors.length) return callback(new MongoError({
message: f('logout failed against db %s', dbName), errors: errors
}), false);

Expand Down
6 changes: 3 additions & 3 deletions lib/topologies/replset.js
Expand Up @@ -7,7 +7,7 @@ var inherits = require('util').inherits,
BasicCursor = require('../cursor'),
retrieveBSON = require('../connection/utils').retrieveBSON,
Logger = require('../connection/logger'),
MongoError = require('../error'),
MongoError = require('../error').MongoError,
Server = require('./server'),
ReplSetState = require('./replset_state'),
assign = require('../utils').assign,
Expand Down Expand Up @@ -1223,7 +1223,7 @@ ReplSet.prototype.auth = function(mechanism, db) {
// Remove the entry from the stored authentication contexts
self.s.authenticationContexts.splice(currentContextIndex, 0);
// Return error
return callback(MongoError.create({
return callback(new MongoError({
message: 'authentication fail', errors: errors
}), false);
}
Expand Down Expand Up @@ -1306,7 +1306,7 @@ ReplSet.prototype.logout = function(dbName, callback) {
// Do not block new operations
self.authenticating = false;
// If we have one or more errors
if(errors.length) return callback(MongoError.create({
if(errors.length) return callback(new MongoError({
message: f('logout failed against db %s', dbName), errors: errors
}), false);

Expand Down

0 comments on commit a132830

Please sign in to comment.