@@ -6,7 +6,7 @@ import { CustomError } from 'ts-custom-error';
6
6
* Abstract error
7
7
* Intended for further extension
8
8
*/
9
- class AbstractError < T extends Error | void = void > extends CustomError {
9
+ class AbstractError < T = void > extends CustomError {
10
10
/**
11
11
* Static description of exception
12
12
*/
@@ -56,6 +56,11 @@ class AbstractError<T extends Error | void = void> extends CustomError {
56
56
* Not all causes can be stringified
57
57
* You must use the replacer to encode the cause property
58
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
+
59
64
public toJSON (
60
65
_key : string = '' ,
61
66
options : {
@@ -94,6 +99,7 @@ class AbstractError<T extends Error | void = void> extends CustomError {
94
99
if ( this . cause instanceof AbstractError ) {
95
100
data . cause = this . cause . toJSON ( 'cause' , options ) ;
96
101
} else {
102
+ // Use `replacer` to further encode this object
97
103
data . cause = this . cause ;
98
104
}
99
105
}
@@ -104,7 +110,7 @@ class AbstractError<T extends Error | void = void> extends CustomError {
104
110
} ;
105
111
}
106
112
107
- public static fromJSON ( json : any ) : AbstractError < any > | undefined {
113
+ public static fromJSON ( json : any ) : AbstractError < unknown > | undefined {
108
114
if ( typeof json !== 'object' || json == null ) {
109
115
return ;
110
116
}
@@ -122,8 +128,9 @@ class AbstractError<T extends Error | void = void> extends CustomError {
122
128
}
123
129
let timestamp ;
124
130
if ( json . data . timestamp != null ) {
125
- if ( ! isNaN ( Date . parse ( json . data . timestamp ) ) ) {
126
- timestamp = new Date ( timestamp ) ;
131
+ const timestampParsed = Date . parse ( json . data . timestamp ) ;
132
+ if ( ! isNaN ( timestampParsed ) ) {
133
+ timestamp = new Date ( timestampParsed ) ;
127
134
} else {
128
135
return ;
129
136
}
@@ -134,24 +141,16 @@ class AbstractError<T extends Error | void = void> extends CustomError {
134
141
if ( json . data . stack != null && typeof json . data . stack !== 'string' ) {
135
142
return ;
136
143
}
137
- let cause ;
138
- if ( json . data . cause != null ) {
139
- // THIS is a problem
140
- // we don't know what the cause may be
141
- // We don't know how to reconstruct it
142
- // It is neither Error, nor AbstractError
143
- // Do we check up the chain?
144
- // The problem is that we can't really decode this if we don't know what the chain might be
145
- // If e as an instance can be encoded with arbitrary error objects
146
- // Then during decoding, how would we know how to reconstruct it?
147
- // super.name
148
144
149
- cause = this . fromJSON ( json . data . cause ) ;
150
- }
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
+
151
150
const e = new this ( json . data . message , {
152
151
data : json . data . data ?? { } ,
153
152
timestamp : timestamp ,
154
- cause : cause ,
153
+ cause : json . data . cause ,
155
154
} ) ;
156
155
e . stack = json . data . stack ;
157
156
return e ;
0 commit comments