error-to-string.js 1.2 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var DESCRIPTORS = require('../internals/descriptors');
  3. var fails = require('../internals/fails');
  4. var anObject = require('../internals/an-object');
  5. var create = require('../internals/object-create');
  6. var normalizeStringArgument = require('../internals/normalize-string-argument');
  7. var nativeErrorToString = Error.prototype.toString;
  8. var INCORRECT_TO_STRING = fails(function () {
  9. if (DESCRIPTORS) {
  10. // Chrome 32- incorrectly call accessor
  11. // eslint-disable-next-line es/no-object-defineproperty -- safe
  12. var object = create(Object.defineProperty({}, 'name', { get: function () {
  13. return this === object;
  14. } }));
  15. if (nativeErrorToString.call(object) !== 'true') return true;
  16. }
  17. // FF10- does not properly handle non-strings
  18. return nativeErrorToString.call({ message: 1, name: 2 }) !== '2: 1'
  19. // IE8 does not properly handle defaults
  20. || nativeErrorToString.call({}) !== 'Error';
  21. });
  22. module.exports = INCORRECT_TO_STRING ? function toString() {
  23. var O = anObject(this);
  24. var name = normalizeStringArgument(O.name, 'Error');
  25. var message = normalizeStringArgument(O.message);
  26. return !name ? message : !message ? name : name + ': ' + message;
  27. } : nativeErrorToString;