Skip to content
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

DO NOT MERGE (needs review and testing). Break error into parts #715

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
e "github.com/cloudposse/atmos/internal/exec"
tuiUtils "github.com/cloudposse/atmos/internal/tui/utils"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)
Expand Down Expand Up @@ -71,7 +72,8 @@ func Execute() error {
if err != nil && !errors.Is(err, cfg.NotFound) {
u.LogErrorAndExit(schema.CliConfiguration{}, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
u.LogErrorAndExit(schema.CliConfiguration{}, err)
u.LogErrorAndExit(cliConfig, err)

We have cliConfig, let's use it (I missed this line in the previous PR)

}

// Set the log level for IError interface
logger.SetPrintDebugPart(cliConfig.Logs.Level)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

LGTM: Setting log level for IError interface.

The new line correctly sets the log level for the IError interface using the CLI configuration. This aligns with the PR objective of enhancing error handling and adjusting log levels.

However, consider adding error handling for the SetPrintDebugPart function call to ensure robustness.

Consider updating the code as follows:

 // Set the log level for IError interface
-logger.SetPrintDebugPart(cliConfig.Logs.Level)
+if err := logger.SetPrintDebugPart(cliConfig.Logs.Level); err != nil {
+    u.LogErrorAndExit(err)
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Set the log level for IError interface
logger.SetPrintDebugPart(cliConfig.Logs.Level)
// Set the log level for IError interface
if err := logger.SetPrintDebugPart(cliConfig.Logs.Level); err != nil {
u.LogErrorAndExit(err)
}

// If CLI configuration was found, process its custom commands and command aliases
if err == nil {
err = processCustomCommands(cliConfig, cliConfig.Commands, RootCmd, true)
Expand Down
12 changes: 10 additions & 2 deletions pkg/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"

"github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)
Expand Down Expand Up @@ -41,7 +42,10 @@ func FindAllStackConfigsInPathsForStack(
matches, err = u.GetGlobMatches(pathWithExt)
if err != nil {
y, _ := u.ConvertToYAML(cliConfig)
return nil, nil, false, fmt.Errorf("%v\n\n\nCLI config:\n\n%v", err, y)
return nil, nil, false, &logger.IError{
Message: err.Error(),
DebugInfo: fmt.Sprintf("CLI config:\n\n%v", y),
}
haitham911 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -119,7 +123,11 @@ func FindAllStackConfigsInPaths(
matches, err = u.GetGlobMatches(pathWithExt)
if err != nil {
y, _ := u.ConvertToYAML(cliConfig)
return nil, nil, fmt.Errorf("%v\n\n\nCLI config:\n\n%v", err, y)
// Return an error with the debug info
return nil, nil, &logger.IError{
Message: err.Error(),
DebugInfo: fmt.Sprintf("CLI config:\n\n%v", y),
}
haitham911 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ import (
"github.com/cloudposse/atmos/pkg/schema"
)

var PrintDebugPart = false
haitham911 marked this conversation as resolved.
Show resolved Hide resolved

// set PrintDebugPart to true if log level is Debug or Trace
func SetPrintDebugPart(val string) {
if val == string(LogLevelDebug) || val == string(LogLevelTrace) {
PrintDebugPart = true
}
}
haitham911 marked this conversation as resolved.
Show resolved Hide resolved

type IError struct {
Message string
DebugInfo string
}

func (e *IError) Error() string {
// Print debug info if PrintDebugPart is true
if PrintDebugPart {
return fmt.Sprintf("%s\n%s", e.Message, e.DebugInfo)
}
return e.Message
}
haitham911 marked this conversation as resolved.
Show resolved Hide resolved

type LogLevel string

const (
Expand Down
Loading