git » chasquid » commit b1fe4f8

config: Improve logging of errors

author Alberto Bertogli
2020-05-16 22:46:43 UTC
committer Alberto Bertogli
2020-05-16 22:46:43 UTC
parent 50986a7b7ef1d476a5a920502faed192908191be

config: Improve logging of errors

Currently, the config package logs errors itself, in addition to
returning them.

That is confusing and results in some duplication of logging.

This patch makes config just return errors, and adjusts the callers
to log them properly.

chasquid.go +1 -1
cmd/chasquid-util/chasquid-util.go +4 -4
internal/config/config.go +4 -7

diff --git a/chasquid.go b/chasquid.go
index 39789c9..f992c0b 100644
--- a/chasquid.go
+++ b/chasquid.go
@@ -72,7 +72,7 @@ func main() {
 
 	conf, err := config.Load(*configDir + "/chasquid.conf")
 	if err != nil {
-		log.Fatalf("Error reading config: %v", err)
+		log.Fatalf("Error loading config: %v", err)
 	}
 	config.LogConfig(conf)
 
diff --git a/cmd/chasquid-util/chasquid-util.go b/cmd/chasquid-util/chasquid-util.go
index e01c50d..fa4be3d 100644
--- a/cmd/chasquid-util/chasquid-util.go
+++ b/cmd/chasquid-util/chasquid-util.go
@@ -206,7 +206,7 @@ func userRemove() {
 func aliasesResolve() {
 	conf, err := config.Load(configDir + "/chasquid.conf")
 	if err != nil {
-		Fatalf("Error reading config")
+		Fatalf("Error loading config: %v", err)
 	}
 	_ = os.Chdir(configDir)
 
@@ -250,7 +250,7 @@ func aliasesResolve() {
 func printConfig() {
 	conf, err := config.Load(configDir + "/chasquid.conf")
 	if err != nil {
-		Fatalf("Error reading config")
+		Fatalf("Error loading config: %v", err)
 	}
 
 	fmt.Println(prototext.Format(conf))
@@ -262,7 +262,7 @@ func domaininfoRemove() {
 
 	conf, err := config.Load(configDir + "/chasquid.conf")
 	if err != nil {
-		Fatalf("Error reading config")
+		Fatalf("Error loading config: %v", err)
 	}
 
 	// File for the corresponding domain.
@@ -292,7 +292,7 @@ func aliasesAdd() {
 
 	conf, err := config.Load(configDir + "/chasquid.conf")
 	if err != nil {
-		Fatalf("Error reading config")
+		Fatalf("Error loading config: %v", err)
 	}
 	_ = os.Chdir(configDir)
 
diff --git a/internal/config/config.go b/internal/config/config.go
index 2b72786..9a01d91 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -5,6 +5,7 @@ package config
 //go:generate protoc --go_out=. --go_opt=paths=source_relative config.proto
 
 import (
+	"fmt"
 	"io/ioutil"
 	"os"
 
@@ -19,15 +20,12 @@ func Load(path string) (*Config, error) {
 
 	buf, err := ioutil.ReadFile(path)
 	if err != nil {
-		log.Errorf("Failed to read config at %q", path)
-		log.Errorf("  (%v)", err)
-		return nil, err
+		return nil, fmt.Errorf("failed to read config at %q: %v", path, err)
 	}
 
 	err = prototext.Unmarshal(buf, c)
 	if err != nil {
-		log.Errorf("Error parsing config: %v", err)
-		return nil, err
+		return nil, fmt.Errorf("parsing config: %v", err)
 	}
 
 	// Fill in defaults for anything that's missing.
@@ -35,8 +33,7 @@ func Load(path string) (*Config, error) {
 	if c.Hostname == "" {
 		c.Hostname, err = os.Hostname()
 		if err != nil {
-			log.Errorf("Could not get hostname: %v", err)
-			return nil, err
+			return nil, fmt.Errorf("could not get hostname: %v", err)
 		}
 	}