-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherrors.ts
More file actions
245 lines (212 loc) · 7.4 KB
/
errors.ts
File metadata and controls
245 lines (212 loc) · 7.4 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import type { Class } from '@matrixai/errors';
import { AbstractError } from '@matrixai/errors';
import sysexits from 'polykey/utils/sysexits.js';
/**
* Root error for Polykey CLI.
* Application errors may need to be serialised over program boundaries.
* For example presenting errors on the terminal, or sending errors over
* the network.
*/
class ErrorPolykeyCLI<T> extends AbstractError<T> {
static description: string = 'Polykey CLI error';
exitCode: number = sysexits.GENERAL;
public static fromJSON<T extends Class<any>>(
this: T,
json: any,
): InstanceType<T> {
if (
typeof json !== 'object' ||
json.type !== this.name ||
typeof json.data !== 'object' ||
typeof json.data.message !== 'string' ||
isNaN(Date.parse(json.data.timestamp)) ||
typeof json.data.description !== 'string' ||
typeof json.data.data !== 'object' ||
typeof json.data.exitCode !== 'number' ||
('stack' in json.data && typeof json.data.stack !== 'string')
) {
throw new TypeError(`Cannot decode JSON to ${this.name}`);
}
const e = new this(json.data.message, {
timestamp: new Date(json.data.timestamp),
data: json.data.data,
cause: json.data.cause,
});
e.exitCode = json.data.exitCode;
e.stack = json.data.stack;
return e;
}
public toJSON(): any {
const json = super.toJSON();
json.data.description = this.description;
json.data.exitCode = this.exitCode;
return json;
}
}
/**
* Uncaught exceptions is a logic error.
* If these exceptions occur, there is a bug.
*/
class ErrorPolykeyCLIUncaughtException<T> extends ErrorPolykeyCLI<T> {
static description = 'Uncaught exception';
exitCode = sysexits.SOFTWARE;
}
/**
* Unhandled rejections is a logic error.
* If these exceptions occur, there is a bug.
*/
class ErrorPolykeyCLIUnhandledRejection<T> extends ErrorPolykeyCLI<T> {
static description = 'Unhandled rejection';
exitCode = sysexits.SOFTWARE;
}
/**
* Asynchronous deadlocks is a logic error.
* If these exceptions occur, there is a bug.
*/
class ErrorPolykeyCLIAsynchronousDeadlock<T> extends ErrorPolykeyCLI<T> {
static description =
'Process exited unexpectedly, likely due to promise deadlock';
exitCode = sysexits.SOFTWARE;
}
/**
* Unexpected error is a runtime error.
* If these exceptions occur, there is a bug. Most likely in Polykey.
*/
class ErrorPolykeyCLIUnexpectedError<T> extends ErrorPolykeyCLI<T> {
static description = 'An unexpected error occured';
exitCode = sysexits.SOFTWARE;
}
class ErrorPolykeyCLIChildProcessFailure<T> extends ErrorPolykeyCLI<T> {
static description = 'A child process failed to exit gracefully';
exitCode = sysexits.UNKNOWN;
}
class ErrorPolykeyCLINodePath<T> extends ErrorPolykeyCLI<T> {
static description = 'Cannot derive default node path from unknown platform';
exitCode = sysexits.USAGE;
}
class ErrorPolykeyCLIClientOptions<T> extends ErrorPolykeyCLI<T> {
static description = 'Missing required client options';
exitCode = sysexits.USAGE;
}
class ErrorPolykeyCLIPasswordWrong<T> extends ErrorPolykeyCLI<T> {
static description = 'Wrong password, please try again';
exitCode = sysexits.USAGE;
}
class ErrorPolykeyCLIPasswordMissing<T> extends ErrorPolykeyCLI<T> {
static description =
'Password is necessary, provide it via --password-file, PK_PASSWORD or when prompted';
exitCode = sysexits.USAGE;
}
class ErrorPolykeyCLIPasswordFileRead<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to read password file';
exitCode = sysexits.NOINPUT;
}
class ErrorPolykeyCLIRecoveryCodeFileRead<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to read recovery code file';
exitCode = sysexits.NOINPUT;
}
class ErrorPolykeyCLIPrivateKeyFileRead<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to read private key Pem file';
exitCode = sysexits.NOINPUT;
}
class ErrorPolykeyCLIPublicJWKFileRead<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to read public JWK file';
exitCode = sysexits.NOINPUT;
}
class ErrorPolykeyCLIFileRead<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to read file';
exitCode = sysexits.NOINPUT;
}
class ErrorPolykeyCLIAgentStatus<T> extends ErrorPolykeyCLI<T> {
static description = 'PolykeyAgent agent status';
exitCode = sysexits.TEMPFAIL;
}
class ErrorPolykeyCLIAgentProcess<T> extends ErrorPolykeyCLI<T> {
static description = 'PolykeyAgent process could not be started';
exitCode = sysexits.OSERR;
}
class ErrorPolykeyCLINodeFindFailed<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to find the node in the DHT';
exitCode = 1;
}
class ErrorPolykeyCLINodePingFailed<T> extends ErrorPolykeyCLI<T> {
static description = 'Node was not online or not found.';
exitCode = 1;
}
class ErrorPolykeyCLIInvalidEnvName<T> extends ErrorPolykeyCLI<T> {
static description =
'Secret retrieved has an invalid environment variable name';
exitCode = sysexits.USAGE;
}
class ErrorPolykeyCLIDuplicateEnvName<T> extends ErrorPolykeyCLI<T> {
static description = 'Environment variable name already retrieved';
exitCode = sysexits.USAGE;
}
class ErrorPolykeyCLIMissingRequiredEnvName<T> extends ErrorPolykeyCLI<T> {
static description = 'A required environment variable is not present';
exitCode = sysexits.USAGE;
}
class ErrorPolykeyCLIMakeDirectory<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to create one or more directories';
exitCode = 1;
}
class ErrorPolykeyCLIRenameSecret<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to rename one or more secrets';
exitCode = 1;
}
class ErrorPolykeyCLIRemoveSecret<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to remove one or more secrets';
exitCode = 1;
}
class ErrorPolykeyCLICatSecret<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to concatenate one or more secrets';
exitCode = 1;
}
class ErrorPolykeyCLIEditSecret<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to edit a secret';
exitCode = 1;
}
class ErrorPolykeyCLITouchSecret<T> extends ErrorPolykeyCLI<T> {
static description = 'Failed to touch one or more secret';
exitCode = 1;
}
class ErrorPolykeyCLIInvalidJWT<T> extends ErrorPolykeyCLI<T> {
static description = 'JWT is not valid';
exitCode = sysexits.USAGE;
}
class ErrorPolykeyCLISchemaInvalid<T> extends ErrorPolykeyCLI<T> {
static description = 'The provided JSON schema is invalid';
exitCode = sysexits.CONFIG;
}
export {
ErrorPolykeyCLI,
ErrorPolykeyCLIUncaughtException,
ErrorPolykeyCLIUnhandledRejection,
ErrorPolykeyCLIUnexpectedError,
ErrorPolykeyCLIChildProcessFailure,
ErrorPolykeyCLIAsynchronousDeadlock,
ErrorPolykeyCLINodePath,
ErrorPolykeyCLIClientOptions,
ErrorPolykeyCLIPasswordWrong,
ErrorPolykeyCLIPasswordMissing,
ErrorPolykeyCLIPasswordFileRead,
ErrorPolykeyCLIRecoveryCodeFileRead,
ErrorPolykeyCLIPrivateKeyFileRead,
ErrorPolykeyCLIPublicJWKFileRead,
ErrorPolykeyCLIFileRead,
ErrorPolykeyCLIAgentStatus,
ErrorPolykeyCLIAgentProcess,
ErrorPolykeyCLINodeFindFailed,
ErrorPolykeyCLINodePingFailed,
ErrorPolykeyCLIInvalidEnvName,
ErrorPolykeyCLIDuplicateEnvName,
ErrorPolykeyCLIMissingRequiredEnvName,
ErrorPolykeyCLIMakeDirectory,
ErrorPolykeyCLIRenameSecret,
ErrorPolykeyCLIRemoveSecret,
ErrorPolykeyCLICatSecret,
ErrorPolykeyCLIEditSecret,
ErrorPolykeyCLITouchSecret,
ErrorPolykeyCLIInvalidJWT,
ErrorPolykeyCLISchemaInvalid,
};