@@ -32,11 +32,6 @@ import { ApiSettings } from '../types/internal';
3232import { logger } from '../logger' ;
3333import { ChromeAdapter } from '../types/chrome-adapter' ;
3434
35- /**
36- * Do not log a message for this error.
37- */
38- const SILENT_ERROR = 'SILENT_ERROR' ;
39-
4035/**
4136 * ChatSession class that enables sending chat messages and stores
4237 * history of sent and received messages so far.
@@ -153,14 +148,9 @@ export class ChatSession {
153148 this . requestOptions
154149 ) ;
155150
156- // Add onto the chain.
157- this . _sendPromise = this . _sendPromise
158- . then ( ( ) => streamPromise )
159- // This must be handled to avoid unhandled rejection, but jump
160- // to the final catch block with a label to not log this error.
161- . catch ( _ignored => {
162- throw new Error ( SILENT_ERROR ) ;
163- } )
151+ // This promise is not returned to the user, but is used to chain promise sequences that
152+ // update the history and handle errors.
153+ const historyUpdatePromise = streamPromise
164154 . then ( streamResult => streamResult . response )
165155 . then ( response => {
166156 if ( response . candidates && response . candidates . length > 0 ) {
@@ -180,16 +170,14 @@ export class ChatSession {
180170 }
181171 }
182172 } )
183- . catch ( e => {
184- // Errors in streamPromise are already catchable by the user as
185- // streamPromise is returned.
186- // Avoid duplicating the error message in logs.
187- if ( e . message !== SILENT_ERROR ) {
188- // Users do not have access to _sendPromise to catch errors
189- // downstream from streamPromise, so they should not throw.
190- logger . error ( e ) ;
191- }
173+ . catch ( ( ) => {
174+ // Suppress unhandled rejection error. The user is responsible for handling the error
175+ // on the returned streamPromise.
192176 } ) ;
177+
178+ // Chain the history update to the serialization promise.
179+ this . _sendPromise = historyUpdatePromise ;
180+
193181 return streamPromise ;
194182 }
195183}
0 commit comments