Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion go/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Comment on lines +164 to +166

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of terminating the entire application with log.Fatalf, it is more idiomatic in Go to return the error to the caller, especially since the function signature FilesCreateVideo() (*genai.GenerateContentResponse, error) already supports returning an error. This allows callers (and unit tests) to handle the failure gracefully.

Suggested change
if myfile.State == genai.FileStateFailed {
log.Fatalf("video file processing failed: %v", myfile.State)
}
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)
Expand Down
3 changes: 3 additions & 0 deletions go/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions go/test_utils_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions javascript/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions python/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading