Files
pmg/internal/ui/error.go
T
20e01d5cae refactor : Refactor error handling to use dry/usefulerror (#283)
* update the go.sum

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

* migrate most of the files to dry errors

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

* update the rest of the files

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

* fixs the review comments

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

* chores

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

---------

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>
Co-authored-by: Abhisek Datta <abhisek.datta@gmail.com>
2026-05-24 12:22:19 +05:30

66 lines
2.0 KiB
Go

package ui
import (
"fmt"
"os"
"github.com/safedep/dry/log"
"github.com/safedep/pmg/errcodes"
)
// ErrorExit prints a minimal, clean error message and exits with a non-zero status code.
func ErrorExit(err error) {
ErrorExitWithCode(err, 1)
}
// ErrorExitWithCode prints a minimal, clean error message and exits with code.
func ErrorExitWithCode(err error, code int) {
log.Errorf("Exiting due to error: %s", err)
usefulErr := convertToUsefulError(err)
ClearStatus()
// Use help as hint, but for unknown errors show bug report link
hint := usefulErr.Help()
if usefulErr.Code() == errcodes.Unknown {
hint = "Report this issue: https://github.com/safedep/pmg/issues/new?labels=bug"
}
if verbosityLevel == VerbosityLevelVerbose {
printVerboseError(usefulErr.Code(), usefulErr.HumanError(), hint,
usefulErr.AdditionalHelp(), usefulErr.Error())
} else {
printMinimalError(usefulErr.Code(), usefulErr.HumanError(), hint)
}
os.Exit(code)
}
// printMinimalError prints error in minimal two-line format:
func printMinimalError(code, message, hint string) {
fmt.Printf("%s %s\n", Colors.ErrorCode(" %s ", code), Colors.Red(message))
if hint != "" && hint != "No additional help is available for this error." {
fmt.Printf(" %s %s\n", Colors.Dim("→"), Colors.Dim(hint))
}
}
// printVerboseError prints detailed error for debugging (--verbose mode)
// Includes additional help and original error chain for troubleshooting
func printVerboseError(code, message, hint, additionalHelp, originalError string) {
fmt.Printf("%s %s\n", Colors.ErrorCode(" %s ", code), Colors.Red(message))
if hint != "" && hint != "No additional help is available for this error." {
fmt.Printf(" %s %s\n", Colors.Dim("→"), Colors.Dim(hint))
}
if additionalHelp != "" && additionalHelp != "No additional help is available for this error." {
fmt.Printf(" %s %s\n", Colors.Dim("→"), Colors.Dim(additionalHelp))
}
if originalError != "" && originalError != message {
fmt.Printf(" %s %s\n", Colors.Dim("┄"), Colors.Dim(originalError))
}
}