@@ -11,6 +11,8 @@ import { invoiceOutputSchema } from "./prompts/extract-invoice.prompt.js";
1111import type { AiConfig } from "./types.js" ;
1212import { ConfigUtils } from "./utils/config.js" ;
1313import { StringUtils } from "./utils/string.js" ;
14+ import { BaseError } from "../../shared/errors/base.js" ;
15+ import { ErrorCode } from "../../shared/errors/types.js" ;
1416
1517export type CliOptions = z . infer < typeof CliOptions > ;
1618export 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