Skip to content

Commit e044378

Browse files
authored
Merge pull request #69 from Cedarich/feature/centralized-error-handling-complete
2 parents e7e3c03 + 1cfe3d0 commit e044378

13 files changed

Lines changed: 1080 additions & 311 deletions

File tree

ai-invoice-extractor/bun.lock

Lines changed: 219 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ai-invoice-extractor/src/cli.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { invoiceOutputSchema } from "./prompts/extract-invoice.prompt.js";
1111
import type { AiConfig } from "./types.js";
1212
import { ConfigUtils } from "./utils/config.js";
1313
import { StringUtils } from "./utils/string.js";
14+
import { BaseError } from "../../shared/errors/base.js";
15+
import { ErrorCode } from "../../shared/errors/types.js";
1416

1517
export type CliOptions = z.infer<typeof CliOptions>;
1618
export const CliOptions = z.object({
@@ -59,7 +61,7 @@ export async function main(argv: string[]) {
5961
const messages = parsedCliOptions.error.issues.map((issue) =>
6062
issue.path.length
6163
? `${issue.path.join(".")}: ${issue.message}`
62-
: issue.message,
64+
: issue.message
6365
);
6466
stderr.push(messages.join("\n"));
6567
exitCode = 1;
@@ -99,6 +101,8 @@ export async function main(argv: string[]) {
99101
// ---------------------------
100102
const extractor = Extractor.create(mergedAiConfig);
101103

104+
const requestId = Math.random().toString(36).substring(7);
105+
102106
try {
103107
const data = await extractor.analyseFile({
104108
path: filePath,
@@ -112,8 +116,19 @@ export async function main(argv: string[]) {
112116
stdout.push(`\n${JSON.stringify(data)}\n\n`);
113117
}
114118
} catch (error) {
115-
stderr.push(`${(error as Error).message}\n`);
116-
exitCode = 1;
119+
if (error instanceof BaseError) {
120+
stderr.push(`Error ${error.code}: ${error.userMessage}`);
121+
if (error.recovery.type === "retry") {
122+
stderr.push(`Recovery: ${error.recovery.description}`);
123+
}
124+
if (error.technicalDetails && process.env.DEBUG) {
125+
stderr.push(`Technical details: ${error.technicalDetails}`);
126+
}
127+
exitCode = Math.floor(error.statusCode / 100) === 4 ? 1 : 2;
128+
} else {
129+
stderr.push(`Unexpected error: ${(error as Error).message}`);
130+
exitCode = 2;
131+
}
117132
}
118133
});
119134

0 commit comments

Comments
 (0)