-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkernel-error.ts
More file actions
66 lines (60 loc) · 1.96 KB
/
kernel-error.ts
File metadata and controls
66 lines (60 loc) · 1.96 KB
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
/**
* Error codes for expected kernel errors that vat code may handle gracefully.
*/
export type ExpectedKernelErrorCode =
| 'OBJECT_REVOKED'
| 'OBJECT_DELETED'
| 'BAD_PROMISE_RESOLUTION'
| 'ENDPOINT_UNREACHABLE'
| 'CONNECTION_LOST'
| 'PEER_RESTARTED'
| 'VAT_TERMINATED'
| 'DELIVERY_FAILED';
/**
* Error codes for fatal kernel errors (kernel bugs or illegal operations).
* These are prefixed with `VAT_FATAL:` in the error message.
*/
export type FatalKernelErrorCode = 'ILLEGAL_SYSCALL' | 'INTERNAL_ERROR';
/**
* All kernel error codes.
*/
export type KernelErrorCode = ExpectedKernelErrorCode | FatalKernelErrorCode;
/**
* Pattern matching kernel error messages.
* Matches both `[KERNEL:<CODE>]` and `[KERNEL:VAT_FATAL:<CODE>]`.
*/
export const KERNEL_ERROR_PATTERN = /^\[KERNEL:(?:(VAT_FATAL):)?([A-Z_]+)\]/u;
/**
* Check whether a value is a kernel error (an Error whose message starts with
* `[KERNEL:...]`).
*
* @param value - The value to check.
* @returns `true` if `value` is an Error with a kernel error message.
*/
export function isKernelError(value: unknown): value is Error {
return value instanceof Error && KERNEL_ERROR_PATTERN.test(value.message);
}
/**
* Extract the kernel error code from an Error, if present.
*
* @param error - The error to inspect.
* @returns The kernel error code, or `undefined` if the error is not a kernel error.
*/
export function getKernelErrorCode(error: Error): KernelErrorCode | undefined {
const match = KERNEL_ERROR_PATTERN.exec(error.message);
if (!match) {
return undefined;
}
return match[2] as KernelErrorCode;
}
/**
* Check whether an Error is a fatal kernel error (its message contains the
* `VAT_FATAL:` infix).
*
* @param error - The error to inspect.
* @returns `true` if the error is a fatal kernel error.
*/
export function isFatalKernelError(error: Error): boolean {
const match = KERNEL_ERROR_PATTERN.exec(error.message);
return match !== null && match[1] === 'VAT_FATAL';
}