File tree Expand file tree Collapse file tree
extensions/googlechat/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22import crypto from "node:crypto" ;
33import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime" ;
44import { parseMediaContentLength } from "openclaw/plugin-sdk/media-runtime" ;
5- import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http" ;
5+ import {
6+ readProviderJsonResponse ,
7+ readResponseTextLimited ,
8+ } from "openclaw/plugin-sdk/provider-http" ;
69import { readResponseWithLimit } from "openclaw/plugin-sdk/response-limit-runtime" ;
710import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime" ;
811import type { ResolvedGoogleChatAccount } from "./accounts.js" ;
@@ -54,7 +57,7 @@ async function withGoogleChatResponse<T>(params: {
5457 } ) ;
5558 try {
5659 if ( ! response . ok ) {
57- const text = await response . text ( ) . catch ( ( ) => "" ) ;
60+ const text = await readResponseTextLimited ( response ) . catch ( ( ) => "" ) ;
5861 throw new Error ( `${ errorPrefix } ${ response . status } : ${ text || response . statusText } ` ) ;
5962 }
6063 return await handleResponse ( response ) ;
Original file line number Diff line number Diff line change @@ -658,5 +658,47 @@ describe("verifyGoogleChatRequest", () => {
658658 expect ( canceled ) . toBe ( true ) ;
659659 expect ( bytesPulled ) . toBeLessThan ( TOTAL_CHUNKS * ONE_MIB ) ;
660660 } ) ;
661+
662+ it ( "caps non-OK sendMessage error bodies before formatting the API error" , async ( ) => {
663+ const ONE_MIB = 1024 * 1024 ;
664+ const TOTAL_CHUNKS = 32 ;
665+ const chunk = new TextEncoder ( ) . encode ( "x" . repeat ( ONE_MIB ) ) ;
666+
667+ let bytesPulled = 0 ;
668+ let canceled = false ;
669+ const oversizedError = new Response (
670+ new ReadableStream < Uint8Array > ( {
671+ pull ( controller ) {
672+ if ( bytesPulled >= TOTAL_CHUNKS * ONE_MIB ) {
673+ controller . close ( ) ;
674+ return ;
675+ }
676+ bytesPulled += chunk . length ;
677+ controller . enqueue ( chunk ) ;
678+ } ,
679+ cancel ( ) {
680+ canceled = true ;
681+ } ,
682+ } ) ,
683+ { status : 500 , statusText : "Internal Server Error" } ,
684+ ) ;
685+ const release = vi . fn ( async ( ) => { } ) ;
686+ mocks . fetchWithSsrFGuard . mockResolvedValueOnce ( {
687+ response : oversizedError ,
688+ release,
689+ } ) ;
690+
691+ await expect (
692+ sendGoogleChatMessage ( {
693+ account,
694+ space : "spaces/AAA" ,
695+ text : "hello" ,
696+ } ) ,
697+ ) . rejects . toThrow ( / ^ G o o g l e C h a t A P I 5 0 0 : x + / ) ;
698+
699+ expect ( canceled ) . toBe ( true ) ;
700+ expect ( bytesPulled ) . toBeLessThan ( TOTAL_CHUNKS * ONE_MIB ) ;
701+ expect ( release ) . toHaveBeenCalledOnce ( ) ;
702+ } ) ;
661703 } ) ;
662704} ) ;
You can’t perform that action at this time.
0 commit comments