-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
263 lines (206 loc) · 7.33 KB
/
errors.ts
File metadata and controls
263 lines (206 loc) · 7.33 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { ErrorObject } from 'ajv';
import chalk from 'chalk';
import { PluginErrorData } from '@codifycli/schemas';
import { ResourceConfig } from '../entities/resource-config.js';
import { SourceMapCache } from '../parser/source-maps.js';
import { formatAjvErrors } from '../utils/ajv.js';
import { RemoveErrorMethods } from './types.js';
export abstract class CodifyError extends Error {
abstract formattedMessage(): string
}
export class InternalError extends CodifyError {
name = 'InternalError'
formattedMessage(): string {
return `Internal error: ${this.message}`;
}
}
export class AjvValidationError extends CodifyError {
validationErrors: ErrorObject[];
sourceMapKey?: string;
sourceMaps?: SourceMapCache;
constructor(
message: string,
validationErrors: ErrorObject[],
sourceMapKey?: string,
sourceMaps?: SourceMapCache,
) {
super(message);
this.validationErrors = validationErrors;
this.sourceMapKey = sourceMapKey;
this.sourceMaps = sourceMaps;
}
formattedMessage(): string {
let errorMessage = `Validation error: ${this.message}.\n\n`;
errorMessage += formatAjvErrors(this.validationErrors, this.sourceMapKey, this.sourceMaps)
return errorMessage;
}
}
export type PluginValidationErrorParams = Array<{
customErrorMessage?: string,
resource: ResourceConfig,
schemaErrors: ErrorObject[],
}>
export class PluginValidationError extends CodifyError {
resourceErrors: PluginValidationErrorParams
sourceMaps?: SourceMapCache
constructor(
params: PluginValidationErrorParams,
sourceMaps?: SourceMapCache,
) {
super('Validation error: the following parameters are not supported.\n\n');
this.resourceErrors = params
this.sourceMaps = sourceMaps;
}
formattedMessage(): string {
let errorMessage = `${this.message}`;
for (const resourceError of this.resourceErrors) {
const { customErrorMessage, resource, schemaErrors } = resourceError;
errorMessage += `Resource "${resource.id}" has invalid parameters.\n`
errorMessage += formatAjvErrors(schemaErrors, resource.sourceMapKey, this.sourceMaps)
if (customErrorMessage) {
let childMessage = `${schemaErrors.length + 1}. ${customErrorMessage}\n`
if (resource.sourceMapKey && this.sourceMaps) {
childMessage += `${this.sourceMaps.getCodeSnippet(resource.sourceMapKey)}\n`;
}
errorMessage += childMessage.split(/\n/)
.map((l) => ` ${l}`)
.join('\n')
}
}
return errorMessage;
}
}
export class TypeNotFoundError extends CodifyError {
invalidConfigs: ResourceConfig[];
sourceMaps?: SourceMapCache;
constructor(invalidConfigs: ResourceConfig[], sourceMaps?: SourceMapCache) {
super('Validation error: invalid type found. Resource type was not found in any plugins.')
this.invalidConfigs = invalidConfigs;
this.sourceMaps = sourceMaps;
}
formattedMessage(): string {
let errorMessage = `${this.message}\n\n`
for (const invalidConfig of this.invalidConfigs) {
if (!invalidConfig.sourceMapKey || !this.sourceMaps) {
errorMessage += `type ${invalidConfig.type} is not valid.`
continue;
}
const codeSnippet = this.sourceMaps?.getCodeSnippet(SourceMapCache.combineKeys(invalidConfig.sourceMapKey!, 'type'))
errorMessage += `Type "${invalidConfig.type}" is not valid\n${codeSnippet}`
}
return errorMessage;
}
}
export class OperatingSystemNotSupportedError extends CodifyError {
invalidConfigs: ResourceConfig[];
sourceMaps?: SourceMapCache;
constructor(invalidConfigs: ResourceConfig[], sourceMaps?: SourceMapCache) {
super('Validation error: invalid operating system found. Resource type is not supported on this operating system.')
this.invalidConfigs = invalidConfigs;
this.sourceMaps = sourceMaps;
}
formattedMessage(): string {
let errorMessage = `${this.message}\n\n`
for (const invalidConfig of this.invalidConfigs) {
if (!invalidConfig.sourceMapKey || !this.sourceMaps) {
errorMessage += `type ${invalidConfig.type} is not valid.`
continue;
}
const codeSnippet = this.sourceMaps?.getCodeSnippet(SourceMapCache.combineKeys(invalidConfig.sourceMapKey!, 'type'))
errorMessage += `Type "${invalidConfig.type}" is not valid\n${codeSnippet}`
}
return errorMessage;
}
}
export class LinuxDistroNotSupportedError extends CodifyError {
invalidConfigs: ResourceConfig[];
sourceMaps?: SourceMapCache;
constructor(invalidConfigs: ResourceConfig[], sourceMaps?: SourceMapCache) {
super('Validation error: invalid Linux distribution found. Resource type is not supported on this Linux distribution.')
this.invalidConfigs = invalidConfigs;
this.sourceMaps = sourceMaps;
}
formattedMessage(): string {
let errorMessage = `${this.message}\n\n`
for (const invalidConfig of this.invalidConfigs) {
if (!invalidConfig.sourceMapKey || !this.sourceMaps) {
errorMessage += `type ${invalidConfig.type} is not valid.`
continue;
}
const codeSnippet = this.sourceMaps?.getCodeSnippet(SourceMapCache.combineKeys(invalidConfig.sourceMapKey!, 'type'))
errorMessage += `Type "${invalidConfig.type}" is not valid\n${codeSnippet}`
}
return errorMessage;
}
}
export class InvalidResourceError extends Error {
name = 'InvalidResourceError'
message!: string;
fileName!: string;
resourceDefinition!: string;
constructor(props: RemoveErrorMethods<InvalidResourceError>) {
super(props.message)
Object.assign(this, props);
}
}
export class SyntaxError extends CodifyError {
name = 'JsonFileParseError'
fileName!: string;
constructor(props: RemoveErrorMethods<SyntaxError>) {
super(props.message)
Object.assign(this, props);
}
formattedMessage(): string {
return `Syntax error: found in ${this.fileName}: ${this.message}`
}
}
export class UnauthorizedError extends CodifyError {
name = 'UnauthorizedError'
requestName?: string
constructor(props: Omit<RemoveErrorMethods<UnauthorizedError>, 'message'>) {
super(`Unauthorized request to Codify. ${props.requestName ?? ''}`)
Object.assign(this, props);
}
formattedMessage(): string {
return this.message
}
}
export class SpawnError extends CodifyError {
name = 'SpawnError'
command: string;
exitCode: number;
data: string;
constructor(command: string, exitCode: number, data: string) {
super(`Command "${command}" failed with exit code ${exitCode}`)
this.command = command;
this.exitCode = exitCode;
this.data = data;
}
formattedMessage(): string {
return `Spawn error: ${this.message}\n\n${this.data}`
}
}
export class PluginError extends CodifyError {
name = 'PluginError';
pluginName: string;
resourceType: string;
errorData: PluginErrorData;
constructor(pluginName: string, resourceType: string, errorData: PluginErrorData) {
super(errorData.message);
this.pluginName = pluginName;
this.resourceType = resourceType;
this.errorData = errorData;
}
formattedMessage(): string {
return this.message;
}
}
export function prettyPrintError(error: unknown): void {
if (error instanceof CodifyError) {
return console.error(chalk.red(error.formattedMessage()));
}
if (error instanceof Error) {
return console.error(chalk.red(error.message));
}
console.error(chalk.red(String(error)));
}