66 "errors"
77 "fmt"
88 "log/slog"
9+ "strings"
910 "time"
1011
1112 "github.com/google/uuid"
@@ -308,9 +309,10 @@ func (c *Client) ModelUpload(ctx context.Context, req ModelUploadRequest, opts M
308309}
309310
310311// consumeUploadFrame inspects a single modelUpload pipeline frame. done is
311- // true for a terminal frame ("ready" or "failed"). Intermediate statuses are
312- // reported through onStatus, deduplicated against *lastStatus so repeated
313- // frames for the same phase fire the callback once.
312+ // true for a terminal frame ("ready", "failed", or other error statuses such as
313+ // "error downloading"). Intermediate statuses are reported through onStatus,
314+ // deduplicated against *lastStatus so repeated frames for the same phase fire
315+ // the callback once.
314316func consumeUploadFrame (raw json.RawMessage , onStatus func (status , message string ), lastStatus * string ) (* ModelUploadResult , bool , error ) {
315317 var item ModelUploadResult
316318 if err := json .Unmarshal (raw , & item ); err != nil {
@@ -321,15 +323,12 @@ func consumeUploadFrame(raw json.RawMessage, onStatus func(status, message strin
321323 switch item .Status {
322324 case uploadStatusReady :
323325 return & item , true , nil
324- case uploadStatusFailed :
325- msg := item .Message
326- if msg == "" {
327- msg = "no failure reason reported"
328- }
329- return nil , true , fmt .Errorf ("model upload failed: %s" , msg )
330326 case "" :
331327 return nil , false , nil
332328 default :
329+ if msg , failed := uploadFailureMessage (item .Status , item .Message ); failed {
330+ return nil , true , fmt .Errorf ("model upload failed: %s" , msg )
331+ }
333332 if item .Status != * lastStatus {
334333 * lastStatus = item .Status
335334 if onStatus != nil {
@@ -340,6 +339,21 @@ func consumeUploadFrame(raw json.RawMessage, onStatus func(status, message strin
340339 }
341340}
342341
342+ // uploadFailureMessage reports whether a modelUpload pipeline status is terminal
343+ // failure and returns the message to surface to the caller.
344+ func uploadFailureMessage (status , message string ) (string , bool ) {
345+ if status == uploadStatusFailed || strings .Contains (strings .ToLower (status ), "error" ) {
346+ if message != "" {
347+ return message , true
348+ }
349+ if status == uploadStatusFailed {
350+ return "no failure reason reported" , true
351+ }
352+ return status , true
353+ }
354+ return "" , false
355+ }
356+
343357// Poll polls for async task results using the getResponse task type.
344358// It blocks until at least minResults items with status "success" have been
345359// returned in a single poll cycle, a data item reports status "error", the
0 commit comments