Skip to content

fix(api): propagate errors from GetLabel instead of returning nil#1004

Open
AdeshDeshmukh wants to merge 1 commit into
goharbor:mainfrom
AdeshDeshmukh:fix/getlabel-return-error
Open

fix(api): propagate errors from GetLabel instead of returning nil#1004
AdeshDeshmukh wants to merge 1 commit into
goharbor:mainfrom
AdeshDeshmukh:fix/getlabel-return-error

Conversation

@AdeshDeshmukh

Copy link
Copy Markdown

Fixes #1003

GetLabel was the only API handler that returned a bare pointer with no error.
Every failure got replaced with nil, leaving callers blind to what happened.
In the artifact label add path this meant a nil pointer dereference when
the code later accessed label.Name.

The fix is straightforward — change the signature to return an error like
every other function in the API layer does, and update both callers to
handle it properly.

Changes:

  • pkg/api/label_handler.go: GetLabel now returns (*models.Label, error).
    Both error paths return the actual error instead of nil.
  • cmd/harbor/root/labels/update.go: Replaced the nil check with a proper
    error check so users see the real error instead of "label is not found".
  • cmd/harbor/root/artifact/label/add.go: Added an error check after the
    GetLabel call so label.Name is only accessed when we have a valid label.

All existing tests pass. No new dependencies introduced.

Copilot AI review requested due to automatic review settings June 16, 2026 06:11

Copilot AI left a comment

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates the label retrieval API to return errors instead of silently returning nil, and propagates that change to CLI commands that depend on label lookup.

Changes:

  • Change api.GetLabel to return (*models.Label, error) and propagate client/context errors.
  • Update labels update command to handle GetLabel errors instead of checking for nil.
  • Update artifact label add command to handle GetLabel errors and format Harbor error messages.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/api/label_handler.go Adjusts GetLabel to return an error on failure rather than nil.
cmd/harbor/root/labels/update.go Updates label update flow to handle GetLabel error return value.
cmd/harbor/root/artifact/label/add.go Updates artifact label add flow to handle GetLabel errors and parse Harbor error messages.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +71 to 74
existingLabel, err := api.GetLabel(labelId)
if err != nil {
return fmt.Errorf("failed to get label: %v", err)
}
Comment thread cmd/harbor/root/labels/update.go Outdated
return fmt.Errorf("label is not found")
existingLabel, err := api.GetLabel(labelId)
if err != nil {
return fmt.Errorf("failed to get label: %v", err)

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.

irrelevant, this in fact increases error distinction.

@AdeshDeshmukh AdeshDeshmukh force-pushed the fix/getlabel-return-error branch from f8490ed to d713250 Compare June 16, 2026 06:13
Comment thread pkg/api/label_handler.go
Comment on lines 116 to 121
if err != nil {
return nil
return nil, err
}

return response.GetPayload()
return response.GetPayload(), nil
}

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.

Consider adding a distinction for label being returned nil / 0
As label not found.

GetLabel silently returned nil on every error, discarding all error context.
This caused callers to receive nil with no way to distinguish a network
failure from a missing label. In the artifact label add command this led
to a nil pointer dereference when accessing label.Name.

Changes:
- pkg/api/label_handler.go: GetLabel now returns (*models.Label, error).
  Both error paths propagate the actual error instead of nil.
- cmd/harbor/root/labels/update.go: Proper error handling with
  ParseHarborErrorMsg for consistent API error formatting.
- cmd/harbor/root/artifact/label/add.go: Added error check after GetLabel
  so label.Name is only accessed with a valid label.

Signed-off-by: Adesh Deshmukh <adeshkd123@gmail.com>
@AdeshDeshmukh AdeshDeshmukh force-pushed the fix/getlabel-return-error branch from d713250 to 4ef7586 Compare June 21, 2026 05:17
@AdeshDeshmukh

Copy link
Copy Markdown
Author

@NucleoFusion Done. Thank you for pointing out that edge case! The nil / 0 distinction is now fully handled. PTAL when you have a chance.

@NucleoFusion NucleoFusion left a comment

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.

lgtm!
Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GetLabel swallows all errors and returns nil, causing potential nil pointer dereference

3 participants