-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraceNode.ts
118 lines (95 loc) · 2.78 KB
/
traceNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { ErrorWrapper } from './checkpoint';
import type { ResultWrapper, ArgsWrapper } from './checkpoint';
import { Checkpoint } from './checkpoint';
import type { TraceEntry } from './traceEntry';
export class TraceNode
implements IPublicActionTraceNode, IPublicStoreTraceNode
{
constructor(
public expectedAmountOfChildren: number | null = null,
parentOfNewTraceNode: TraceNode | null,
) {
if (parentOfNewTraceNode) {
this.parent = parentOfNewTraceNode;
parentOfNewTraceNode.connectChild(this);
}
}
readonly parent: TraceNode | null = null;
readonly traces: TraceEntry[] = [];
private connectChild(child: TraceNode): void {
this.traces.push(child);
}
start(args: ArgsWrapper<any[]>): void {
this.traces.push(new Checkpoint('executionStart', null, args));
}
saveLog(
description: string,
serializableToJsonContextPayload: Record<string, any>,
): void {
this.traces.push(
new Checkpoint('log', description, serializableToJsonContextPayload),
);
}
saveError(error: Error): void {
this.#saveError(false, error);
}
saveMaybeError(error: unknown): void {
this.#saveMaybeError(false, error);
}
returnResolution(result: ResultWrapper<any>): void {
this.traces.push(new Checkpoint('wasResolvedWithReturn', null, result));
}
errorResolution(error: Error): void {
this.#saveError(true, error);
}
maybeErrorResolution(error: unknown): void {
this.#saveMaybeError(true, error);
}
finish(): void {
this.traces.push(new Checkpoint('executionFinish', null, null));
}
#saveMaybeError(isResolution: boolean, error: unknown): void {
if (error instanceof Error) {
this.#saveError(isResolution, error);
} else {
this.traces.push(
new Checkpoint(
isResolution
? 'wasResolvedWithErrorWithUnknownStructure'
: 'errorWithUnknownStructure',
NOT_AN_ERROR_HAS_BEEN_THROWN_DESCRIPTION,
new ErrorWrapper(error),
),
);
}
}
#saveError(isResolution: boolean, error: Error): void {
this.traces.push(
new Checkpoint(
isResolution ? 'wasResolvedWithError' : 'error',
null,
error,
),
);
}
toJSON(): { traces: TraceEntry[] } {
return {
traces: this.traces,
};
}
}
export interface IPublicStoreTraceNode {
readonly parent: TraceNode | null;
readonly traces: TraceEntry[];
expectedAmountOfChildren: number | null;
}
export interface IPublicActionTraceNode {
saveLog(
description: string,
serializableToJsonContextPayload: Record<string, any>,
): void;
saveError(error: Error): void;
saveMaybeError(error: unknown): void;
}
export const NOT_AN_ERROR_HAS_BEEN_THROWN_DESCRIPTION =
'Captured error which is not an instance of Error class';