diff --git a/go/files.go b/go/files.go index cff2004..5f4f576 100644 --- a/go/files.go +++ b/go/files.go @@ -160,7 +160,10 @@ func FilesCreateVideo() (*genai.GenerateContentResponse, error) { fmt.Printf("myfile=%+v\n", myfile) // Poll until the video file is completely processed (state becomes ACTIVE). - for myfile.State == genai.FileStateUnspecified || myfile.State != genai.FileStateActive { + for myfile.State != genai.FileStateActive { + if myfile.State == genai.FileStateFailed { + return nil, fmt.Errorf("video file processing failed: %v", myfile.State) + } fmt.Println("Processing video...") fmt.Println("File state:", myfile.State) time.Sleep(5 * time.Second) diff --git a/go/test_utils.go b/go/test_utils.go index fb1a0e1..87b3cbb 100644 --- a/go/test_utils.go +++ b/go/test_utils.go @@ -25,6 +25,9 @@ func getMedia() string { // Helping for printing the response. func printResponse(resp *genai.GenerateContentResponse) { + if resp == nil { + return + } for _, cand := range resp.Candidates { if cand.Content != nil { for _, part := range cand.Content.Parts { diff --git a/go/test_utils_test.go b/go/test_utils_test.go new file mode 100644 index 0000000..7697fba --- /dev/null +++ b/go/test_utils_test.go @@ -0,0 +1,32 @@ +package examples + +import ( + "testing" + + "google.golang.org/genai" +) + +func TestPrintResponseNil(t *testing.T) { + // Must not panic on nil response + printResponse(nil) +} + +func TestPrintResponseEmpty(t *testing.T) { + resp := &genai.GenerateContentResponse{} + printResponse(resp) +} + +func TestPrintResponseWithCandidates(t *testing.T) { + resp := &genai.GenerateContentResponse{ + Candidates: []*genai.Candidate{ + { + Content: &genai.Content{ + Parts: []*genai.Part{ + {Text: "test output"}, + }, + }, + }, + }, + } + printResponse(resp) +} diff --git a/javascript/files.js b/javascript/files.js index 31cd378..7094149 100644 --- a/javascript/files.js +++ b/javascript/files.js @@ -110,6 +110,11 @@ export async function filesCreateVideo() { // Poll until the video file is completely processed (state becomes ACTIVE). while (!myfile.state || myfile.state.toString() !== 'ACTIVE') { + if (myfile.state && myfile.state.toString() === 'FAILED') { + throw new Error( + `Video file processing failed: ${myfile.error?.message || 'state is FAILED'}`, + ); + } console.log('Processing video...'); console.log('File state: ', myfile.state); await sleep(5000); diff --git a/python/files.py b/python/files.py index c462104..f9bf532 100644 --- a/python/files.py +++ b/python/files.py @@ -80,6 +80,8 @@ def test_files_create_video(self): # Poll until the video file is completely processed (state becomes ACTIVE). while not myfile.state or myfile.state.name != "ACTIVE": + if myfile.state and myfile.state.name == "FAILED": + raise ValueError(f"Video file processing failed: {getattr(myfile, 'error', None) or 'state is FAILED'}") print("Processing video...") print("File state:", myfile.state) time.sleep(5)