diff --git a/packages/cli/src/nonInteractiveCli.ts b/packages/cli/src/nonInteractiveCli.ts index 2d8d845114..7b89732b10 100644 --- a/packages/cli/src/nonInteractiveCli.ts +++ b/packages/cli/src/nonInteractiveCli.ts @@ -71,6 +71,7 @@ export async function runNonInteractive( ? new StreamJsonFormatter() : null; + let errorToHandle: unknown | undefined; try { consolePatcher.patch(); coreEvents.on(CoreEvent.UserFeedback, handleUserFeedback); @@ -213,6 +214,8 @@ export async function runNonInteractive( message: 'Maximum session turns exceeded', }); } + } else if (event.type === GeminiEventType.Error) { + throw event.value.error; } } @@ -302,7 +305,7 @@ export async function runNonInteractive( } } } catch (error) { - handleError(error, config); + errorToHandle = error; } finally { consolePatcher.cleanup(); coreEvents.off(CoreEvent.UserFeedback, handleUserFeedback); @@ -310,5 +313,9 @@ export async function runNonInteractive( await shutdownTelemetry(config); } } + + if (errorToHandle) { + handleError(errorToHandle, config); + } }); }