Skip to content

Commit a559447

Browse files
juliusmarmingecodex
andcommitted
fix(acp): return typed schema collection failures
Co-authored-by: codex <[email protected]>
1 parent 162cbae commit a559447

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

‎packages/effect-acp/scripts/generate.test.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const isDownloadError = Schema.is(Generator.AcpGeneratorDownloadError);
1313
const isDownloadFileError = Schema.is(Generator.AcpGeneratorDownloadFileError);
1414
const isDocumentDecodeError = Schema.is(Generator.AcpGeneratorDocumentDecodeError);
1515
const isFormatExitError = Schema.is(Generator.AcpGeneratorFormatExitError);
16+
const isSchemaNameParseError = Schema.is(Generator.AcpGeneratorSchemaNameParseError);
17+
const isSchemaValueDeclarationMissingError = Schema.is(
18+
Generator.AcpGeneratorSchemaValueDeclarationMissingError,
19+
);
1620

1721
const httpClient = (response: Response) =>
1822
HttpClient.make((request) => Effect.succeed(HttpClientResponse.fromWeb(request, response)));
@@ -61,7 +65,10 @@ describe("ACP schema generator errors", () => {
6165
expect(error.cause).toBeDefined();
6266
expect(error.message).toContain(outputPath);
6367
const { cause: _, ...directDiagnostics } = error;
64-
expect(JSON.stringify(directDiagnostics)).not.toMatch(
68+
expect(directDiagnostics).not.toHaveProperty("url");
69+
expect(directDiagnostics.urlProtocol).toBe("https:");
70+
expect(directDiagnostics.urlHostname).toBe("example.test");
71+
expect(error.message).not.toMatch(
6572
/generator-user|generator-password|private|token|generator-secret|fragment/,
6673
);
6774
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
@@ -140,4 +147,29 @@ describe("ACP schema generator errors", () => {
140147
expect(spawned?.args).toEqual(["oxfmt", generatedDir]);
141148
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
142149
});
150+
151+
it.effect("returns malformed generated schema declarations as typed failures", () =>
152+
Effect.gen(function* () {
153+
const missingValueError = yield* Generator.collectSchemaEntries(
154+
"export type Session = string;",
155+
).pipe(Effect.flip);
156+
assert(isSchemaValueDeclarationMissingError(missingValueError));
157+
expect(missingValueError).toMatchObject({
158+
lineIndex: 0,
159+
typeDeclarationLength: 29,
160+
nextLinePresent: false,
161+
});
162+
expect(missingValueError).not.toHaveProperty("typeDeclaration");
163+
164+
const nameParseError = yield* Generator.collectSchemaEntries(
165+
"export type @ = string;\nexport const invalid = Schema.String;",
166+
).pipe(Effect.flip);
167+
assert(isSchemaNameParseError(nameParseError));
168+
expect(nameParseError).toMatchObject({
169+
lineIndex: 0,
170+
typeDeclarationLength: 23,
171+
});
172+
expect(nameParseError).not.toHaveProperty("typeDeclaration");
173+
}),
174+
);
143175
});

‎packages/effect-acp/scripts/generate.ts‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,27 @@ export class AcpGeneratorFormatExitError extends Schema.TaggedErrorClass<AcpGene
112112

113113
export class AcpGeneratorSchemaValueDeclarationMissingError extends Schema.TaggedErrorClass<AcpGeneratorSchemaValueDeclarationMissingError>()(
114114
"AcpGeneratorSchemaValueDeclarationMissingError",
115-
{ typeDeclaration: Schema.String },
115+
{
116+
lineIndex: Schema.Number,
117+
typeDeclarationLength: Schema.Number,
118+
nextLinePresent: Schema.Boolean,
119+
nextLineLength: Schema.optional(Schema.Number),
120+
},
116121
) {
117122
override get message(): string {
118-
return `Generated ACP schema type declaration has no following value declaration: ${this.typeDeclaration}`;
123+
return `Generated ACP schema type declaration at line ${this.lineIndex + 1} has no following value declaration.`;
119124
}
120125
}
121126

122127
export class AcpGeneratorSchemaNameParseError extends Schema.TaggedErrorClass<AcpGeneratorSchemaNameParseError>()(
123128
"AcpGeneratorSchemaNameParseError",
124-
{ typeDeclaration: Schema.String },
129+
{
130+
lineIndex: Schema.Number,
131+
typeDeclarationLength: Schema.Number,
132+
},
125133
) {
126134
override get message(): string {
127-
return `Could not extract an ACP schema name from generated declaration: ${this.typeDeclaration}`;
135+
return `Could not extract an ACP schema name from generated declaration at line ${this.lineIndex + 1}.`;
128136
}
129137
}
130138

@@ -313,9 +321,7 @@ const writeGeneratedFiles = Effect.fn("writeGeneratedFiles")(function* (
313321
yield* fs.writeFileString(metaOutputPath, metaOutput);
314322
});
315323

316-
function collectSchemaEntries(
317-
chunk: string,
318-
): ReadonlyArray<{ readonly name: string; readonly code: string }> {
324+
export const collectSchemaEntries = Effect.fn("collectSchemaEntries")(function* (chunk: string) {
319325
const lines = chunk
320326
.split("\n")
321327
.map((line) => line.trim())
@@ -330,14 +336,20 @@ function collectSchemaEntries(
330336

331337
const constLine = lines[index + 1];
332338
if (!constLine?.startsWith("export const ")) {
333-
throw new AcpGeneratorSchemaValueDeclarationMissingError({
334-
typeDeclaration: typeLine,
339+
return yield* new AcpGeneratorSchemaValueDeclarationMissingError({
340+
lineIndex: index,
341+
typeDeclarationLength: typeLine.length,
342+
nextLinePresent: constLine !== undefined,
343+
...(constLine === undefined ? {} : { nextLineLength: constLine.length }),
335344
});
336345
}
337346

338347
const match = /^export type ([A-Za-z0-9_]+)/.exec(typeLine);
339348
if (!match?.[1]) {
340-
throw new AcpGeneratorSchemaNameParseError({ typeDeclaration: typeLine });
349+
return yield* new AcpGeneratorSchemaNameParseError({
350+
lineIndex: index,
351+
typeDeclarationLength: typeLine.length,
352+
});
341353
}
342354

343355
entries.push({
@@ -348,7 +360,7 @@ function collectSchemaEntries(
348360
}
349361

350362
return entries;
351-
}
363+
});
352364

353365
function normalizeNullableTypes(value: Schema.Json): Schema.Json {
354366
if (Array.isArray(value)) {
@@ -431,7 +443,8 @@ const generateSchemas = Effect.fn("generateSchemas")(function* (skipDownload: bo
431443

432444
const output = generator.generate("openapi-3.1", normalizedDefinitions as never, false).trim();
433445
if (output.length > 0) {
434-
for (const entry of collectSchemaEntries(output)) {
446+
const schemaEntries = yield* collectSchemaEntries(output);
447+
for (const entry of schemaEntries) {
435448
if (!generatedEntries.has(entry.name)) {
436449
generatedEntries.set(entry.name, entry.code);
437450
}

0 commit comments

Comments
 (0)