-
Notifications
You must be signed in to change notification settings - Fork 180
feat(pkg): Add package errors for the CLI
#636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+615
−0
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
47111f4
feat(pkg): Initialize package errors
vg006 c907fda
feat(pkg): Add error type and methods
vg006 9db5509
refac(pkg): Update error parse with utils
vg006 f5f5a89
fix(pkg): Add utilities to fix cycle import
vg006 74c9892
feat(pkg): Add functions and methods to errors
vg006 114940d
fix(pkg): Update error package and Lint
vg006 1484162
refac(pkg): Update the Error structure
vg006 e1e5e71
feat(pkg): Add tests to the errors package
vg006 f462a9b
refac(pkg): update the methods, tests and styles
vg006 e7afe9b
fix(pkg): resolve review comments in errors
vg006 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| package errors | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/charmbracelet/lipgloss/tree" | ||
| "github.com/goharbor/harbor-cli/pkg/views" | ||
| ) | ||
|
|
||
| var ( | ||
| as = errors.As | ||
| ) | ||
|
|
||
| type Frame struct { | ||
| Message string | ||
| Hints []string | ||
| } | ||
|
|
||
| type Error struct { | ||
| frames []Frame | ||
| cause error | ||
| } | ||
|
|
||
| func New(message string, hints ...string) *Error { | ||
| return &Error{ | ||
| frames: []Frame{{Message: message, Hints: hints}}, | ||
| } | ||
| } | ||
|
|
||
| func Newf(format string, args ...any) *Error { | ||
| return &Error{ | ||
| frames: []Frame{{Message: fmt.Sprintf(format, args...)}}, | ||
| } | ||
| } | ||
|
|
||
| func NewWithCause(cause error, message string, hints ...string) *Error { | ||
| return &Error{ | ||
| cause: cause, | ||
| frames: []Frame{{Message: message, Hints: hints}}, | ||
| } | ||
| } | ||
|
|
||
| func (e *Error) WithMessage(message string, hints ...string) *Error { | ||
| e.frames = append(e.frames, Frame{Message: message, Hints: hints}) | ||
| return e | ||
| } | ||
|
|
||
| func (e *Error) Error() string { | ||
| if len(e.frames) == 0 { | ||
| return "" | ||
| } | ||
|
|
||
| var parts []string | ||
|
|
||
| rootFrame := e.frames[0] | ||
| parts = append(parts, views.ErrCauseStyle.Render(rootFrame.Message)) | ||
|
|
||
| if e.cause != nil { | ||
| if code := parseHarborErrorCode(e.cause); code != "" { | ||
| parts = append(parts, | ||
| views.ErrTitleStyle.Render("Code: ")+views.ErrCauseStyle.Render(code), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| if e.cause != nil { | ||
| causeText := e.cause.Error() | ||
| if he := isHarborError(e.cause); he != nil { | ||
| causeText = he.Message() | ||
| } | ||
|
|
||
| cause := views.ErrTitleStyle.Render("Cause: ") | ||
| causeText = views.ErrCauseStyle.Render(causeText) | ||
| causeTree := tree.Root(cause + causeText). | ||
| Enumerator(tree.RoundedEnumerator). | ||
| EnumeratorStyle(views.ErrEnumeratorStyle). | ||
| ItemStyle(views.ErrHintStyle) | ||
|
|
||
| if he := isHarborError(e.cause); he != nil { | ||
| for _, h := range he.Hints() { | ||
| causeTree.Child(h) | ||
| } | ||
| } | ||
| parts = append(parts, causeTree.String()) | ||
| } | ||
|
|
||
| if len(rootFrame.Hints) > 0 { | ||
| hintsTree := tree.New(). | ||
| Root("Hints:"). | ||
| RootStyle(views.ErrTitleStyle). | ||
| Enumerator(tree.RoundedEnumerator). | ||
| EnumeratorStyle(views.ErrEnumeratorStyle). | ||
| ItemStyle(views.ErrHintStyle) | ||
|
|
||
| for _, h := range rootFrame.Hints { | ||
| hintsTree.Child(h) | ||
| } | ||
| parts = append(parts, hintsTree.String()) | ||
| } | ||
|
|
||
| if len(e.frames) > 1 { | ||
| msgTree := tree.New(). | ||
| Root("Messages:"). | ||
| RootStyle(views.ErrTitleStyle). | ||
| Enumerator(tree.RoundedEnumerator). | ||
| EnumeratorStyle(views.ErrEnumeratorStyle). | ||
| ItemStyle(views.ErrTitleStyle) | ||
|
|
||
| for _, f := range e.frames[1:] { | ||
| msgWithHints := tree.Root(f.Message). | ||
| RootStyle(views.ErrTitleStyle). | ||
| Enumerator(tree.RoundedEnumerator). | ||
| EnumeratorStyle(views.ErrEnumeratorStyle). | ||
| ItemStyle(views.ErrHintStyle) | ||
| for _, h := range f.Hints { | ||
| msgWithHints.Child(h) | ||
| } | ||
| msgTree.Child(msgWithHints) | ||
| } | ||
| parts = append(parts, msgTree.String()) | ||
| } | ||
|
|
||
| return strings.Join(parts, "\n") | ||
| } | ||
|
|
||
| func (e *Error) Message() string { | ||
| if len(e.frames) == 0 { | ||
| return "" | ||
| } | ||
| return e.frames[0].Message | ||
| } | ||
|
|
||
| func (e *Error) Errors() []string { | ||
| msgs := make([]string, len(e.frames)) | ||
| for i, f := range e.frames { | ||
| msgs[i] = f.Message | ||
| } | ||
| return msgs | ||
| } | ||
|
|
||
| func (e *Error) Hints() []string { | ||
| var all []string | ||
| for _, f := range e.frames { | ||
| all = append(all, f.Hints...) | ||
| } | ||
| return all | ||
| } | ||
|
|
||
| func (e *Error) Frames() []Frame { | ||
| if len(e.frames) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| frames := make([]Frame, len(e.frames)) | ||
| for i, f := range e.frames { | ||
| frames[i].Message = f.Message | ||
| if len(f.Hints) > 0 { | ||
| frames[i].Hints = append([]string(nil), f.Hints...) | ||
| } | ||
| } | ||
|
|
||
| return frames | ||
| } | ||
|
|
||
| func (e *Error) Cause() error { return e.cause } | ||
|
|
||
| func (e *Error) Status() string { | ||
| if e.cause == nil { | ||
| return "" | ||
| } | ||
| return parseHarborErrorCode(e.cause) | ||
| } | ||
|
|
||
| func (e *Error) Unwrap() error { return e.cause } | ||
| func AsError(err error) *Error { | ||
| var e *Error | ||
| if errors.As(err, &e) { | ||
| return e | ||
| } | ||
| return &Error{ | ||
| frames: []Frame{{Message: parseHarborErrorMsg(err)}}, | ||
| cause: err, | ||
| } | ||
| } | ||
|
|
||
| func IsError(err error) bool { | ||
| var e *Error | ||
| return as(err, &e) | ||
| } | ||
|
|
||
| func Cause(err error) error { | ||
| if e := isHarborError(err); e != nil { | ||
| return e.Cause() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func Hints(err error) []string { | ||
| if e := isHarborError(err); e != nil { | ||
| return e.Hints() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func Status(err error) string { | ||
| if e := isHarborError(err); e != nil { | ||
| return e.Status() | ||
| } | ||
| return "" | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.