serverSelection.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const MongooseError = require('./mongooseError');
  6. const allServersUnknown = require('../helpers/topology/allServersUnknown');
  7. const isAtlas = require('../helpers/topology/isAtlas');
  8. /*!
  9. * ignore
  10. */
  11. const atlasMessage = 'Could not connect to any servers in your MongoDB Atlas cluster. ' +
  12. 'One common reason is that you\'re trying to access the database from ' +
  13. 'an IP that isn\'t whitelisted. Make sure your current IP address is on your Atlas ' +
  14. 'cluster\'s IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/';
  15. class MongooseServerSelectionError extends MongooseError {
  16. /**
  17. * MongooseServerSelectionError constructor
  18. *
  19. * @api private
  20. */
  21. assimilateError(err) {
  22. const reason = err.reason;
  23. // Special message for a case that is likely due to IP whitelisting issues.
  24. const isAtlasWhitelistError = isAtlas(reason) && allServersUnknown(reason);
  25. this.message = isAtlasWhitelistError ?
  26. atlasMessage :
  27. err.message;
  28. for (const key in err) {
  29. if (key !== 'name') {
  30. this[key] = err[key];
  31. }
  32. }
  33. return this;
  34. }
  35. }
  36. Object.defineProperty(MongooseServerSelectionError.prototype, 'name', {
  37. value: 'MongooseServerSelectionError'
  38. });
  39. module.exports = MongooseServerSelectionError;