1
1
'use strict' ;
2
2
3
+ /**
4
+ * @typedef {{ [key: string]: any } } Extensions
5
+ * @typedef {Error } Err
6
+ * @property {string } message
7
+ */
8
+
9
+ /**
10
+ *
11
+ * @param {Error } obj
12
+ * @param {Extensions } props
13
+ * @returns {Error & Extensions }
14
+ */
3
15
function assign ( obj , props ) {
4
16
for ( const key in props ) {
5
17
Object . defineProperty ( obj , key , {
@@ -12,6 +24,13 @@ function assign(obj, props) {
12
24
return obj ;
13
25
}
14
26
27
+ /**
28
+ *
29
+ * @param {any } err - An Error
30
+ * @param {string|Extensions } code - A string code or props to set on the error
31
+ * @param {Extensions } [props] - Props to set on the error
32
+ * @returns {Error & Extensions }
33
+ */
15
34
function createError ( err , code , props ) {
16
35
if ( ! err || typeof err === 'string' ) {
17
36
throw new TypeError ( 'Please pass an Error to err-code' ) ;
@@ -23,10 +42,10 @@ function createError(err, code, props) {
23
42
24
43
if ( typeof code === 'object' ) {
25
44
props = code ;
26
- code = undefined ;
45
+ code = '' ;
27
46
}
28
47
29
- if ( code != null ) {
48
+ if ( code ) {
30
49
props . code = code ;
31
50
}
32
51
@@ -40,7 +59,10 @@ function createError(err, code, props) {
40
59
41
60
ErrClass . prototype = Object . create ( Object . getPrototypeOf ( err ) ) ;
42
61
43
- return assign ( new ErrClass ( ) , props ) ;
62
+ // @ts -ignore
63
+ const output = assign ( new ErrClass ( ) , props ) ;
64
+
65
+ return output ;
44
66
}
45
67
}
46
68
0 commit comments