From 4895f963d59198b81e7d8a8a7f7f66986fa195c0 Mon Sep 17 00:00:00 2001 From: Vadims Gedzs Date: Thu, 3 Aug 2023 17:52:02 +0300 Subject: [PATCH 1/2] fix(config): correct error handling for envConfig --- cmd/argo-watcher/config/config.go | 14 +++++++++++--- docs/development.md | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/argo-watcher/config/config.go b/cmd/argo-watcher/config/config.go index 33b02bfe..198e4deb 100644 --- a/cmd/argo-watcher/config/config.go +++ b/cmd/argo-watcher/config/config.go @@ -2,10 +2,10 @@ package config import ( "errors" + "github.com/shini4i/argo-watcher/internal/helpers" "strconv" envConfig "github.com/kelseyhightower/envconfig" - "github.com/shini4i/argo-watcher/internal/helpers" ) type ServerConfig struct { @@ -35,13 +35,21 @@ type ServerConfig struct { // Otherwise, it returns the parsed server configuration and any error encountered during the parsing process. func NewServerConfig() (*ServerConfig, error) { // parse config - var config ServerConfig - err := envConfig.Process("", &config) + var ( + err error + config ServerConfig + ) + + if err := envConfig.Process("", &config); err != nil { + panic(err) + } + // custom checks allowedTypes := []string{"postgres", "in-memory"} if config.StateType == "" || !helpers.Contains(allowedTypes, config.StateType) { return nil, errors.New("variable STATE_TYPE must be one of [\"postgres\", \"in-memory\"]") } + // return config return &config, err } diff --git a/docs/development.md b/docs/development.md index ad5924e7..43575968 100644 --- a/docs/development.md +++ b/docs/development.md @@ -63,7 +63,7 @@ cd cmd/argo-watcher # install dependencies go mod tidy # start argo-watcher -ARGO_URL=http://localhost:8081 STATE_TYPE=in-memory go run . +ARGO_URL=http://localhost:8081 STATE_TYPE=in-memory go run . -server ``` ### Running the unit tests From 8c8e19d9ae59f774f8bb08c4fae3234cf5d91905 Mon Sep 17 00:00:00 2001 From: Vadims Gedzs Date: Thu, 3 Aug 2023 18:00:47 +0300 Subject: [PATCH 2/2] fix: panic -> return err --- cmd/argo-watcher/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/argo-watcher/config/config.go b/cmd/argo-watcher/config/config.go index 198e4deb..985ff84a 100644 --- a/cmd/argo-watcher/config/config.go +++ b/cmd/argo-watcher/config/config.go @@ -41,7 +41,7 @@ func NewServerConfig() (*ServerConfig, error) { ) if err := envConfig.Process("", &config); err != nil { - panic(err) + return nil, err } // custom checks