Skip to content
Open
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
15 changes: 8 additions & 7 deletions cli/cmd/doc/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,7 @@ func runUpload(ctx context.Context, opts *UploadOptions, fopts *cmdutil.FormatOp
}
k, err := svc.CreateKnowledgeFromFile(ctx, kbID, path, meta, opts.EnableMultimodel, opts.Name, cmp.Or(opts.Channel, uploadChannel), nil)
if err != nil {
if errors.Is(err, sdk.ErrDuplicateFile) {
// SDK returns sentinel without an "HTTP error <status>:" prefix
// (the duplicate is detected by file hash, not by status code),
// so WrapHTTP would misclassify it as network.error.
return existingDocumentError(err, k, "file already uploaded to this knowledge base")
}
return cmdutil.WrapHTTP(err, "upload %s", path)
return classifyUploadError(err, k, path)
}
return renderUploadSuccess(k, fopts, "Uploaded", opts.Name, path)
}
Expand All @@ -324,3 +318,10 @@ func existingDocumentError(cause error, k *sdk.Knowledge, message string) *cmdut
}
return err
}

func classifyUploadError(err error, k *sdk.Knowledge, path string) error {
if errors.Is(err, sdk.ErrDuplicateFile) {
return existingDocumentError(err, k, "file already uploaded to this knowledge base")
}
return cmdutil.WrapHTTP(err, "upload %s", path)
}
2 changes: 1 addition & 1 deletion cli/cmd/doc/upload_recursive.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func runUploadRecursive(ctx context.Context, opts *UploadOptions, fopts *cmdutil
if !fopts.WantsJSON() {
fmt.Fprintf(iostreams.IO.Out, "FAIL %s: %v\n", filepath.Base(p), err)
}
return err
return classifyUploadError(err, k, p)
}
id, name := "", ""
if k != nil {
Expand Down
7 changes: 4 additions & 3 deletions cli/cmd/doc/upload_recursive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestUploadRecursive_PartialFailure_Exits1(t *testing.T) {
require.ErrorAs(t, err, &typed)
// Any per-file failure aggregates to operation.failed (exit 1), matching the
// batch-delete contract — a partial failure (even a permanent one like a
// duplicate, which classifies as server.error per-file) must not surface as
// duplicate, which classifies as resource.already_exists per-file) must not surface as
// a retryable exit 7 an agent would loop on. Per-file codes live in the
// batch envelope.
assert.Equal(t, cmdutil.CodeOperationFailed, typed.Code)
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestUploadRecursive_JSON_BatchEnvelope(t *testing.T) {
k *sdk.Knowledge
err error
}{
"bad.pdf": {err: errors.New("HTTP error 500: internal")},
"bad.pdf": {err: sdk.ErrDuplicateFile},
}}
opts := &UploadOptions{Recursive: true, Glob: "*"}
err := runUploadRecursive(context.Background(), opts, &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, svc, "kb_xxx", dir)
Expand Down Expand Up @@ -297,7 +297,8 @@ func TestUploadRecursive_JSON_BatchEnvelope(t *testing.T) {
assert.NotNil(t, item.Result)
} else {
assert.False(t, item.OK)
assert.NotNil(t, item.Error)
require.NotNil(t, item.Error)
assert.Equal(t, string(cmdutil.CodeResourceAlreadyExists), item.Error.Type)
}
}

Expand Down
Loading