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

Commit

Permalink
feat(errors): create MongoNetworkError
Browse files Browse the repository at this point in the history
NODE-1080. Related to NODE-1057 and NODE-1061.
  • Loading branch information
Sebastian Hallum Clarke authored and mbroadst committed Aug 6, 2017
1 parent b271c2b commit df12740
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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
3 changes: 2 additions & 1 deletion lib/connection/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var inherits = require('util').inherits,
EventEmitter = require('events').EventEmitter,
Connection = require('./connection'),
MongoError = require('../error'),
MongoNetworkError = require('../network_error'),
Logger = require('./logger'),
f = require('util').format,
Query = require('./commands').Query,
Expand Down Expand Up @@ -322,7 +323,7 @@ function attemptReconnect(self) {
self.destroy();
// Emit close event
self.emit('reconnectFailed'
, new MongoError(f('failed to reconnect after %s attempts with interval %s ms', self.options.reconnectTries, self.options.reconnectInterval)));
, new MongoNetworkError(f('failed to reconnect after %s attempts with interval %s ms', self.options.reconnectTries, self.options.reconnectInterval)));
} else {
self.reconnectId = setTimeout(attemptReconnect(self), self.options.reconnectInterval);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var Logger = require('./connection/logger')
, retrieveBSON = require('./connection/utils').retrieveBSON
, MongoError = require('./error')
, MongoNetworkError = require('./network_error')
, f = require('util').format;

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

Expand Down Expand Up @@ -578,7 +579,7 @@ var nextFunction = function(self, callback) {
if(isConnectionDead(self, callback)) return;

// Check if topology is destroyed
if(self.topology.isDestroyed()) return callback(new MongoError('connection destroyed, not possible to instantiate cursor'));
if(self.topology.isDestroyed()) return callback(new MongoNetworkError('connection destroyed, not possible to instantiate cursor'));

// query, cmd, options, cursorState, callback
self._find(function(err) {
Expand All @@ -604,7 +605,7 @@ var nextFunction = function(self, callback) {
self.cursorState.cursorIndex = 0;

// Check if topology is destroyed
if(self.topology.isDestroyed()) return callback(new MongoError('connection destroyed, not possible to instantiate cursor'));
if(self.topology.isDestroyed()) return callback(new MongoNetworkError('connection destroyed, not possible to instantiate cursor'));

// Check if connection is dead and return if not possible to
// execute a getmore on this connection
Expand Down
44 changes: 44 additions & 0 deletions lib/network_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";

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

/**
* Creates a new MongoNetworkError object
* @method
* @param {object} options The error options
* @return {MongoNetworkError} A MongoNetworkError instance
*/
MongoNetworkError.create = function(options) {
var err = null;

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

return err;
}

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

module.exports = MongoNetworkError;
3 changes: 2 additions & 1 deletion lib/topologies/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var inherits = require('util').inherits,
Pool = require('../connection/pool'),
Query = require('../connection/commands').Query,
MongoError = require('../error'),
MongoNetworkError = require('../network_error'),
PreTwoSixWireProtocolSupport = require('../wireprotocol/2_4_support'),
TwoSixWireProtocolSupport = require('../wireprotocol/2_6_support'),
ThreeTwoWireProtocolSupport = require('../wireprotocol/3_2_support'),
Expand Down Expand Up @@ -350,7 +351,7 @@ var eventHandler = function(self, event) {
// On first connect fail
if(self.s.pool.state == 'disconnected' && self.initalConnect && ['close', 'timeout', 'error', 'parseError'].indexOf(event) != -1) {
self.initalConnect = false;
return self.emit('error', new MongoError(f('failed to connect to server [%s] on first connect [%s]', self.name, err)));
return self.emit('error', new MongoNetworkError(f('failed to connect to server [%s] on first connect [%s]', self.name, err)));
}

// Reconnect event, emit the server
Expand Down

0 comments on commit df12740

Please sign in to comment.