@@ -6,95 +6,114 @@ var helpers = require("./helpers");
66var setPrototypeOf = helpers . setPrototypeOf ;
77var getPrototypeOf = helpers . getPrototypeOf ;
88var defineProperty = helpers . defineProperty ;
9- var hasOwnProperty = helpers . hasOwnProperty ;
109var objectCreate = helpers . objectCreate ;
1110
1211// Small test for IE6-8, which checks if the environment prints errors "nicely"
13- // If not, a toString() method to be added to the error objects with formatting like in more modern browsers
14- var uglyErrorPrinting = ( new Error ( ) ) . toString ( ) === "[object Error]" ;
12+ // If not, a toString() method to be added to the error objects with formatting
13+ // like in more modern browsers
14+ var uglyErrorPrinting = new Error ( ) . toString ( ) === "[object Error]" ;
1515
1616// For compatibility
1717var extendableErrorName = "" ;
1818
1919function ExtendableError ( message ) {
20- // Get the constructor
21- var originalConstructor = this . constructor ;
22- // Get the constructor name from the non-standard name property. If undefined (on old IEs), it uses the string representation
23- // of the function to extract the name. This should work in all cases, except for directly instantiated ExtendableError objects,
24- // for which the name of the ExtendableError class / function is used
25- var constructorName = originalConstructor . name || ( function ( ) {
26- var constructorNameMatch = originalConstructor . toString ( ) . match ( / ^ f u n c t i o n \s * ( [ ^ \s ( ] + ) / ) ;
27- return constructorNameMatch === null ? ( extendableErrorName ? extendableErrorName : "Error" ) : constructorNameMatch [ 1 ] ;
20+ // Get the constructor
21+ var originalConstructor = this . constructor ;
22+ // Get the constructor name from the non-standard name property. If undefined
23+ // (on old IEs), it uses the string representation of the function to extract
24+ // the name. This should work in all cases, except for directly instantiated
25+ // ExtendableError objects, for which the name of the ExtendableError class /
26+ // function is used
27+ var constructorName =
28+ originalConstructor . name ||
29+ ( function ( ) {
30+ var constructorNameMatch = originalConstructor
31+ . toString ( )
32+ . match ( / ^ f u n c t i o n \s * ( [ ^ \s ( ] + ) / ) ;
33+ return constructorNameMatch === null
34+ ? extendableErrorName
35+ ? extendableErrorName
36+ : "Error"
37+ : constructorNameMatch [ 1 ] ;
2838 } ) ( ) ;
29- // If the constructor name is "Error", ...
30- var constructorNameIsError = constructorName === "Error" ;
31- // change it to the name of the ExtendableError class / function
32- var name = constructorNameIsError ? extendableErrorName : constructorName ;
39+ // If the constructor name is "Error", ...
40+ var constructorNameIsError = constructorName === "Error" ;
41+ // change it to the name of the ExtendableError class / function
42+ var name = constructorNameIsError ? extendableErrorName : constructorName ;
3343
34- // Obtain a new Error instance. This also sets the message property already.
35- var instance = Error . apply ( this , arguments ) ;
44+ // Obtain a new Error instance. This also sets the message property already.
45+ var instance = Error . apply ( this , arguments ) ;
3646
37- // Set the prototype of this to the prototype of instance
38- setPrototypeOf ( instance , getPrototypeOf ( this ) ) ;
47+ // Set the prototype of this to the prototype of instance
48+ setPrototypeOf ( instance , getPrototypeOf ( this ) ) ;
3949
40- // On old IEs, the instance will not extend our subclasses this way. The fix is to use this from the function call instead.
41- if ( ! ( instance instanceof originalConstructor ) || ! ( instance instanceof ExtendableError ) ) {
42- var instance = this ;
43- Error . apply ( this , arguments ) ;
44- defineProperty ( instance , "message" , {
45- configurable : true ,
46- enumerable : false ,
47- value : message ,
48- writable : true
49- } ) ;
50- }
51-
52- // define the name property
53- defineProperty ( instance , "name" , {
54- configurable : true ,
55- enumerable : false ,
56- value : name ,
57- writable : true
50+ // On old IEs, the instance will not extend our subclasses this way. The fix is to use this from the function call instead.
51+ if (
52+ ! ( instance instanceof originalConstructor ) ||
53+ ! ( instance instanceof ExtendableError )
54+ ) {
55+ var instance = this ;
56+ Error . apply ( this , arguments ) ;
57+ defineProperty ( instance , "message" , {
58+ configurable : true ,
59+ enumerable : false ,
60+ value : message ,
61+ writable : true ,
5862 } ) ;
63+ }
64+
65+ // define the name property
66+ defineProperty ( instance , "name" , {
67+ configurable : true ,
68+ enumerable : false ,
69+ value : name ,
70+ writable : true ,
71+ } ) ;
5972
60- // Use Error.captureStackTrace on V8 to capture the proper stack trace excluding any of our error classes
61- if ( Error . captureStackTrace ) {
62- Error . captureStackTrace ( instance , constructorNameIsError ? ExtendableError : originalConstructor ) ;
63- }
64- // instance.stack can still be undefined, in which case the best solution is to create a new Error object and get it from there
65- if ( instance . stack === undefined ) {
66- var err = new Error ( message ) ;
67- err . name = instance . name ;
68- instance . stack = err . stack ;
69- }
73+ // Use Error.captureStackTrace on V8 to capture the proper stack trace excluding any of our error classes
74+ if ( Error . captureStackTrace ) {
75+ Error . captureStackTrace (
76+ instance ,
77+ constructorNameIsError ? ExtendableError : originalConstructor ,
78+ ) ;
79+ }
80+ // instance.stack can still be undefined, in which case the best solution is to create a new Error object and get it from there
81+ if ( instance . stack === undefined ) {
82+ var err = new Error ( message ) ;
83+ err . name = instance . name ;
84+ instance . stack = err . stack ;
85+ }
7086
71- // If the environment does not have a proper string representation (IE), provide an alternative toString()
72- if ( uglyErrorPrinting ) {
73- defineProperty ( instance , "toString" , {
74- configurable : true ,
75- enumerable : false ,
76- value : function toString ( ) {
77- return ( this . name || "Error" ) + ( typeof this . message === "undefined" ? "" : ": " + this . message ) ;
78- } ,
79- writable : true
80- } ) ;
81- }
87+ // If the environment does not have a proper string representation (IE), provide an alternative toString()
88+ if ( uglyErrorPrinting ) {
89+ defineProperty ( instance , "toString" , {
90+ configurable : true ,
91+ enumerable : false ,
92+ value : function toString ( ) {
93+ return (
94+ ( this . name || "Error" ) +
95+ ( typeof this . message === "undefined" ? "" : ": " + this . message )
96+ ) ;
97+ } ,
98+ writable : true ,
99+ } ) ;
100+ }
82101
83- // We're done!
84- return instance ;
102+ // We're done!
103+ return instance ;
85104}
86105
87106// Get the name of the ExtendableError function or use the string literal
88107extendableErrorName = ExtendableError . name || "ExtendableError" ;
89108
90109// Set the prototype of ExtendableError to an Error object
91110ExtendableError . prototype = objectCreate ( Error . prototype , {
92- constructor : {
93- value : Error ,
94- enumerable : false ,
95- writable : true ,
96- configurable : true
97- }
111+ constructor : {
112+ value : Error ,
113+ enumerable : false ,
114+ writable : true ,
115+ configurable : true ,
116+ } ,
98117} ) ;
99118
100119// Export
0 commit comments