-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-formatting.ts
More file actions
176 lines (144 loc) · 4.59 KB
/
error-formatting.ts
File metadata and controls
176 lines (144 loc) · 4.59 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
/**
* Error formatting utilities for consistent error messages across CLI commands
* Following M0.5 standards: [ERROR] <command>: <context> - <specific issue> (<suggestion>)
*/
export interface ErrorContext {
command: string;
context: string;
issue: string;
suggestion: string;
filePath?: string;
keyPath?: string;
sectionReference?: string;
}
export interface WarningContext {
command: string;
context: string;
issue: string;
filePath?: string;
}
export interface InfoContext {
command: string;
message: string;
}
/**
* Format a standardized error message
*/
export function formatError(ctx: ErrorContext): string {
let message = `[ERROR] ${ctx.command}: ${ctx.context} - ${ctx.issue} (${ctx.suggestion})`;
if (ctx.filePath) {
message += `\n File: ${ctx.filePath}`;
}
if (ctx.keyPath) {
message += `\n Key path: ${ctx.keyPath}`;
}
if (ctx.sectionReference) {
message += `\n Reference: UMS v2.2 ${ctx.sectionReference}`;
}
return message;
}
/**
* Format a command-specific error with consistent styling
*/
export function formatCommandError(
command: string,
message: string,
filePath?: string
): string {
let formatted = `[ERROR] ${command}: ${message}`;
if (filePath) {
formatted += `\n File: ${filePath}`;
}
return formatted;
}
/**
* Format validation error with enhanced context
*/
export function formatValidationError(
command: string,
filePath: string,
issue: string,
suggestion: string,
keyPath?: string,
sectionRef?: string
): string {
return formatError({
command,
context: `validation failed`,
issue,
suggestion,
filePath,
...(keyPath && { keyPath }),
...(sectionRef && { sectionReference: sectionRef }),
});
}
/**
* Format a standardized warning message
*/
export function formatWarning(ctx: WarningContext): string {
let message = `[WARN] ${ctx.command}: ${ctx.context} - ${ctx.issue} (continuing...)`;
if (ctx.filePath) {
message += `\n File: ${ctx.filePath}`;
}
return message;
}
/**
* Format a standardized info message
*/
export function formatInfo(ctx: InfoContext): string {
return `[INFO] ${ctx.command}: ${ctx.message}`;
}
/**
* Format deprecation warning with enhanced guidance
*/
export function formatDeprecationWarning(
command: string,
moduleId: string,
replacedBy?: string,
filePath?: string
): string {
let message = `[WARN] ${command}: Module '${moduleId}' is deprecated`;
if (replacedBy) {
message += ` and has been replaced by '${replacedBy}'. Please update your persona file to use the replacement module.`;
} else {
message += '. This module may be removed in a future version.';
}
if (filePath) {
message += `\n File: ${filePath}`;
}
return message;
}
/**
* Common error messages for ID validation
*/
export const ID_VALIDATION_ERRORS = {
invalidFormat: (id: string): string =>
`Module ID '${id}' does not match required format. Must be lowercase with optional path segments separated by '/'`,
emptySegment: (id: string): string =>
`Module ID '${id}' contains empty segments (double slashes or leading/trailing slashes)`,
invalidCharacters: (id: string): string =>
`Module ID '${id}' contains invalid characters. Only lowercase letters, numbers, and hyphens are allowed`,
uppercaseCharacters: (id: string): string =>
`Module ID '${id}' contains uppercase characters. All segments must be lowercase`,
invalidModuleName: (moduleName: string): string =>
`Module name '${moduleName}' is invalid. Must start with a letter or number and contain only lowercase letters, numbers, and hyphens`,
};
/**
* Common error messages for schema validation
*/
export const SCHEMA_VALIDATION_ERRORS = {
missingField: (field: string): string =>
`Required field '${field}' is missing`,
wrongType: (field: string, expected: string, actual: string): string =>
`Field '${field}' must be ${expected}, got ${actual}`,
wrongSchemaVersion: (actual: string): string =>
`Schema version must be '2.0', '2.1', or '2.2', got '${actual}'`,
undeclaredDirective: (directive: string, declared: string[]): string =>
`Directive '${directive}' is not declared. Declared directives: ${declared.join(', ')}`,
missingRequiredDirective: (directive: string): string =>
`Required directive '${directive}' is missing from body`,
invalidDirectiveType: (directive: string, expected: string): string =>
`Directive '${directive}' must be ${expected}`,
duplicateModuleId: (id: string, groupName: string): string =>
`Module ID '${id}' appears multiple times in group '${groupName}'. Each ID must be unique within a group.`,
};