@@ -49,112 +49,6 @@ class AbstractError<T = void> extends CustomError {
49
49
public get description ( ) : string {
50
50
return this . constructor [ 'description' ] ;
51
51
}
52
-
53
- /**
54
- * Called automatically by JSON.stringify
55
- * Options can be used to explicitly remove sensitive information
56
- * Not all causes can be stringified
57
- * You must use the replacer to encode the cause property
58
- */
59
-
60
-
61
- // We should use a Proxy, to "proxy the object" or the exception chain
62
- // And then specialise the toJSON, rather than doing this?
63
-
64
- public toJSON (
65
- _key : string = '' ,
66
- options : {
67
- description ?: boolean ;
68
- message ?: boolean ,
69
- timestamp ?: boolean ;
70
- data ?: boolean ;
71
- cause ?: boolean ;
72
- stack ?: boolean ;
73
- } = { }
74
- ) : {
75
- type : string ;
76
- data : {
77
- description ?: string ;
78
- message ?: string ;
79
- timestamp ?: Date ,
80
- data ?: POJO ;
81
- cause ?: T ,
82
- stack ?: string
83
- }
84
- } {
85
- options . description ??= true ;
86
- options . message ??= true ;
87
- options . timestamp ??= true ;
88
- options . data ??= true ;
89
- options . cause ??= true ;
90
- options . stack ??= true ;
91
- const data : POJO = { } ;
92
- if ( options . description ) data . description = this . description ;
93
- if ( options . message ) data . message = this . message ;
94
- if ( options . timestamp ) data . timestamp = this . timestamp ;
95
- if ( options . data ) data . data = this . data ;
96
- if ( options . cause ) {
97
- // Propagate the options down the exception chain
98
- // but only if the cause is another AbstractError
99
- if ( this . cause instanceof AbstractError ) {
100
- data . cause = this . cause . toJSON ( 'cause' , options ) ;
101
- } else {
102
- // Use `replacer` to further encode this object
103
- data . cause = this . cause ;
104
- }
105
- }
106
- if ( options . stack ) data . stack = this . stack ;
107
- return {
108
- type : this . name ,
109
- data
110
- } ;
111
- }
112
-
113
- public static fromJSON ( json : any ) : AbstractError < unknown > | undefined {
114
- if ( typeof json !== 'object' || json == null ) {
115
- return ;
116
- }
117
- if ( ! ( 'type' in json ) || ! ( 'data' in json ) ) {
118
- return ;
119
- }
120
- if ( json . type !== this . name ) {
121
- return ;
122
- }
123
- if ( typeof json . data !== 'object' ) {
124
- return ;
125
- }
126
- if ( json . data . message != null && typeof json . data . message !== 'string' ) {
127
- return ;
128
- }
129
- let timestamp ;
130
- if ( json . data . timestamp != null ) {
131
- const timestampParsed = Date . parse ( json . data . timestamp ) ;
132
- if ( ! isNaN ( timestampParsed ) ) {
133
- timestamp = new Date ( timestampParsed ) ;
134
- } else {
135
- return ;
136
- }
137
- }
138
- if ( json . data . data != null && typeof json . data . data !== 'object' ) {
139
- return ;
140
- }
141
- if ( json . data . stack != null && typeof json . data . stack !== 'string' ) {
142
- return ;
143
- }
144
-
145
- // The cause cannot be reconstructed, unless it's the same as this exception class
146
- // Even then, that would only be the case, if we use toError() again on it
147
- // and no it's not part of the chain of exceptions
148
- // An alternative is class decoration
149
-
150
- const e = new this ( json . data . message , {
151
- data : json . data . data ?? { } ,
152
- timestamp : timestamp ,
153
- cause : json . data . cause ,
154
- } ) ;
155
- e . stack = json . data . stack ;
156
- return e ;
157
- }
158
52
}
159
53
160
54
export default AbstractError ;
0 commit comments