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

fail if an error log has occured #41

Merged
merged 4 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cmd/klotho/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ var cfg struct {
}

var hadWarnings = atomic.NewBool(false)
var hadErrors = atomic.NewBool(false)

func init() {
err := zap.RegisterEncoder("klotho-cli", func(zcfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
return logging.NewConsoleEncoder(cfg.verbose, hadWarnings), nil
return logging.NewConsoleEncoder(cfg.verbose, hadWarnings, hadErrors), nil
})

if err != nil {
Expand Down Expand Up @@ -80,6 +81,7 @@ func main() {
flags.BoolVar(&cfg.version, "version", false, "Print the version")
flags.BoolVar(&cfg.update, "update", false, "update the cli to the latest version")
flags.StringVar(&cfg.login, "login", "", "Login to Klotho with email. For anonymous login, use 'local'")
_ = flags.MarkHidden("internalDebug")

err := root.Execute()
if err != nil {
Expand All @@ -88,6 +90,7 @@ func main() {
} else if !root.SilenceErrors {
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this regress the silence errors (which we set on line 270 so it doesn't double-print)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, we still set that to make sure that we dont double print anything

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah we actually will want this so that the non plugin errors surface, going to add it back

zap.S().Errorf("%v", err)
}
zap.S().Error("Klotho compilation failed")
os.Exit(1)
}
if hadWarnings.Load() && cfg.strict {
Expand Down Expand Up @@ -264,8 +267,12 @@ func run(cmd *cobra.Command, args []string) (err error) {
analyticsClient.Info("klotho compiling")

result, err := compiler.Compile(input)
if err != nil {
errHandler.PrintErr(err)
if err != nil || hadErrors.Load() {
if err != nil {
errHandler.PrintErr(err)
} else {
err = errors.New("Failed run of klotho invocation")
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this should go similar to the hadWarnings check (line 94)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that results in a double print as well though since we already log it. i think if we are treating error log lines and errors the same, having them go through the same flow is fine, it then treats zap errors as actual errors

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we would also still print the output then if we did it that way which i thought this task is trying to prevent

Copy link
Contributor

Choose a reason for hiding this comment

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

True, we do want to prevent writing the output. I guess this error wouldn't get printed since we don't explicitly print it (like we do for err != nil) and also set SilenceErrors. 👍

}
analyticsClient.Error("klotho compiling failed")

cmd.SilenceErrors = true
Expand Down
13 changes: 9 additions & 4 deletions pkg/logging/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ type ConsoleEncoder struct {
Annotation annotationField
Node astNodeField
HadWarnings *atomic.Bool
HadErrors *atomic.Bool

*bufferEncoder
}

func NewConsoleEncoder(verbose bool, hadWarnings *atomic.Bool) *ConsoleEncoder {
func NewConsoleEncoder(verbose bool, hadWarnings *atomic.Bool, hadErrors *atomic.Bool) *ConsoleEncoder {
return &ConsoleEncoder{
Verbose: verbose,
HadWarnings: hadWarnings,
HadErrors: hadErrors,
bufferEncoder: &bufferEncoder{b: pool.Get()},
}
}
Expand All @@ -68,9 +70,9 @@ func (enc *ConsoleEncoder) Clone() zapcore.Encoder {
bufferEncoder: &bufferEncoder{b: pool.Get()},
Verbose: enc.Verbose,
HadWarnings: enc.HadWarnings,

File: enc.File,
Annotation: enc.Annotation,
HadErrors: enc.HadErrors,
File: enc.File,
Annotation: enc.Annotation,
}
_, _ = ne.bufferEncoder.b.Write(enc.b.Bytes())

Expand Down Expand Up @@ -110,6 +112,9 @@ func (enc *ConsoleEncoder) EncodeEntry(ent zapcore.Entry, fieldList []zapcore.Fi
if ent.Level >= zapcore.WarnLevel {
enc.HadWarnings.Store(true)
}
if ent.Level >= zapcore.ErrorLevel {
enc.HadErrors.Store(true)
}

var (
file = enc.File.f
Expand Down