From 2e3cac7330e991909d41a1da5be8d5ce625a3b6d Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Mon, 4 Dec 2023 22:13:57 +0100 Subject: [PATCH] Don't panic --- config/config.go | 30 +++++++++++++++--------------- logging/logger.go | 4 ++-- logging/logger_windows.go | 4 ++-- tracing/tracing.go | 5 ++--- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/config/config.go b/config/config.go index e98b9109..591d7fa7 100644 --- a/config/config.go +++ b/config/config.go @@ -165,7 +165,7 @@ func (c *Config) LoadDefaults(ctx context.Context) { if err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to unmarshal global configuration: %w", err)) + log.Fatal(fmt.Errorf("failed to unmarshal global configuration: %w", err)) } for configObject, configMap := range gconf { @@ -194,7 +194,7 @@ func (c *Config) LoadDefaults(ctx context.Context) { err := fmt.Errorf("unknown config object: %s", configObject) span.RecordError(err) span.End() - log.Panic(err) + log.Fatal(err) } } } @@ -202,7 +202,7 @@ func (c *Config) LoadDefaults(ctx context.Context) { } else if !os.IsNotExist(err) { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to read global configuration file: %w", err)) + log.Fatal(fmt.Errorf("failed to read global configuration file: %w", err)) } c.pluginDefaults = PluginConfig{ @@ -222,7 +222,7 @@ func (c *Config) LoadDefaults(ctx context.Context) { if err := c.GlobalKoanf.Load(structs.Provider(c.globalDefaults, "json"), nil); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to load default global configuration: %w", err)) + log.Fatal(fmt.Errorf("failed to load default global configuration: %w", err)) } } @@ -230,7 +230,7 @@ func (c *Config) LoadDefaults(ctx context.Context) { if err := c.PluginKoanf.Load(structs.Provider(c.pluginDefaults, "json"), nil); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to load default plugin configuration: %w", err)) + log.Fatal(fmt.Errorf("failed to load default plugin configuration: %w", err)) } } @@ -245,7 +245,7 @@ func (c *Config) LoadGlobalEnvVars(ctx context.Context) { if err := c.GlobalKoanf.Load(loadEnvVars(), nil); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to load environment variables: %w", err)) + log.Fatal(fmt.Errorf("failed to load environment variables: %w", err)) } span.End() @@ -259,7 +259,7 @@ func (c *Config) LoadPluginEnvVars(ctx context.Context) { if err := c.PluginKoanf.Load(loadEnvVars(), nil); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to load environment variables: %w", err)) + log.Fatal(fmt.Errorf("failed to load environment variables: %w", err)) } span.End() @@ -278,7 +278,7 @@ func (c *Config) LoadGlobalConfigFile(ctx context.Context) { if err := c.GlobalKoanf.Load(file.Provider(c.globalConfigFile), yaml.Parser()); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to load global configuration: %w", err)) + log.Fatal(fmt.Errorf("failed to load global configuration: %w", err)) } span.End() @@ -291,7 +291,7 @@ func (c *Config) LoadPluginConfigFile(ctx context.Context) { if err := c.PluginKoanf.Load(file.Provider(c.pluginConfigFile), yaml.Parser()); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to load plugin configuration: %w", err)) + log.Fatal(fmt.Errorf("failed to load plugin configuration: %w", err)) } span.End() @@ -306,7 +306,7 @@ func (c *Config) UnmarshalGlobalConfig(ctx context.Context) { }); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to unmarshal global configuration: %w", err)) + log.Fatal(fmt.Errorf("failed to unmarshal global configuration: %w", err)) } span.End() @@ -321,7 +321,7 @@ func (c *Config) UnmarshalPluginConfig(ctx context.Context) { }); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to unmarshal plugin configuration: %w", err)) + log.Fatal(fmt.Errorf("failed to unmarshal plugin configuration: %w", err)) } span.End() @@ -335,7 +335,7 @@ func (c *Config) MergeGlobalConfig( if err := c.GlobalKoanf.Load(confmap.Provider(updatedGlobalConfig, "."), nil); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to merge global configuration: %w", err)) + log.Fatal(fmt.Errorf("failed to merge global configuration: %w", err)) } if err := c.GlobalKoanf.UnmarshalWithConf("", &c.Global, koanf.UnmarshalConf{ @@ -343,7 +343,7 @@ func (c *Config) MergeGlobalConfig( }); err != nil { span.RecordError(err) span.End() - log.Panic(fmt.Errorf("failed to unmarshal global configuration: %w", err)) + log.Fatal(fmt.Errorf("failed to unmarshal global configuration: %w", err)) } span.End() @@ -356,7 +356,7 @@ func (c *Config) ValidateGlobalConfig(ctx context.Context) { if err := c.GlobalKoanf.Unmarshal("", &globalConfig); err != nil { span.RecordError(err) span.End() - log.Panic( + log.Fatal( gerr.ErrValidationFailed.Wrap( fmt.Errorf("failed to unmarshal global configuration: %w", err)), ) @@ -466,6 +466,6 @@ func (c *Config) ValidateGlobalConfig(ctx context.Context) { } span.RecordError(fmt.Errorf("failed to validate global configuration")) span.End() - log.Panic("failed to validate global configuration") + log.Fatal("failed to validate global configuration") } } diff --git a/logging/logger.go b/logging/logger.go index c7dfa7fa..c52087f7 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -73,7 +73,7 @@ func NewLogger(ctx context.Context, cfg LoggerConfig) zerolog.Logger { if err != nil { span.RecordError(err) span.End() - log.Panic(err) + log.Fatal(err) } outputs = append(outputs, syslogWriter) case config.RSyslog: @@ -82,7 +82,7 @@ func NewLogger(ctx context.Context, cfg LoggerConfig) zerolog.Logger { rsyslogWriter, err := syslog.Dial( cfg.RSyslogNetwork, cfg.RSyslogAddress, cfg.SyslogPriority, config.DefaultSyslogTag) if err != nil { - log.Panic(err) + log.Fatal(err) } outputs = append(outputs, zerolog.SyslogLevelWriter(rsyslogWriter)) default: diff --git a/logging/logger_windows.go b/logging/logger_windows.go index 8a9ca21e..54a3de52 100644 --- a/logging/logger_windows.go +++ b/logging/logger_windows.go @@ -68,9 +68,9 @@ func NewLogger(ctx context.Context, cfg LoggerConfig) zerolog.Logger { }, ) case config.Syslog: - log.Panic("Syslog is not supported on Windows") + log.Fatal("Syslog is not supported on Windows") case config.RSyslog: - log.Panic("RSyslog is not supported on Windows") + log.Fatal("RSyslog is not supported on Windows") default: outputs = append(outputs, consoleWriter) } diff --git a/tracing/tracing.go b/tracing/tracing.go index 9103f2a9..72b1fdf1 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -28,7 +28,7 @@ func OTLPTracer(insecure bool, collectorURL, serviceName string) func(context.Co ), ) if err != nil { - log.Panic(err) + log.Fatal(err) } resources, err := resource.New( @@ -40,8 +40,7 @@ func OTLPTracer(insecure bool, collectorURL, serviceName string) func(context.Co ), ) if err != nil { - // logger.Error().Err(err).Msg("Could not set resources") - log.Panic(err) + log.Fatal(err) } resources, _ = resource.Merge(