error-to-string.js 1.2 KB

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