-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
155 lines (122 loc) · 4.22 KB
/
errors.ts
File metadata and controls
155 lines (122 loc) · 4.22 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
import { ErrorObject } from 'ajv';
import chalk from 'chalk';
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 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 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)));
}