diff --git a/kpm.go b/kpm.go index fbafb996..89d5f92c 100644 --- a/kpm.go +++ b/kpm.go @@ -16,7 +16,8 @@ func main() { reporter.InitReporter() kpmcli, err := client.NewKpmClient() if err != nil { - reporter.Fatal(err) + wrappedErr := reporter.WrapError("failed to initialize kpm client", err) + reporter.Fatal(wrappedErr) } app := cli.NewApp() app.Name = "kpm" diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index 5ec109c4..adc02f85 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -1,60 +1,64 @@ package errors import ( - "errors" + "fmt" ) -var FailedDownloadError = errors.New("failed to download dependency") -var CheckSumMismatchError = errors.New("checksum mismatch") -var FailedToVendorDependency = errors.New("failed to vendor dependency") -var FailedToPackage = errors.New("failed to package.") -var InvalidDependency = errors.New("invalid dependency.") -var InternalBug = errors.New("internal bug, please contact us and we will fix the problem.") -var FailedToLoadPackage = errors.New("failed to load package, please check the package path is valid.") +// KpmError is a custom error type for kpm +type KpmError struct { + Message string + Err error + Context string +} -// Invalid Options Format Errors -// Invalid 'kpm init' -var InvalidInitOptions = errors.New("invalid 'kpm init' argument, you must provide a name for the package to be initialized.") +func (e *KpmError) Error() string { + return fmt.Sprintf("%s: %s - %v", e.Message, e.Context, e.Err) +} -// Invalid 'kpm add' -var InvalidAddOptions = errors.New("invalid 'kpm add' argument, you must provide a package name or url for the package") -var InvalidAddOptionsInvalidGitUrl = errors.New("invalid 'kpm add' argument, you must provide a Git Url for the package.") -var InvalidAddOptionsInvalidOciRef = errors.New("invalid 'kpm add' argument, you must provide a valid Oci Ref for the package.") +func NewKpmError(message string, err error, context string) error { + return &KpmError{ + Message: message, + Err: err, + Context: context, + } +} -var InvalidAddOptionsInvalidOciReg = errors.New("invalid 'kpm add' argument, you must provide a Reg for the package.") -var InvalidAddOptionsInvalidOciRepo = errors.New("invalid 'kpm add' argument, you must provide a Repo for the package.") - -// Invalid 'kpm update' -var MultipleSources = errors.New("multiple sources found, there must be a single source.") - -// Invalid 'kpm run' -var InvalidRunOptionsWithoutEntryFiles = errors.New("invalid 'kpm run' argument, you must provide an entry file.") -var EntryFileNotFound = errors.New("entry file cannot be found, please make sure the '--input' entry file can be found") -var CompileFailed = errors.New("failed to compile kcl.") -var FailedUnTarKclPackage = errors.New("failed to untar kcl package, please re-download") -var UnknownTarFormat = errors.New("unknown tar format.") -var KclPacakgeTarNotFound = errors.New("the kcl package tar path is not found") -var InvalidKclPacakgeTar = errors.New("the kcl package tar path is an invalid *.tar file") - -// Invalid KCL_PKG_PATH -var InvalidKpmHomeInCurrentPkg = errors.New("environment variable KCL_PKG_PATH cannot be set to the same path as the current KCL package.") - -// Invalid oci -var FailedLogin = errors.New("failed to login, please check registry, username and password is valid.") -var FailedLogout = errors.New("failed to logout, the registry not logged in.") -var FailedPull = errors.New("failed to pull kcl package") -var FailedPushToOci = errors.New("failed to push kcl package tar to oci.") -var InvalidOciRef = errors.New("invalid oci reference.") -var NotOciUrl = errors.New("url is not an oci url.") -var IsOciRef = errors.New("oci ref is not an url.") - -// Invalid Version -var InvalidVersionFormat = errors.New("failed to parse version.") -var PathNotFound = errors.New("path not found.") -var PathIsEmpty = errors.New("path is empty.") -var InvalidPkg = errors.New("invalid kcl package.") -var InvalidOciUrl = errors.New("invalid oci url") -var UnknownEnv = errors.New("invalid environment variable") - -// No kcl files -var NoKclFiles = errors.New("No input KCL files") +// Error variables +var ( + FailedDownloadError = NewKpmError("Operation failed", fmt.Errorf("failed to download dependency"), "") + CheckSumMismatchError = NewKpmError("Operation failed", fmt.Errorf("checksum mismatch"), "") + FailedToVendorDependency = NewKpmError("Operation failed", fmt.Errorf("failed to vendor dependency"), "") + FailedToPackage = NewKpmError("Operation failed", fmt.Errorf("failed to package"), "") + InvalidDependency = NewKpmError("Operation failed", fmt.Errorf("invalid dependency"), "") + InternalBug = NewKpmError("Operation failed", fmt.Errorf("internal bug, please contact us and we will fix the problem"), "") + FailedToLoadPackage = NewKpmError("Operation failed", fmt.Errorf("failed to load package, please check the package path is valid"), "") + InvalidInitOptions = NewKpmError("Operation failed", fmt.Errorf("invalid 'kpm init' argument, you must provide a name for the package to be initialized"), "") + InvalidAddOptions = NewKpmError("Operation failed", fmt.Errorf("invalid 'kpm add' argument, you must provide a package name or url for the package"), "") + InvalidAddOptionsInvalidGitUrl = NewKpmError("Operation failed", fmt.Errorf("invalid 'kpm add' argument, you must provide a Git Url for the package"), "") + InvalidAddOptionsInvalidOciRef = NewKpmError("Operation failed", fmt.Errorf("invalid 'kpm add' argument, you must provide a valid Oci Ref for the package"), "") + InvalidAddOptionsInvalidOciReg = NewKpmError("Operation failed", fmt.Errorf("invalid 'kpm add' argument, you must provide a Reg for the package"), "") + InvalidAddOptionsInvalidOciRepo = NewKpmError("Operation failed", fmt.Errorf("invalid 'kpm add' argument, you must provide a Repo for the package"), "") + MultipleSources = NewKpmError("Operation failed", fmt.Errorf("multiple sources found, there must be a single source"), "") + InvalidRunOptionsWithoutEntryFiles = NewKpmError("Operation failed", fmt.Errorf("invalid 'kpm run' argument, you must provide an entry file"), "") + EntryFileNotFound = NewKpmError("Operation failed", fmt.Errorf("entry file cannot be found, please make sure the '--input' entry file can be found"), "") + CompileFailed = NewKpmError("Operation failed", fmt.Errorf("failed to compile kcl"), "") + FailedUnTarKclPackage = NewKpmError("Operation failed", fmt.Errorf("failed to untar kcl package, please re-download"), "") + UnknownTarFormat = NewKpmError("Operation failed", fmt.Errorf("unknown tar format"), "") + KclPacakgeTarNotFound = NewKpmError("Operation failed", fmt.Errorf("the kcl package tar path is not found"), "") + InvalidKclPacakgeTar = NewKpmError("Operation failed", fmt.Errorf("the kcl package tar path is an invalid *.tar file"), "") + InvalidKpmHomeInCurrentPkg = NewKpmError("Operation failed", fmt.Errorf("environment variable KCL_PKG_PATH cannot be set to the same path as the current KCL package"), "") + FailedLogin = NewKpmError("Operation failed", fmt.Errorf("failed to login, please check registry, username and password is valid"), "") + FailedLogout = NewKpmError("Operation failed", fmt.Errorf("failed to logout, the registry not logged in"), "") + FailedPull = NewKpmError("Operation failed", fmt.Errorf("failed to pull kcl package"), "") + FailedPushToOci = NewKpmError("Operation failed", fmt.Errorf("failed to push kcl package tar to oci"), "") + InvalidOciRef = NewKpmError("Operation failed", fmt.Errorf("invalid oci reference"), "") + NotOciUrl = NewKpmError("Operation failed", fmt.Errorf("url is not an oci url"), "") + IsOciRef = NewKpmError("Operation failed", fmt.Errorf("oci ref is not an url"), "") + InvalidVersionFormat = NewKpmError("Operation failed", fmt.Errorf("failed to parse version"), "") + PathNotFound = NewKpmError("Operation failed", fmt.Errorf("path not found"), "") + PathIsEmpty = NewKpmError("Operation failed", fmt.Errorf("path is empty"), "") + InvalidPkg = NewKpmError("Operation failed", fmt.Errorf("invalid kcl package"), "") + InvalidOciUrl = NewKpmError("Operation failed", fmt.Errorf("invalid oci url"), "") + UnknownEnv = NewKpmError("Operation failed", fmt.Errorf("invalid environment variable"), "") + NoKclFiles = NewKpmError("Operation failed", fmt.Errorf("No input KCL files"), "") +) diff --git a/pkg/reporter/reporter.go b/pkg/reporter/reporter.go index 247f7b78..5500e74b 100644 --- a/pkg/reporter/reporter.go +++ b/pkg/reporter/reporter.go @@ -5,36 +5,48 @@ package reporter import ( "fmt" "io" - "log" "os" "strings" + "kcl-lang.io/kpm/pkg/errors" + "github.com/sirupsen/logrus" ) // Init the log. func InitReporter() { - log.SetFlags(0) logrus.SetLevel(logrus.ErrorLevel) } // Report prints to the logger. // Arguments are handled in the manner of fmt.Println. func Report(v ...any) { - log.Println(v...) + logrus.Error(v...) } -// ExitWithReport prints to the logger and exit with 0. +// ExitWithReport prints to the logger and exits with 0. // Arguments are handled in the manner of fmt.Println. func ExitWithReport(v ...any) { - log.Println(v...) + logrus.Error(v...) os.Exit(0) } -// Fatal prints to the logger and exit with 1. +// Fatal prints to the logger and exits with 1. // Arguments are handled in the manner of fmt.Println. func Fatal(v ...any) { - log.Fatal(v...) + logrus.Error(v...) + os.Exit(1) +} + +// LogError logs the error using the centralized error logging system. +func LogError(err error) { + logrus.Error(err) +} + +// WrapError wraps the given error with a context message and logs it. +func WrapError(context string, err error) error { + wrappedErr := errors.NewKpmError("Operation failed", err, context) + return wrappedErr } // Event is the interface that specifies the event used to show logs to users.