forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.ts
More file actions
182 lines (166 loc) · 5.33 KB
/
Copy pathconfig.ts
File metadata and controls
182 lines (166 loc) · 5.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
import { ZodError } from 'zod';
/**
* Base class for all config-related errors
*/
export abstract class ConfigError extends Error {
protected constructor(message: string) {
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}
/**
* Thrown when a config file doesn't exist
*/
export class ConfigNotFoundError extends ConfigError {
constructor(
public readonly filePath: string,
public readonly fileType: string
) {
super(`${fileType} config file not found at: ${filePath}`);
}
}
/**
* Thrown when a config file can't be read
*/
export class ConfigReadError extends ConfigError {
constructor(
public readonly filePath: string,
public override readonly cause: unknown
) {
const message = cause instanceof Error ? cause.message : String(cause);
super(`Failed to read config file at ${filePath}: ${message}`);
}
}
/**
* Thrown when a config file can't be written
*/
export class ConfigWriteError extends ConfigError {
constructor(
public readonly filePath: string,
public override readonly cause: unknown
) {
const message = cause instanceof Error ? cause.message : String(cause);
super(`Failed to write config file at ${filePath}: ${message}`);
}
}
/**
* Format a Zod path array to bracket notation: agents[0].name
*/
function formatPath(path: PropertyKey[]): string {
if (path.length === 0) return 'root';
return path
.map((segment, i) =>
typeof segment === 'number' ? `[${segment}]` : i === 0 ? String(segment) : `.${String(segment)}`
)
.join('');
}
// Zod issue with extended properties for type-safe access (Zod 4 compatible)
interface ZodIssueExt {
code: string;
path: PropertyKey[];
message: string;
expected?: unknown;
received?: unknown;
options?: unknown[]; // Zod 3
values?: unknown[]; // Zod 4
keys?: string[];
unionErrors?: { issues: ZodIssueExt[] }[]; // Zod 3
errors?: ZodIssueExt[]; // Zod 4
discriminator?: string; // Zod 4 discriminated union
}
/**
* Format a single Zod issue into an actionable message.
* Augments common cases with "got X, expected Y" format.
* Falls back to Zod's message for unhandled cases.
*/
function formatZodIssue(issue: ZodIssueExt): string {
const path = formatPath(issue.path);
switch (issue.code) {
case 'invalid_type':
if (issue.expected !== undefined) {
if (issue.received !== undefined) {
return `${path}: got ${JSON.stringify(issue.received)}, expected ${JSON.stringify(issue.expected)}`;
}
return `${path}: expected ${JSON.stringify(issue.expected)}`;
}
break;
case 'invalid_enum_value':
case 'invalid_value': {
const opts = issue.options ?? issue.values;
if (Array.isArray(opts)) {
const expectedStr = opts.map(o => `"${String(o)}"`).join(' | ');
if (issue.received !== undefined) {
return `${path}: got ${JSON.stringify(issue.received)}, expected ${expectedStr}`;
}
return `${path}: expected ${expectedStr}`;
}
break;
}
case 'invalid_literal':
if (issue.received !== undefined && issue.expected !== undefined) {
return `${path}: got ${JSON.stringify(issue.received)}, expected ${JSON.stringify(issue.expected)}`;
}
break;
case 'unrecognized_keys':
if (Array.isArray(issue.keys)) {
return `${path}: unknown keys (remove): ${issue.keys.map(k => `"${k}"`).join(', ')}`;
}
break;
case 'invalid_union': {
// Zod 4 discriminated union: show discriminator field and valid options
if (issue.discriminator) {
return `${path}: invalid "${issue.discriminator}" value`;
}
// Pick the most actionable error from union failures (one fix, not all branches)
const unionErrors = issue.unionErrors?.flatMap(e => e.issues) ?? issue.errors ?? [];
for (const err of unionErrors) {
if (err.code === 'invalid_enum_value' || err.code === 'invalid_value') {
return formatZodIssue(err);
}
}
for (const err of unionErrors) {
if (err.code === 'invalid_type') {
return formatZodIssue(err);
}
}
const firstError = unionErrors[0];
if (firstError) return formatZodIssue(firstError);
break;
}
case 'invalid_union_discriminator': {
const opts = issue.options ?? issue.values;
if (Array.isArray(opts)) {
return `${path}: expected ${opts.map(o => `"${String(o)}"`).join(' | ')}`;
}
break;
}
}
// Fail open: unhandled cases use Zod's message verbatim
return `${path}: ${issue.message}`;
}
/**
* Thrown when config validation fails
*/
export class ConfigValidationError extends ConfigError {
constructor(
public readonly filePath: string,
public readonly fileType: string,
public readonly zodError: ZodError
) {
const formattedErrors = (zodError.issues as ZodIssueExt[]).map(issue => ` - ${formatZodIssue(issue)}`).join('\n');
super(`${filePath}:\n${formattedErrors}`);
}
}
/**
* Thrown when JSON parsing fails
*/
export class ConfigParseError extends ConfigError {
constructor(
public readonly filePath: string,
public override readonly cause: unknown
) {
const message = cause instanceof Error ? cause.message : String(cause);
super(`Failed to parse JSON in config file at ${filePath}: ${message}`);
}
}