-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.ts
203 lines (187 loc) · 5.44 KB
/
errors.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { isPrimitiveValue, lowerFirst } from './utils.ts';
import { ErrItem } from './ok-computer.ts';
interface IError<T = unknown> {
// `toPrimitiveError` gives error objects the opportunity to serialize into a primitive value (if possible)
readonly toPrimitiveError: () => T;
}
export const isIError = (val: any): val is IError =>
val != null && typeof val.toPrimitiveError === 'function';
const STRUCTURE = Symbol.for('ok-computer.structure');
export interface IStructure {
readonly [STRUCTURE]: true;
}
export const isIStructure = (val: any): val is IStructure =>
val != null && val[STRUCTURE] === true;
export const asStructure = <T>(val: T): T & IStructure => {
if (val == null) {
throw new Error('Expected object');
}
Object.defineProperty(val, STRUCTURE, {
value: true,
enumerable: false,
configurable: false,
writable: false
});
return val as T & IStructure;
};
interface ILogicalOperator<T> {
readonly type: string;
readonly operator: string;
readonly errors: T[];
}
export class LogicalOperatorError<
// NOTE: `extends any[]` so it can be treated as a tuple if desired
// e.g. new LogicalOperatorError<[number, string, boolean]>('OK', [1, 'two', true])
T extends any[]
> implements ILogicalOperator<T>, IError<ILogicalOperator<T> | string>
{
type: string;
operator: string;
errors: T;
constructor(operator: string, errors: T) {
this.type = `${operator}Error`;
this.operator = operator;
this.errors = errors;
if (this.errors.length < 1) {
throw new Error(`Expected at least 1 error: ${this.operator}`);
}
// TODO: Check `errors` are actually errors w/ `isError`?
}
toPrimitiveError(): string | ILogicalOperator<T> {
// If possible, serialize into string
const primitiveErrors = this.errors.map((err) =>
isIError(err) ? err.toPrimitiveError() : err
);
if (primitiveErrors.every(isPrimitiveValue)) {
const str = primitiveErrors
.map((err, i) =>
err == null
? `${err}`
: i === 0 || typeof err !== 'string'
? err.toString()
: lowerFirst(err)
)
.join(` ${this.operator.toLowerCase()} `);
return primitiveErrors.length > 1 ? `(${str})` : str;
}
// Otherwise just serialize to `ILogicalOperator`
return {
type: this.type,
operator: this.operator,
errors: this.errors
};
}
toJSON() {
return this.toPrimitiveError();
}
toString() {
const error = this.toPrimitiveError();
return typeof error === 'string' ? error : `[object ${this.type}]`;
}
// Undecided if this is a good idea; could make console.log debugging v. confusing
// [Symbol.for('nodejs.util.inspect.custom')]() {
// return this.toPrimitiveError();
// }
}
export class ORError<T extends any[]> extends LogicalOperatorError<T> {
constructor(errors: T) {
super('OR', errors);
}
}
export class ANDError<T extends any[]> extends LogicalOperatorError<T> {
constructor(errors: T) {
super('AND', errors);
}
}
export class XORError<T extends any[]> extends LogicalOperatorError<T> {
constructor(errors: T) {
super('XOR', errors);
}
}
interface IPeerError<T> {
readonly type: string;
readonly key: string;
readonly error: T;
}
export class PeerError<T>
implements IPeerError<T>, IError<IPeerError<T> | string>
{
type: string;
key: string;
error: T;
constructor(key: string, error: T) {
this.type = 'PeerError';
this.key = key;
this.error = error;
}
toPrimitiveError(): string | IPeerError<T> {
// If possible, serialize into string
const primitiveError = isIError(this.error)
? this.error.toPrimitiveError()
: this.error;
// Otherwise just serialize to `IPeerError`
return typeof primitiveError === 'string'
? `Peer "${this.key}" ${lowerFirst(primitiveError)}`
: { type: this.type, key: this.key, error: this.error };
}
toJSON() {
return this.toPrimitiveError();
}
toString() {
const primitive = this.toPrimitiveError();
return typeof primitive === 'string' ? primitive : `[object ${this.type}]`;
}
// [Symbol.for('nodejs.util.inspect.custom')]() {
// return this.toPrimitiveError();
// }
}
interface INegateError<T> {
readonly type: string;
readonly error: T;
}
export class NegateError<T>
implements INegateError<T>, IError<INegateError<T> | string>
{
type: string;
error: T;
constructor(error: T) {
this.type = 'NegateError';
this.error = error;
}
toPrimitiveError(): string | INegateError<T> {
return typeof this.error === 'string'
? `not("${this.error}")`
: { type: this.type, error: this.error };
}
toJSON() {
return this.toPrimitiveError();
}
toString() {
const json = this.toPrimitiveError();
return typeof json === 'string' ? json : `[object ${this.type}]`;
}
// [Symbol.for('nodejs.util.inspect.custom')]() {
// return this.toPrimitiveError();
// }
}
export class AssertError extends Error {
public errors: ErrItem<any>[];
public value?: unknown;
constructor(errors: ErrItem<any>[], value?: unknown) {
super();
try {
const firstError = errors[0];
this.message = `Invalid: first of ${errors.length} errors: ${
firstError.path
}: ${
firstError.err.toString() !== '[object Object]'
? firstError.err
: JSON.stringify(firstError.err)
}`;
} catch (ex) {
this.message = 'Invalid';
}
this.errors = errors;
this.value = value;
}
}