Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 219 additions & 2 deletions ai-invoice-extractor/bun.lock

Large diffs are not rendered by default.

21 changes: 18 additions & 3 deletions ai-invoice-extractor/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { invoiceOutputSchema } from "./prompts/extract-invoice.prompt.js";
import type { AiConfig } from "./types.js";
import { ConfigUtils } from "./utils/config.js";
import { StringUtils } from "./utils/string.js";
import { BaseError } from "../../shared/errors/base.js";
import { ErrorCode } from "../../shared/errors/types.js";

export type CliOptions = z.infer<typeof CliOptions>;
export const CliOptions = z.object({
Expand Down Expand Up @@ -59,7 +61,7 @@ export async function main(argv: string[]) {
const messages = parsedCliOptions.error.issues.map((issue) =>
issue.path.length
? `${issue.path.join(".")}: ${issue.message}`
: issue.message,
: issue.message
);
stderr.push(messages.join("\n"));
exitCode = 1;
Expand Down Expand Up @@ -99,6 +101,8 @@ export async function main(argv: string[]) {
// ---------------------------
const extractor = Extractor.create(mergedAiConfig);

const requestId = Math.random().toString(36).substring(7);

try {
const data = await extractor.analyseFile({
path: filePath,
Expand All @@ -112,8 +116,19 @@ export async function main(argv: string[]) {
stdout.push(`\n${JSON.stringify(data)}\n\n`);
}
} catch (error) {
stderr.push(`${(error as Error).message}\n`);
exitCode = 1;
if (error instanceof BaseError) {
stderr.push(`Error ${error.code}: ${error.userMessage}`);
if (error.recovery.type === "retry") {
stderr.push(`Recovery: ${error.recovery.description}`);
}
if (error.technicalDetails && process.env.DEBUG) {
stderr.push(`Technical details: ${error.technicalDetails}`);
}
exitCode = Math.floor(error.statusCode / 100) === 4 ? 1 : 2;
} else {
stderr.push(`Unexpected error: ${(error as Error).message}`);
exitCode = 2;
}
}
});

Expand Down
Loading
Loading