Skip to content

refac: artifact list command with errors package#780

Open
vg006 wants to merge 11 commits intogoharbor:mainfrom
vg006:refac/alist
Open

refac: artifact list command with errors package#780
vg006 wants to merge 11 commits intogoharbor:mainfrom
vg006:refac/alist

Conversation

@vg006
Copy link
Copy Markdown
Contributor

@vg006 vg006 commented Mar 31, 2026

Description

This PR demonstrates the implementation of the project's pkg/errors package in the #636, by replacing the existing non-uniform error return statements with the pkg/errors package APIs.

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Documentation update
  • Chore / maintenance

Changes

  • Replace the return statements with fmt.Errorf, errors.New with the built-in pkg/errors

Overview

e1 e2 e3

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

❌ Patch coverage is 65.26316% with 66 lines in your changes missing coverage. Please review.
✅ Project coverage is 8.61%. Comparing base (60ad0bd) to head (27bfaa7).
⚠️ Report is 123 commits behind head on main.

Files with missing lines Patch % Lines
pkg/utils/config.go 10.52% 17 Missing ⚠️
pkg/utils/encryption.go 5.88% 16 Missing ⚠️
pkg/errors/utils.go 48.27% 12 Missing and 3 partials ⚠️
pkg/errors/errors.go 93.63% 5 Missing and 2 partials ⚠️
cmd/harbor/root/artifact/list.go 0.00% 6 Missing ⚠️
pkg/utils/client.go 40.00% 3 Missing ⚠️
pkg/prompt/prompt.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             main    #780      +/-   ##
=========================================
- Coverage   10.99%   8.61%   -2.38%     
=========================================
  Files         173     271      +98     
  Lines        8671   13294    +4623     
=========================================
+ Hits          953    1145     +192     
- Misses       7612   12031    +4419     
- Partials      106     118      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@NucleoFusion
Copy link
Copy Markdown
Contributor

It would be nice to also have a reference for the API handlers for that to Hint etc like how chaining would work

@qcserestipy qcserestipy self-requested a review April 1, 2026 17:46
projectName, repoName, err = utils.ParseProjectRepo(args[0])
if err != nil {
return fmt.Errorf("failed to parse project/repo: %v", err)
return errors.New("Invalid project/repository format", "expected format: <project>/<repository>")
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.

This and other instances of this are wrong no? since we would use the WithHint or WithMessage here instead of a new error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, the APIs are changed a bit. WithHint is removed and merged with WithMessage which accepts the hints optionally.

In the previous style, at different layers, we could add both messages and hints optionally. So this caused issues in rendering the tree view.

Thus, I updated the WithMessage method to accept the hints optionally, but the messages aren't optional. So hints are passed only if a message is conveyed at that level.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You can check the signature of WithMessage here

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.

yeah but shouldn't it still chain here, instead of returning a new one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's my bad. Thanks for noting it down.

Actually for initial testing, I refactored simple errors in the artifact/list.go and this is one of such. After I refactored all returns functions that are invoked at each layer. So I forgot to replace the initial one with the actually refactored one at the primitive layer. I have updated it now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You can view the related changes here [1][2]

@vg006
Copy link
Copy Markdown
Contributor Author

vg006 commented Apr 7, 2026

@NucleoFusion The exact errors that will be returned by the API calls are quite hard to reproduce. So, if the sample tree needed to be visualized, it can be seen the in the preview of this PR #636.

But the least possible demonstration is shown below, which is produced when the CLI makes API calls to Harbor instance that is down.

Here the error is raised while attempting to fetch project name.

e4

For simplicity, use could remove the message add here, so render the following.

e5

@vg006 vg006 requested a review from NucleoFusion April 7, 2026 07:17
vg006 added 11 commits April 7, 2026 17:16
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Signed-off-by: vg006 <devvg006@gmail.com>
Copy link
Copy Markdown
Collaborator

@qcserestipy qcserestipy left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Looks very cool. I just left a few comments here and there.

Hints []string
}

type Error struct {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I really like this package and think it is great. I wanted to note that it might be worth to document that we are shadowing the std errors package and explicitely telling people that they have to be careful about it (Like making a dedicated docs md for this).
A good way to let people do this difference could be this:

import (
    stderrors "errors"
    "github.com/goharbor/harbor-cli/pkg/errors"
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for supporting my proposal. Yeah, maintaining proper documentation for this package will help maintainers and contributors to refactor either the project or the package itself. I think adding that doc to the CONTRIBUTING.md will inform the new contributors at the first place, if they work on this package or try to handle errors.

Also yeah, this package wraps the std errors, enforces to use it rather for uniformity across the codebase. Thus we could instruct the contributors to use this package instead of the std. So there will be only,

import (
    "github.com/goharbor/harbor-cli/pkg/errors"
)

)

const (
GreenANSI = "\033[32m"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

To me it looks like that those colors and styles are already defined further up. Also, there are not used anywhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah the styles that are declared using the lipgloss library are the ones that I introduced newly. But the hardcoded ANSI sequences were defined earlier in the project itself and removed by @NucleoFusion last month (ref). I don't know why, but I suspect an issue while rebasing. I will check it and clean it up.

parts = append(parts, hintsTree.String())
}

if len(e.frames) > 1 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It would be great if you could test whether the tree structure output breaks in pipeline environments. I do not know whether all terms or envs would support this or expect single line error messages. Maybe @bupd and @NucleoFusion have some ideas, too.

Copy link
Copy Markdown
Contributor Author

@vg006 vg006 Apr 9, 2026

Choose a reason for hiding this comment

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

This tree output is planned to only be returned on non-verbose mode, where it's used by a real user. But for the CI and any other environment, the verbose mode of logging will be used I guess. So this tree won't be renderded, rather the error will be detailed by the logger as in the PR #722.

Even if CI or other envs use non-verbose mode, it will work I believe, because the ANSI sequences are valid UTF-8 encoded, won't disrupt tools like grep, rg parsing them, when piped the harbor output in their scripts.

For example, I tested the following in the local env to check compatibility with grep

package main

import (
	"fmt"

	"github.com/charmbracelet/lipgloss"
)

func main() {
	style := lipgloss.NewStyle().
		Bold(true).
		Foreground(lipgloss.Color("205")).
		Padding(1)
	output := style.Render("Hello, Pipeline!")
	fmt.Println(output)
}

Preview

ci

@vg006
Copy link
Copy Markdown
Contributor Author

vg006 commented Apr 9, 2026

Thank you @qcserestipy for supporting and reviewing the package. However, the above comments are very specific to the errors package that we are developing in the PR #636, as this PR is a sample, demonstrating the errors package by refactoring the artifact list command.

As you really interested in the betterment of this package, I really appreciate you to look into it PR #636 and will be happy to discuss further in there, will be helpful to all track relevant reviews and changes properly, that's why.

Thank you.

@NucleoFusion
Copy link
Copy Markdown
Contributor

NucleoFusion commented Apr 9, 2026

Thank you @qcserestipy for supporting and reviewing the package. However, the above comments are very specific to the errors package that we are developing in the PR #636, as this PR is a sample, demonstrating the errors package by refactoring the artifact list command.

As you really interested in the betterment of this package, I really appreciate you to look into it PR #636 and will be happy to discuss further in there, will be helpful to all track relevant reviews and changes properly, that's why.

Thank you.

Isnt this PR much suited for merging? since we also get a reference material which the umbrella issue can mention and even other contributors can pick up on?

I think the changes (aside from the errors package) are simple enough that we wont need to spend a lot of extra time reviewing it.
@qcserestipy what do you think?

@vg006
Copy link
Copy Markdown
Contributor Author

vg006 commented Apr 9, 2026

Yeah @NucleoFusion,

  • This PR is partially ready to merge, as it needs some more subtle changes.
  • This could be used as a referrence and could create an umbrella issue, tracking all the migrations.
  • Also the migration is quite simple and easier to review and manage, if we move command-by-command.

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.

Proposal: Fix improper logging and error handling

3 participants