mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
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>
This commit is contained in:
co-authored by
Abhisek Datta
parent
e5fe0df82e
commit
20e01d5cae
+22
-21
@@ -4,7 +4,8 @@ import (
|
||||
"github.com/safedep/dry/cloud"
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/pmg/usefulerror"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -29,70 +30,70 @@ func runLogin(cmd *cobra.Command, args []string) error {
|
||||
if loginFromEnv {
|
||||
resolver, err := cloud.NewEnvCredentialResolver()
|
||||
if err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Failed to create environment credential resolver"))
|
||||
}
|
||||
|
||||
creds, err := resolver.Resolve()
|
||||
if err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeInvalidArgument).
|
||||
WithCode(errcodes.InvalidArgument).
|
||||
WithHumanError("Failed to resolve credentials from environment").
|
||||
WithHelp("Set SAFEDEP_API_KEY and SAFEDEP_TENANT_ID environment variables"))
|
||||
}
|
||||
|
||||
apiKey, err = creds.GetAPIKey()
|
||||
if err != nil || apiKey == "" {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
WithCode(usefulerror.ErrCodeInvalidArgument).
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
WithCode(errcodes.InvalidArgument).
|
||||
WithHumanError("SAFEDEP_API_KEY environment variable is not set"))
|
||||
}
|
||||
|
||||
tenantID, err = creds.GetTenantDomain()
|
||||
if err != nil || tenantID == "" {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
WithCode(usefulerror.ErrCodeInvalidArgument).
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
WithCode(errcodes.InvalidArgument).
|
||||
WithHumanError("SAFEDEP_TENANT_ID environment variable is not set"))
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
tenantID, err = ui.PromptInput("Tenant ID: ")
|
||||
if err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Failed to read Tenant ID"))
|
||||
}
|
||||
|
||||
if tenantID == "" {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
WithCode(usefulerror.ErrCodeInvalidArgument).
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
WithCode(errcodes.InvalidArgument).
|
||||
WithHumanError("Tenant ID cannot be empty"))
|
||||
}
|
||||
|
||||
apiKey, err = ui.PromptSecret("API Key: ")
|
||||
if err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Failed to read API Key"))
|
||||
}
|
||||
|
||||
if apiKey == "" {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
WithCode(usefulerror.ErrCodeInvalidArgument).
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
WithCode(errcodes.InvalidArgument).
|
||||
WithHumanError("API Key cannot be empty"))
|
||||
}
|
||||
}
|
||||
|
||||
store, err := cloud.NewKeychainCredentialStore()
|
||||
if err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Failed to initialize credential store").
|
||||
WithHelp("Your system may not support secure credential storage"))
|
||||
}
|
||||
@@ -103,9 +104,9 @@ func runLogin(cmd *cobra.Command, args []string) error {
|
||||
}()
|
||||
|
||||
if err := store.SaveAPIKeyCredential(apiKey, tenantID); err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Failed to save credentials").
|
||||
WithHelp("Your system may not support secure credential storage"))
|
||||
}
|
||||
|
||||
+6
-5
@@ -4,7 +4,8 @@ import (
|
||||
"github.com/safedep/dry/cloud"
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/pmg/usefulerror"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -19,9 +20,9 @@ func newLogoutCommand() *cobra.Command {
|
||||
func runLogout(cmd *cobra.Command, args []string) error {
|
||||
store, err := cloud.NewKeychainCredentialStore()
|
||||
if err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Failed to initialize credential store").
|
||||
WithHelp("Your system may not support secure credential storage"))
|
||||
}
|
||||
@@ -32,9 +33,9 @@ func runLogout(cmd *cobra.Command, args []string) error {
|
||||
}()
|
||||
|
||||
if err := store.Clear(); err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Failed to clear credentials"))
|
||||
}
|
||||
|
||||
|
||||
+12
-11
@@ -9,7 +9,8 @@ import (
|
||||
"github.com/safedep/pmg/internal/analytics"
|
||||
"github.com/safedep/pmg/internal/audit"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/pmg/usefulerror"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -42,8 +43,8 @@ func runSync(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if !cfg.Config.Cloud.Enabled {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Cloud sync is not enabled").
|
||||
WithHelp("Set 'cloud.enabled: true' in PMG config to enable cloud sync"))
|
||||
}
|
||||
@@ -54,15 +55,15 @@ func runSync(cmd *cobra.Command, args []string) error {
|
||||
|
||||
locked, err := lock.TryLockContext(lockCtx, 250*time.Millisecond)
|
||||
if err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Failed to acquire cloud sync lock").
|
||||
WithHelp("Another sync may be in progress; try again shortly"))
|
||||
}
|
||||
if !locked {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Another cloud sync is already in progress").
|
||||
WithHelp("Wait for the in-progress sync to finish, then try again"))
|
||||
}
|
||||
@@ -77,9 +78,9 @@ func runSync(cmd *cobra.Command, args []string) error {
|
||||
|
||||
bundle, err := audit.NewSyncClientBundle(cfg)
|
||||
if err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeLifecycle).
|
||||
WithCode(errcodes.Lifecycle).
|
||||
WithHumanError("Failed to initialize cloud sync client").
|
||||
WithHelp("Run 'pmg cloud login' to store credentials, or set SAFEDEP_API_KEY and SAFEDEP_TENANT_ID environment variables"))
|
||||
}
|
||||
@@ -92,9 +93,9 @@ func runSync(cmd *cobra.Command, args []string) error {
|
||||
synced, err := bundle.Sync(ctx)
|
||||
recordLastSyncAttempt(cfg)
|
||||
if err != nil {
|
||||
ui.ErrorExit(usefulerror.Useful().
|
||||
ui.ErrorExit(usefulerror.NewUsefulError().
|
||||
Wrap(err).
|
||||
WithCode(usefulerror.ErrCodeNetwork).
|
||||
WithCode(errcodes.Network).
|
||||
WithHumanError("Failed to sync events to SafeDep Cloud").
|
||||
WithHelp("Check your network connectivity and ensure SafeDep Cloud is reachable").
|
||||
WithAdditionalHelp("Override the cloud endpoint with SAFEDEP_CLOUD_DATA_ADDR if needed"))
|
||||
|
||||
Reference in New Issue
Block a user