-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy patherrors.ts
More file actions
212 lines (187 loc) · 5.53 KB
/
Copy patherrors.ts
File metadata and controls
212 lines (187 loc) · 5.53 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
/**
* Typed error hierarchy for the Codex plugin.
* Provides structured error types with codes, causes, and context.
*/
/**
* Error codes for categorizing errors.
*/
export const ErrorCode = {
NETWORK_ERROR: "CODEX_NETWORK_ERROR",
API_ERROR: "CODEX_API_ERROR",
AUTH_ERROR: "CODEX_AUTH_ERROR",
VALIDATION_ERROR: "CODEX_VALIDATION_ERROR",
RATE_LIMIT: "CODEX_RATE_LIMIT",
TIMEOUT: "CODEX_TIMEOUT",
CODEX_UNAVAILABLE: "CODEX_UNAVAILABLE",
} as const;
/**
* Options for creating a CodexError.
*/
export interface CodexErrorOptions {
code?: string;
cause?: unknown;
context?: Record<string, unknown>;
}
/**
* Base error class for all Codex plugin errors.
* Supports error chaining via `cause` and arbitrary context data.
*/
export class CodexError extends Error {
override readonly name: string = "CodexError";
readonly code: string;
readonly context?: Record<string, unknown>;
constructor(message: string, options?: CodexErrorOptions) {
super(message, { cause: options?.cause });
this.code = options?.code ?? ErrorCode.API_ERROR;
this.context = options?.context;
// istanbul ignore next -- Error.captureStackTrace always exists in Node.js
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
}
/**
* Options for creating a CodexApiError.
*/
export interface CodexApiErrorOptions extends CodexErrorOptions {
status: number;
headers?: Record<string, string>;
}
/**
* Error for HTTP/API response errors.
*/
export class CodexApiError extends CodexError {
override readonly name = "CodexApiError";
readonly status: number;
readonly headers?: Record<string, string>;
constructor(message: string, options: CodexApiErrorOptions) {
super(message, { ...options, code: options.code ?? ErrorCode.API_ERROR });
this.status = options.status;
this.headers = options.headers;
}
}
/**
* Options for creating a CodexAuthError.
*/
export interface CodexAuthErrorOptions extends CodexErrorOptions {
accountId?: string;
retryable?: boolean;
}
/**
* Error for authentication failures.
*/
export class CodexAuthError extends CodexError {
override readonly name = "CodexAuthError";
readonly accountId?: string;
readonly retryable: boolean;
constructor(message: string, options?: CodexAuthErrorOptions) {
super(message, { ...options, code: options?.code ?? ErrorCode.AUTH_ERROR });
this.accountId = options?.accountId;
this.retryable = options?.retryable ?? false;
}
}
/**
* Options for creating a CodexNetworkError.
*/
export interface CodexNetworkErrorOptions extends CodexErrorOptions {
retryable?: boolean;
}
/**
* Error for network/connection failures.
*/
export class CodexNetworkError extends CodexError {
override readonly name = "CodexNetworkError";
readonly retryable: boolean;
constructor(message: string, options?: CodexNetworkErrorOptions) {
super(message, {
...options,
code: options?.code ?? ErrorCode.NETWORK_ERROR,
});
this.retryable = options?.retryable ?? true;
}
}
/**
* Options for creating a CodexValidationError.
*/
export interface CodexValidationErrorOptions extends CodexErrorOptions {
field?: string;
expected?: string;
}
/**
* Error for input validation failures.
*/
export class CodexValidationError extends CodexError {
override readonly name = "CodexValidationError";
readonly field?: string;
readonly expected?: string;
constructor(message: string, options?: CodexValidationErrorOptions) {
super(message, {
...options,
code: options?.code ?? ErrorCode.VALIDATION_ERROR,
});
this.field = options?.field;
this.expected = options?.expected;
}
}
/**
* Options for creating a CodexRateLimitError.
*/
export interface CodexRateLimitErrorOptions extends CodexErrorOptions {
retryAfterMs?: number;
accountId?: string;
}
/**
* Error for rate limit exceeded.
*/
export class CodexRateLimitError extends CodexError {
override readonly name = "CodexRateLimitError";
readonly retryAfterMs?: number;
readonly accountId?: string;
constructor(message: string, options?: CodexRateLimitErrorOptions) {
super(message, { ...options, code: options?.code ?? ErrorCode.RATE_LIMIT });
this.retryAfterMs = options?.retryAfterMs;
this.accountId = options?.accountId;
}
}
/**
* Storage-specific error with a filesystem code, target path, and user-facing hint.
*/
export class StorageError extends CodexError {
override readonly name = "StorageError";
readonly path: string;
readonly hint: string;
constructor(
message: string,
code: string,
path: string,
hint: string,
cause?: Error,
) {
super(message, { code, cause });
this.path = path;
this.hint = hint;
}
}
/**
* Raised when every probe model is rejected because the account/workspace has no
* Codex entitlement (e.g. the API answers `model is not supported when using
* Codex with a ChatGPT account` for all candidates). The account is otherwise
* signed in and working; callers should surface this as a warning, not a failure.
*/
export class CodexUnavailableError extends CodexError {
override readonly name = "CodexUnavailableError";
constructor(message: string, options?: CodexErrorOptions) {
super(message, { ...options, code: options?.code ?? ErrorCode.CODEX_UNAVAILABLE });
}
}
/**
* Type guard for `CodexUnavailableError` that survives cross-realm/duplicate-module
* boundaries by also matching on the structural `code` marker.
*/
export function isCodexUnavailableError(error: unknown): error is CodexUnavailableError {
if (error instanceof CodexUnavailableError) return true;
return (
error instanceof Error &&
(error as { code?: unknown }).code === ErrorCode.CODEX_UNAVAILABLE
);
}