-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
f19ad69
d36cbf0
92678a9
83674d1
2bc991d
a08d257
4496b1c
f2ba630
0ec4c03
f7cc62e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -71,7 +71,12 @@ func Execute() error { | |||||
if err != nil && !errors.Is(err, cfg.NotFound) { | ||||||
u.LogErrorAndExit(schema.CliConfiguration{}, err) | ||||||
} | ||||||
|
||||||
// Set the log level for ExtendedError interface . must be set on root level to avoid missing log level | ||||||
if cliConfig.Logs.Level != "" { | ||||||
if err := u.SetPrintDebugPart(cliConfig.Logs.Level); err != nil { | ||||||
u.LogErrorAndExit(schema.CliConfiguration{}, err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We already have |
||||||
} | ||||||
} | ||||||
// If CLI configuration was found, process its custom commands and command aliases | ||||||
if err == nil { | ||||||
err = processCustomCommands(cliConfig, cliConfig.Commands, RootCmd, true) | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |||||
"os" | ||||||
"os/exec" | ||||||
"runtime/debug" | ||||||
"sync" | ||||||
|
||||||
"github.com/fatih/color" | ||||||
|
||||||
|
@@ -19,6 +20,41 @@ const ( | |||||
LogLevelWarning = "Warning" | ||||||
) | ||||||
|
||||||
var ( | ||||||
PrintDebugPart bool = false | ||||||
mu sync.Mutex | ||||||
) | ||||||
|
||||||
// set PrintDebugPart to true if log level is Debug or Trace | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
In |
||||||
func SetPrintDebugPart(val string) error { | ||||||
mu.Lock() | ||||||
haitham911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
defer mu.Unlock() | ||||||
if val == "" { | ||||||
return errors.New("log level not set") | ||||||
} | ||||||
if val == LogLevelDebug || val == LogLevelTrace { | ||||||
PrintDebugPart = true | ||||||
} else { | ||||||
PrintDebugPart = false | ||||||
} | ||||||
return nil | ||||||
} | ||||||
aknysh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
// ExtendedError is an error type that includes a message and debug info. | ||||||
type ExtendedError struct { | ||||||
Message string // Error message to be printed with log level Error , Warning or Info | ||||||
aknysh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
DebugInfo string // Debug info to be printed with log level Debug or Trace | ||||||
} | ||||||
|
||||||
// Error returns the error message . If PrintDebugPart is true, it returns the error message and debug info | ||||||
aknysh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
func (e *ExtendedError) Error() string { | ||||||
// Print debug info if PrintDebugPart is true | ||||||
if PrintDebugPart { | ||||||
return fmt.Sprintf("%s\n%s", e.Message, e.DebugInfo) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use These two parts e.Message and e.DebugInfo should be printed in diff colors (this is the main point of this PR) |
||||||
} | ||||||
return e.Message | ||||||
} | ||||||
aknysh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
aknysh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// PrintMessage prints the message to the console | ||||||
func PrintMessage(message string) { | ||||||
fmt.Println(message) | ||||||
|
@@ -48,6 +84,12 @@ func LogErrorAndExit(cliConfig schema.CliConfiguration, err error) { | |||||
// LogError logs errors to std.Error | ||||||
func LogError(cliConfig schema.CliConfiguration, err error) { | ||||||
if err != nil { | ||||||
// set PrintDebugPart to true if log level is Debug or Trace | ||||||
if cliConfig.Logs.Level != "" { | ||||||
if setErr := SetPrintDebugPart(cliConfig.Logs.Level); setErr != nil { | ||||||
color.Red("%s\n", setErr) | ||||||
} | ||||||
} | ||||||
aknysh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
c := color.New(color.FgRed) | ||||||
_, printErr := c.Fprintln(color.Error, err.Error()+"\n") | ||||||
if printErr != nil { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have
cliConfig
, let's use it (I missed this line in the previous PR)