cast.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*!
  2. * Module dependencies.
  3. */
  4. var MongooseError = require('../error.js');
  5. var util = require('util');
  6. /**
  7. * Casting Error constructor.
  8. *
  9. * @param {String} type
  10. * @param {String} value
  11. * @inherits MongooseError
  12. * @api private
  13. */
  14. function CastError(type, value, path, reason) {
  15. var stringValue = util.inspect(value);
  16. stringValue = stringValue.replace(/^'/, '"').replace(/'$/, '"');
  17. if (stringValue.charAt(0) !== '"') {
  18. stringValue = '"' + stringValue + '"';
  19. }
  20. MongooseError.call(this, 'Cast to ' + type + ' failed for value ' +
  21. stringValue + ' at path "' + path + '"');
  22. if (Error.captureStackTrace) {
  23. Error.captureStackTrace(this);
  24. } else {
  25. this.stack = new Error().stack;
  26. }
  27. this.stringValue = stringValue;
  28. this.name = 'CastError';
  29. this.kind = type;
  30. this.value = value;
  31. this.path = path;
  32. this.reason = reason;
  33. }
  34. /*!
  35. * Inherits from MongooseError.
  36. */
  37. CastError.prototype = Object.create(MongooseError.prototype);
  38. CastError.prototype.constructor = MongooseError;
  39. /*!
  40. * ignore
  41. */
  42. CastError.prototype.setModel = function(model) {
  43. this.model = model;
  44. this.message = 'Cast to ' + this.kind + ' failed for value ' +
  45. this.stringValue + ' at path "' + this.path + '"' + ' for model "' +
  46. model.modelName + '"';
  47. };
  48. /*!
  49. * exports
  50. */
  51. module.exports = CastError;