Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 63 lines 2.17 KB
Raw
1 import { Data } from 'effect';
2
3 // The generation pipeline runs inside the `generation-runtime` companion module,
4 // a separately bundled file with its own copy of these classes. Errors cross
5 // back into the CLI as plain values, so this module has no compiler import and
6 // `rehydrateGenerationError` rebuilds them as instances of the CLI's classes,
7 // which keeps `_tag` handling and error rendering exactly as before.
8
9 export class SafeOutputPathError extends Data.TaggedError('generation/SafeOutputPathError')<{
10 readonly filename: string;
11 readonly outputDir: string;
12 readonly resolvedPath: string;
13 readonly message: string;
14 }> {}
15
16 export class TypeScriptTranspileError extends Data.TaggedError('error/TypeScriptTranspileError')<{
17 readonly message: string;
18 readonly cause: string;
19 }> {}
20
21 export class GenerateTypeFromJsonSchemaError extends Data.TaggedError('json-parsing-error')<{
22 cause?: unknown;
23 message?: string;
24 }> {}
25
26 export type GenerationError =
27 SafeOutputPathError | TypeScriptTranspileError | GenerateTypeFromJsonSchemaError;
28
29 const withOriginalStack = <E extends Error>(error: E, original: Error): E => {
30 if (typeof original.stack === 'string') {
31 Object.defineProperty(error, 'stack', { value: original.stack, configurable: true });
32 }
33 return error;
34 };
35
36 /**
37 * Rebuilds a generation error produced by the companion module as an instance
38 * of this module's class, carrying over its fields and stack.
39 */
40 export const rehydrateGenerationError = (error: GenerationError): GenerationError => {
41 switch (error._tag) {
42 case 'generation/SafeOutputPathError':
43 return withOriginalStack(
44 new SafeOutputPathError({
45 filename: error.filename,
46 outputDir: error.outputDir,
47 resolvedPath: error.resolvedPath,
48 message: error.message,
49 }),
50 error
51 );
52 case 'error/TypeScriptTranspileError':
53 return withOriginalStack(
54 new TypeScriptTranspileError({ message: error.message, cause: error.cause }),
55 error
56 );
57 case 'json-parsing-error':
58 return withOriginalStack(
59 new GenerateTypeFromJsonSchemaError({ cause: error.cause, message: error.message }),
60 error
61 );
62 }
63 };