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

Commit df12740

Browse files
Sebastian Hallum Clarkembroadst
Sebastian Hallum Clarke
authored andcommitted
feat(errors): create MongoNetworkError
NODE-1080. Related to NODE-1057 and NODE-1061.
1 parent b271c2b commit df12740

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ try {
1313

1414
module.exports = {
1515
MongoError: require('./lib/error')
16+
, MongoNetworkError: require('./lib/network_error')
1617
, Connection: require('./lib/connection/connection')
1718
, Server: require('./lib/topologies/server')
1819
, ReplSet: require('./lib/topologies/replset')

lib/connection/pool.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var inherits = require('util').inherits,
44
EventEmitter = require('events').EventEmitter,
55
Connection = require('./connection'),
66
MongoError = require('../error'),
7+
MongoNetworkError = require('../network_error'),
78
Logger = require('./logger'),
89
f = require('util').format,
910
Query = require('./commands').Query,
@@ -322,7 +323,7 @@ function attemptReconnect(self) {
322323
self.destroy();
323324
// Emit close event
324325
self.emit('reconnectFailed'
325-
, new MongoError(f('failed to reconnect after %s attempts with interval %s ms', self.options.reconnectTries, self.options.reconnectInterval)));
326+
, new MongoNetworkError(f('failed to reconnect after %s attempts with interval %s ms', self.options.reconnectTries, self.options.reconnectInterval)));
326327
} else {
327328
self.reconnectId = setTimeout(attemptReconnect(self), self.options.reconnectInterval);
328329
}

lib/cursor.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var Logger = require('./connection/logger')
44
, retrieveBSON = require('./connection/utils').retrieveBSON
55
, MongoError = require('./error')
6+
, MongoNetworkError = require('./network_error')
67
, f = require('util').format;
78

89
var BSON = retrieveBSON(),
@@ -443,7 +444,7 @@ var isConnectionDead = function(self, callback) {
443444
self.cursorState.killed = true;
444445
self.cursorState.documents = [];
445446
self.cursorState.cursorIndex = 0;
446-
callback(MongoError.create(f('connection to host %s:%s was destroyed', self.pool.host, self.pool.port)))
447+
callback(MongoNetworkError.create(f('connection to host %s:%s was destroyed', self.pool.host, self.pool.port)))
447448
return true;
448449
}
449450

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

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

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

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

609610
// Check if connection is dead and return if not possible to
610611
// execute a getmore on this connection

lib/network_error.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict";
2+
3+
/**
4+
* Creates a new MongoNetworkError
5+
* @class
6+
* @augments Error
7+
* @param {string} message The error message
8+
* @return {MongoNetworkError} A MongoNetworkError instance
9+
*/
10+
function MongoNetworkError(message) {
11+
this.name = 'MongoNetworkError';
12+
this.message = message;
13+
Error.captureStackTrace(this, MongoNetworkError);
14+
}
15+
16+
/**
17+
* Creates a new MongoNetworkError object
18+
* @method
19+
* @param {object} options The error options
20+
* @return {MongoNetworkError} A MongoNetworkError instance
21+
*/
22+
MongoNetworkError.create = function(options) {
23+
var err = null;
24+
25+
if(options instanceof Error) {
26+
err = new MongoNetworkError(options.message);
27+
err.stack = options.stack;
28+
} else if(typeof options == 'string') {
29+
err = new MongoNetworkError(options);
30+
} else {
31+
err = new MongoNetworkError(options.message || options.errmsg || options.$err || "n/a");
32+
// Other options
33+
for(var name in options) {
34+
err[name] = options[name];
35+
}
36+
}
37+
38+
return err;
39+
}
40+
41+
// Extend JavaScript error
42+
MongoNetworkError.prototype = new Error;
43+
44+
module.exports = MongoNetworkError;

lib/topologies/server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var inherits = require('util').inherits,
1010
Pool = require('../connection/pool'),
1111
Query = require('../connection/commands').Query,
1212
MongoError = require('../error'),
13+
MongoNetworkError = require('../network_error'),
1314
PreTwoSixWireProtocolSupport = require('../wireprotocol/2_4_support'),
1415
TwoSixWireProtocolSupport = require('../wireprotocol/2_6_support'),
1516
ThreeTwoWireProtocolSupport = require('../wireprotocol/3_2_support'),
@@ -350,7 +351,7 @@ var eventHandler = function(self, event) {
350351
// On first connect fail
351352
if(self.s.pool.state == 'disconnected' && self.initalConnect && ['close', 'timeout', 'error', 'parseError'].indexOf(event) != -1) {
352353
self.initalConnect = false;
353-
return self.emit('error', new MongoError(f('failed to connect to server [%s] on first connect [%s]', self.name, err)));
354+
return self.emit('error', new MongoNetworkError(f('failed to connect to server [%s] on first connect [%s]', self.name, err)));
354355
}
355356

356357
// Reconnect event, emit the server

0 commit comments

Comments
 (0)