Skip to content
Merged
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
35 changes: 35 additions & 0 deletions cmd/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,41 @@ import (
"github.com/Use-Tusk/tusk-cli/internal/api"
)

// ExitCodeError wraps an error with a specific process exit code. main.go
// unwraps this to pick the right os.Exit value; without it, Cobra-returned
// errors map to exit 1.
type ExitCodeError struct {
Code int
Err error
}

func (e *ExitCodeError) Error() string {
if e == nil || e.Err == nil {
return ""
}
return e.Err.Error()
}

func (e *ExitCodeError) Unwrap() error {
if e == nil {
return nil
}
return e.Err
}

// ExitCodeOf returns the exit code embedded in err (or any wrapper in its
// chain), defaulting to 1 if none is present.
func ExitCodeOf(err error) int {
if err == nil {
return 0
}
var ec *ExitCodeError
if errors.As(err, &ec) {
return ec.Code
}
return 1
}

// formatApiError converts raw API errors into user-friendly messages with
// actionable guidance. Non-API errors pass through unchanged.
func formatApiError(err error) error {
Expand Down
Loading
Loading