-
Notifications
You must be signed in to change notification settings - Fork 16
/
mod.ts
81 lines (69 loc) · 1.63 KB
/
mod.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
import { Result } from "./index.d.ts";
type V8Error = ErrorConstructor & {
// deno-lint-ignore ban-types
captureStackTrace(err: Error, ssf: Function): void;
};
const canElideFrames = "captureStackTrace" in Error;
export class AssertionError<T> extends Error implements Result {
[key: string]: unknown
get name(): "AssertionError" {
return "AssertionError";
}
get ok() {
return false;
}
constructor(
public message = "Unspecified AssertionError",
props?: T,
// deno-lint-ignore ban-types
ssf?: Function,
) {
super(message);
if (canElideFrames) {
(Error as V8Error).captureStackTrace(
this,
ssf || AssertionError,
);
}
for (const key in props) {
if (!(key in this)) {
// @ts-ignore: allow arbitrary assignment of values onto class
this[key] = props[key];
}
}
}
toJSON(stack?: boolean): Record<string, unknown> {
return {
...this,
name: this.name,
message: this.message,
ok: false,
// include stack if exists and not turned off
stack: stack !== false ? this.stack : undefined,
};
}
}
export class AssertionResult<T> implements Result {
[key: string]: unknown
get name(): "AssertionResult" {
return "AssertionResult";
}
get ok() {
return true;
}
constructor(props?: T) {
for (const key in props) {
if (!(key in this)) {
// @ts-ignore: allow arbitrary assignment of values onto class
this[key] = props[key];
}
}
}
toJSON(): Record<string, unknown> {
return {
...this,
name: this.name,
ok: this.ok,
};
}
}