UNPKG

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