@@ -156,8 +156,8 @@ export class ChatSession {
156156 this . _apiSettings ,
157157 this . model ,
158158 generateContentRequest ,
159- // Merge requestOptions
160159 this . chromeAdapter ,
160+ // Merge requestOptions
161161 {
162162 ...this . requestOptions ,
163163 ...singleRequestOptions
@@ -167,12 +167,20 @@ export class ChatSession {
167167 // Add onto the chain.
168168 this . _sendPromise = this . _sendPromise
169169 . then ( ( ) => streamPromise )
170- // This must be handled to avoid unhandled rejection, but jump
171- // to the final catch block with a label to not log this error.
170+ . then ( streamResult => streamResult . response )
172171 . catch ( _ignored => {
173172 throw new Error ( SILENT_ERROR ) ;
174173 } )
175- . then ( streamResult => streamResult . response )
174+ // We want to log errors that the user cannot catch.
175+ // The user can catch all errors that are thrown from the `streamPromise` and the
176+ // `streamResult.response`, since these are returned to the user in the `GenerateContentResult`.
177+ // The user cannot catch errors that are thrown in the following `then` block, which appends
178+ // the model's response to the chat history.
179+ //
180+ // To prevent us from logging errors that the user *can* catch, we re-throw them as
181+ // SILENT_ERROR, then in the final `catch` block below, we only log errors that are not
182+ // SILENT_ERROR. There is currently no way for these errors to be propagated to the user,
183+ // so we log them to try to make up for this.
176184 . then ( response => {
177185 if ( response . candidates && response . candidates . length > 0 ) {
178186 this . _history . push ( newContent ) ;
@@ -192,16 +200,7 @@ export class ChatSession {
192200 }
193201 } )
194202 . catch ( e => {
195- // Errors in streamPromise are already catchable by the user as
196- // streamPromise is returned.
197- // Avoid duplicating the error message in logs.
198- // AbortErrors are thrown after the initial streamPromise resolves, since the request
199- // may be aborted once streaming has begun. Since these errors won't be wrapped in a SILENT_ERROR,
200- // we have to explicitly check for them. The user will be able to catch these AbortErrors when
201- // awaiting the resolution of the result.response.
202- if ( e . message !== SILENT_ERROR && e . name !== 'AbortError' ) {
203- // Users do not have access to _sendPromise to catch errors
204- // downstream from streamPromise, so they should not throw.
203+ if ( e . message !== SILENT_ERROR ) {
205204 logger . error ( e ) ;
206205 }
207206 } ) ;
0 commit comments