1
- /**
2
- * Return a function that will copy properties from
3
- * one object to another excluding any originally
4
- * listed. Returned function will create a new `{}`.
5
- *
6
- * @param {String } excluded properties ...
7
- * @return {Function }
8
- */
9
- function exclude ( ...args : string [ ] ) : ( a : { } , b ?: { } ) => Record < string , unknown > {
10
- var excludes = args
11
-
12
- function excludeProps ( res : Record < string , unknown > , obj : Record < string , unknown > ) {
13
- Object . keys ( obj ) . forEach ( function ( key ) {
14
- if ( ! ~ excludes . indexOf ( key ) ) res [ key ] = obj [ key ] ;
15
- } ) ;
16
- }
17
-
18
- return function extendExclude ( ) {
19
- var args = [ ] . slice . call ( arguments )
20
- , i = 0
21
- , res = { } ;
22
-
23
- for ( ; i < args . length ; i ++ ) {
24
- excludeProps ( res , args [ i ] ) ;
25
- }
26
-
27
- return res ;
28
- } ;
29
- } ;
30
-
31
1
export default class AssertionError < T > extends Error {
32
2
name = 'AssertionError'
33
3
showDiff : boolean
34
4
[ key : string ] : any
35
5
36
- constructor ( message : string , _props ?: T , ssf ?: Function ) {
37
- super ( )
38
- var extend = exclude ( 'name' , 'message' , 'stack' , 'constructor' , 'toJSON' )
39
- , props = extend ( _props || { } ) ;
6
+ constructor ( message : string , props ?: T , ssf ?: Function ) {
7
+ super ( message )
40
8
41
9
// default values
42
10
this . message = message || 'Unspecified AssertionError' ;
43
11
this . showDiff = false ;
44
12
45
13
// copy from properties
46
14
for ( var key in props ) {
47
- this [ key ] = props [ key ] ;
15
+ if ( ! ( key in this ) ) {
16
+ // @ts -ignore
17
+ this [ key ] = props [ key ] ;
18
+ }
48
19
}
49
20
50
21
// capture stack trace
51
- ssf = ssf || AssertionError ;
52
22
if ( ( Error as any ) . captureStackTrace ) {
53
- ( Error as any ) . captureStackTrace ( this , ssf ) ;
23
+ ( Error as any ) . captureStackTrace ( this , ssf || AssertionError ) ;
54
24
} else {
55
25
try {
56
26
throw new Error ( ) ;
@@ -60,21 +30,15 @@ export default class AssertionError<T> extends Error {
60
30
}
61
31
}
62
32
63
- /**
64
- * Allow errors to be converted to JSON for static transfer.
65
- *
66
- * @param {Boolean } include stack (default: `true`)
67
- * @return {Object } object that can be `JSON.stringify`
68
- */
69
-
70
- toJSON ( stack : boolean ) {
71
- var extend = exclude ( 'constructor' , 'toJSON' , 'stack' )
72
- , props = extend ( { name : this . name } , this ) ;
33
+ // Allow errors to be converted to JSON for static transfer.
34
+ toJSON ( stack : boolean ) : Record < string , unknown > {
35
+ const { ...props } = this
73
36
74
37
// include stack if exists and not turned off
75
38
if ( false !== stack && this . stack ) {
76
39
props . stack = this . stack ;
77
40
}
41
+ props . message = this . message
78
42
79
43
return props ;
80
44
} ;
0 commit comments