Skip to content

Commit 04bc62a

Browse files
committed
WIP
1 parent 5e99300 commit 04bc62a

File tree

6 files changed

+0
-280
lines changed

6 files changed

+0
-280
lines changed

src/AbstractError.ts

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -49,112 +49,6 @@ class AbstractError<T = void> extends CustomError {
4949
public get description(): string {
5050
return this.constructor['description'];
5151
}
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-
}
15852
}
15953

16054
export default AbstractError;

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export { default as AbstractError } from './AbstractError';
2-
export * from './utils';
32
export * from './types';

src/utils.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

test-errors.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

test-json.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

tests/index.test.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,6 @@ describe('index', () => {
2626
expect(e.cause).toBeInstanceOf(Error);
2727
}
2828
});
29-
test('json', () => {
30-
try {
31-
throw new AbstractError();
32-
} catch (e) {
33-
const eObject = JSON.parse(JSON.stringify(e));
34-
expect(eObject.name).toBe(AbstractError.name);
35-
expect(eObject.description).toBe('');
36-
expect(eObject.data).toStrictEqual({});
37-
expect(eObject.cause).toBeUndefined();
38-
expect(eObject.stack).toBeDefined();
39-
// Timestamp is stringified to ISO8601
40-
expect(e.timestamp.toISOString()).toBe(eObject.timestamp);
41-
expect(new Date(eObject.timestamp).getTime()).toBe(e.timestamp.getTime());
42-
}
43-
});
4429
test('extending', () => {
4530
class ErrorProgram extends AbstractError {
4631
public static description = 'static description';
@@ -70,27 +55,4 @@ describe('index', () => {
7055
expect(e.cause).toBe(eOriginal);
7156
}
7257
});
73-
test('causation chain serialize', () => {
74-
const eOriginal = new AbstractError('original');
75-
try {
76-
try {
77-
throw eOriginal;
78-
} catch (e) {
79-
const e_ = new AbstractError('wrapped', { cause: e as AbstractError });
80-
expect(e_.cause).toBeInstanceOf(AbstractError);
81-
expect(e_.cause).toBe(eOriginal);
82-
expect(e_.cause.message).toBe('original');
83-
throw e_;
84-
}
85-
} catch (e) {
86-
const eObject = JSON.parse(JSON.stringify(e));
87-
expect(eObject.cause.name).toBe(eOriginal.name);
88-
expect(eObject.cause.description).toBe('');
89-
expect(eObject.cause.message).toBe('original');
90-
expect(eObject.message).toBe('wrapped');
91-
expect(eObject.cause.data).toStrictEqual({});
92-
expect(eObject.cause.cause).toBeUndefined();
93-
expect(eObject.cause.stack).toBeDefined();
94-
}
95-
});
9658
});

0 commit comments

Comments
 (0)