Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

errors.ts

453 lines12.9 KB

1import * as Schema from "effect/Schema";
2import * as SchemaAST from "effect/SchemaAST";
3import type * as SchemaIssue from "effect/SchemaIssue";
4​
5import * as AcpSchema from "./_generated/schema.gen.ts";
6​
7const AcpRequestOperation = Schema.Literals([
8 "decode-extension-request-payload",
9 "encode-extension-response",
10 "handle-request",
11 "handle-extension-request",
12 "receive-response",
13 "receive-streaming-response",
14]);
15type AcpRequestOperation = typeof AcpRequestOperation.Type;
16​
17export const AcpRequestId = Schema.Union([Schema.String, Schema.Number]);
18export type AcpRequestId = typeof AcpRequestId.Type;
19​
20const AcpSchemaIssueKind = Schema.Literals([
21 "Filter",
22 "Encoding",
23 "Pointer",
24 "Composite",
25 "AnyOf",
26 "InvalidType",
27 "InvalidValue",
28 "MissingKey",
29 "UnexpectedKey",
30 "Forbidden",
31 "OneOf",
32]);
33type AcpSchemaIssueKind = typeof AcpSchemaIssueKind.Type;
34​
35interface AcpSchemaIssueDiagnostics {
36 readonly issueCount: number;
37 readonly issueKinds: ReadonlyArray<AcpSchemaIssueKind>;
38 readonly maximumPathDepth: number;
40​
41const MAX_SUMMARY_ISSUES = 3;
42const MAX_SUMMARY_PATH_SEGMENTS = 8;
43​
44const summaryPathSegment = (segment: PropertyKey): string =>
45 typeof segment === "number" || /^[A-Za-z_][A-Za-z0-9_]{0,63}$/.test(String(segment))
46 ? String(segment)
47 : "*";
48​
49const summaryPath = (path: ReadonlyArray<PropertyKey>): string => {
50 if (path.length === 0) return "payload";
51 const shown = path.slice(0, MAX_SUMMARY_PATH_SEGMENTS).map(summaryPathSegment).join(".");
52 return path.length > MAX_SUMMARY_PATH_SEGMENTS ? `${shown}…` : shown;
53};
54​
55const summaryReason = (issue: SchemaIssue.Issue): string => {
56 switch (issue._tag) {
57 case "MissingKey":
58 return "missing";
59 case "UnexpectedKey":
60 return "unexpected";
61 case "InvalidType":
62 return "wrong type";
63 default:
64 return "invalid";
65 }
66};
67​
68const isCatchAllMember = (issue: SchemaIssue.Issue) =>
69 "ast" in issue && SchemaAST.resolveTitle(issue.ast) === "other";
70​
71/**
72 * ACP unions end in a member titled "other" that admits custom and future
73 * variants; it fails only because the discriminator names a known variant, so
74 * the known variant's issues are the ones reported.
75 */
76const schemaIssueSummary = (root: SchemaIssue.Issue): string => {
77 const entries = new Set<string>();
78 const visit = (issue: SchemaIssue.Issue, path: ReadonlyArray<PropertyKey>): void => {
79 switch (issue._tag) {
80 case "Filter":
81 case "Encoding":
82 visit(issue.issue, path);
83 return;
84 case "Pointer":
85 visit(issue.issue, [...path, ...issue.path]);
86 return;
87 case "Composite":
88 case "AnyOf": {
89 const known = issue.issues.filter((child) => !isCatchAllMember(child));
90 const children = known.length > 0 ? known : issue.issues;
91 if (children.length === 0) entries.add(`${summaryPath(path)} ${summaryReason(issue)}`);
92 for (const child of children) visit(child, path);
93 return;
94 }
95 default:
96 entries.add(`${summaryPath(path)} ${summaryReason(issue)}`);
97 }
98 };
99 visit(root, []);
100 const shown = [...entries].slice(0, MAX_SUMMARY_ISSUES);
101 const hidden = entries.size - shown.length;
102 return hidden > 0 ? `${shown.join(", ")} (+${hidden} more)` : shown.join(", ");
103};
104​
105const schemaIssueDiagnostics = (root: SchemaIssue.Issue): AcpSchemaIssueDiagnostics => {
106 let issueCount = 0;
107 let maximumPathDepth = 0;
108 const issueKinds = new Set<AcpSchemaIssueKind>();
109​
110 const visit = (issue: SchemaIssue.Issue, pathDepth: number): void => {
111 issueCount += 1;
112 issueKinds.add(issue._tag);
113 maximumPathDepth = Math.max(maximumPathDepth, pathDepth);
114 switch (issue._tag) {
115 case "Filter":
116 case "Encoding":
117 visit(issue.issue, pathDepth);
118 break;
119 case "Pointer":
120 visit(issue.issue, pathDepth + issue.path.length);
121 break;
122 case "Composite":
123 case "AnyOf":
124 for (const child of issue.issues) visit(child, pathDepth);
125 break;
126 }
127 };
128​
129 visit(root, 0);
130 return {
131 issueCount,
132 issueKinds: [...issueKinds],
133 maximumPathDepth,
134 };
135};
136​
137export interface AcpRequestDiagnostics {
138 readonly method?: string;
139 readonly requestId?: AcpRequestId;
140 readonly operation?: AcpRequestOperation;
141 readonly cause?: unknown;
142 readonly issueCount?: number;
143 readonly issueKinds?: ReadonlyArray<AcpSchemaIssueKind>;
144 readonly maximumPathDepth?: number;
146​
147export class AcpSpawnError extends Schema.TaggedError<AcpSpawnError>()("AcpSpawnError", {
148 command: Schema.optional(Schema.String),
149 cause: Schema.Defect(),
150}) {
151 override get message() {
152 return this.command
153 ? `Failed to spawn ACP process for command: ${this.command}`
154 : "Failed to spawn ACP process";
155 }
157​
158export class AcpProcessExitedError extends Schema.TaggedError<AcpProcessExitedError>()(
159 "AcpProcessExitedError",
160 {
161 code: Schema.optional(Schema.Number),
162 signal: Schema.optionalKey(Schema.String),
163 pid: Schema.optionalKey(Schema.Int),
164 stderr: Schema.optionalKey(Schema.String),
165 cause: Schema.optional(Schema.Defect()),
166 },
167) {
168 override get message() {
169 const base =
170 this.signal !== undefined
171 ? `ACP process exited after signal ${this.signal}`
172 : this.code === undefined
173 ? "ACP process exited"
174 : `ACP process exited with code ${this.code}`;
175 const excerpt = this.stderr?.trim();
176 return excerpt && excerpt.length > 0 ? `${base}\n${excerpt}` : base;
177 }
179​
180export const AcpProtocolParseOperation = Schema.Literals([
181 "encode-message",
182 "decode-wire-message",
183 "decode-notification-payload",
184]);
185export type AcpProtocolParseOperation = typeof AcpProtocolParseOperation.Type;
186​
187export class AcpProtocolParseError extends Schema.TaggedError<AcpProtocolParseError>()(
188 "AcpProtocolParseError",
189 {
190 operation: AcpProtocolParseOperation,
191 method: Schema.optionalKey(Schema.String),
192 requestId: Schema.optionalKey(AcpRequestId),
193 issueCount: Schema.optionalKey(Schema.Number),
194 issueKinds: Schema.optionalKey(Schema.Array(AcpSchemaIssueKind)),
195 maximumPathDepth: Schema.optionalKey(Schema.Number),
196 issueSummary: Schema.optionalKey(Schema.String),
197 cause: Schema.Defect(),
198 },
199) {
200 override get message() {
201 const method = this.method === undefined ? "" : ` for method '${this.method}'`;
202 const summary = this.issueSummary === undefined ? "" : `: ${this.issueSummary}`;
203 return `ACP protocol operation '${this.operation}' failed${method}${summary}.`;
204 }
205​
206 static fromSchemaError(
207 operation: AcpProtocolParseOperation,
208 method: string,
209 cause: Schema.SchemaError,
210 ) {
211 return new AcpProtocolParseError({
212 operation,
213 method,
214 ...schemaIssueDiagnostics(cause.issue),
215 issueSummary: schemaIssueSummary(cause.issue),
216 cause,
217 });
218 }
219​
220 static fromEncodingError(
221 method: string | undefined,
222 requestId: AcpRequestId | undefined,
223 cause: unknown,
224 ) {
225 return new AcpProtocolParseError({
226 operation: "encode-message",
227 ...(method === undefined ? {} : { method }),
228 ...(requestId === undefined ? {} : { requestId }),
229 cause,
230 });
231 }
233​
234export class AcpTransportError extends Schema.TaggedError<AcpTransportError>()(
235 "AcpTransportError",
236 {
237 operation: Schema.optional(
238 Schema.Literals(["call-rpc", "read-input-stream", "read-process-exit-status"]),
239 ),
240 method: Schema.optional(Schema.String),
241 detail: Schema.optional(Schema.String),
242 pid: Schema.optionalKey(Schema.Int),
243 cause: Schema.Defect(),
244 },
245) {
246 override get message() {
247 const method = this.method ? ` for method ${this.method}` : "";
248 return this.operation
249 ? `ACP transport operation ${this.operation} failed${method}.`
250 : "ACP transport operation failed.";
251 }
253​
254export class AcpInputStreamEndedError extends Schema.TaggedError<AcpInputStreamEndedError>()(
255 "AcpInputStreamEndedError",
256 {},
257) {
258 override get message() {
259 return "ACP input stream ended.";
260 }
262​
263export class AcpRequestError extends Schema.TaggedError<AcpRequestError>()("AcpRequestError", {
264 code: AcpSchema.ErrorCode,
265 errorMessage: Schema.String,
266 data: Schema.optional(Schema.Unknown),
267 method: Schema.optionalKey(Schema.String),
268 requestId: Schema.optionalKey(AcpRequestId),
269 operation: Schema.optionalKey(AcpRequestOperation),
270 issueCount: Schema.optionalKey(Schema.Number),
271 issueKinds: Schema.optionalKey(Schema.Array(AcpSchemaIssueKind)),
272 maximumPathDepth: Schema.optionalKey(Schema.Number),
273 cause: Schema.optionalKey(Schema.Defect()),
274}) {
275 override get message() {
276 return this.errorMessage;
277 }
278​
279 static fromProtocolError(
280 error: AcpSchema.Error,
281 context: {
282 readonly method: string;
283 readonly requestId?: AcpRequestId;
284 readonly cause?: unknown;
285 },
286 ) {
287 return new AcpRequestError({
288 code: error.code,
289 errorMessage: error.message,
290 ...(error.data !== undefined ? { data: error.data } : {}),
291 method: context.method,
292 ...(context.requestId === undefined ? {} : { requestId: context.requestId }),
293 operation: "receive-response",
294 cause: context.cause ?? error,
295 });
296 }
297​
298 static fromExtensionResponseFailure(method: string, requestId: AcpRequestId, cause: unknown) {
299 return AcpRequestError.internalError("Extension request failed", undefined, {
300 method,
301 requestId,
302 operation: "receive-response",
303 cause,
304 });
305 }
306​
307 static fromExtensionResponseEncodingError(
308 method: string,
309 requestId: AcpRequestId,
310 cause: AcpProtocolParseError,
311 ) {
312 return AcpRequestError.internalError("Internal error", undefined, {
313 method,
314 requestId,
315 operation: "encode-extension-response",
316 cause,
317 });
318 }
319​
320 static unsupportedStreamingResponse(method: string, requestId: AcpRequestId) {
321 return AcpRequestError.internalError(
322 "Streaming extension responses are not supported",
323 undefined,
324 {
325 method,
326 requestId,
327 operation: "receive-streaming-response",
328 },
329 );
330 }
331​
332 static fromCoreHandlerError(error: AcpError, method: string) {
333 if (error._tag === "AcpRequestError") {
334 return error;
335 }
336 return AcpRequestError.internalError(
337 `ACP request handler failed for method '${method}'`,
338 undefined,
339 {
340 method,
341 operation: "handle-request",
342 cause: error,
343 },
344 );
345 }
346​
347 static fromExtensionHandlerError(error: AcpError, method: string) {
348 if (error._tag === "AcpRequestError") {
349 return error;
350 }
351 return AcpRequestError.internalError(
352 `ACP extension request handler failed for method '${method}'`,
353 undefined,
354 {
355 method,
356 operation: "handle-extension-request",
357 cause: error,
358 },
359 );
360 }
361​
362 static parseError(message = "Parse error", data?: unknown) {
363 return new AcpRequestError({
364 code: -32700,
365 errorMessage: message,
366 ...(data !== undefined ? { data } : {}),
367 });
368 }
369​
370 static invalidRequest(message = "Invalid request", data?: unknown) {
371 return new AcpRequestError({
372 code: -32600,
373 errorMessage: message,
374 ...(data !== undefined ? { data } : {}),
375 });
376 }
377​
378 static methodNotFound(method: string) {
379 return new AcpRequestError({
380 code: -32601,
381 errorMessage: `Method not found: ${method}`,
382 });
383 }
384​
385 static invalidParams(message = "Invalid params", data?: unknown) {
386 return new AcpRequestError({
387 code: -32602,
388 errorMessage: message,
389 ...(data !== undefined ? { data } : {}),
390 });
391 }
392​
393 static invalidExtensionPayload(method: string, cause: Schema.SchemaError) {
394 const diagnostics = schemaIssueDiagnostics(cause.issue);
395 return new AcpRequestError({
396 code: -32602,
397 errorMessage: `Invalid payload for ACP extension method '${method}'.`,
398 data: diagnostics,
399 method,
400 operation: "decode-extension-request-payload",
401 ...diagnostics,
402 cause,
403 });
404 }
405​
406 static internalError(
407 message = "Internal error",
408 data?: unknown,
409 diagnostics: AcpRequestDiagnostics = {},
410 ) {
411 return new AcpRequestError({
412 code: -32603,
413 errorMessage: message,
414 ...(data !== undefined ? { data } : {}),
415 ...diagnostics,
416 });
417 }
418​
419 static authRequired(message = "Authentication required", data?: unknown) {
420 return new AcpRequestError({
421 code: -32000,
422 errorMessage: message,
423 ...(data !== undefined ? { data } : {}),
424 });
425 }
426​
427 static resourceNotFound(message = "Resource not found", data?: unknown) {
428 return new AcpRequestError({
429 code: -32002,
430 errorMessage: message,
431 ...(data !== undefined ? { data } : {}),
432 });
433 }
434​
435 toProtocolError() {
436 return AcpSchema.Error.make({
437 code: this.code,
438 message: this.errorMessage,
439 ...(this.data !== undefined ? { data: this.data } : {}),
440 });
441 }
443​
444export const AcpError = Schema.Union([
445 AcpRequestError,
446 AcpSpawnError,
447 AcpProcessExitedError,
448 AcpProtocolParseError,
449 AcpTransportError,
450 AcpInputStreamEndedError,
451]);
452​
453export type AcpError = typeof AcpError.Type;