From 6b1916a529c5368d47f0d6a11570bfcedfb4ebd9 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 31 May 2021 08:11:23 +0300 Subject: [PATCH] [#552] cmd/node: Implement error checker with details Implement function `fatalOnErrDetails` similar to `fatalOnErr` but accepting string details that are written to log output. Use the function everywhere in application code without wrapping in an if-else statement. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/grpc.go | 4 +--- cmd/neofs-node/main.go | 9 +++++++++ cmd/neofs-node/netmap.go | 13 +++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/neofs-node/grpc.go b/cmd/neofs-node/grpc.go index 6f7205a33..588df3073 100644 --- a/cmd/neofs-node/grpc.go +++ b/cmd/neofs-node/grpc.go @@ -22,9 +22,7 @@ func initGRPC(c *cfg) { if c.cfgGRPC.tlsEnabled { creds, err := credentials.NewServerTLSFromFile(c.cfgGRPC.tlsCertFile, c.cfgGRPC.tlsKeyFile) - if err != nil { - fatalOnErr(fmt.Errorf("could not read credentionals from file: %w", err)) - } + fatalOnErrDetails("could not read credentials from file", err) serverOpts = append(serverOpts, grpc.Creds(creds)) } diff --git a/cmd/neofs-node/main.go b/cmd/neofs-node/main.go index 50350b1a4..70f891f22 100644 --- a/cmd/neofs-node/main.go +++ b/cmd/neofs-node/main.go @@ -3,6 +3,7 @@ package main import ( "context" "flag" + "fmt" "log" "github.com/nspcc-dev/neofs-node/misc" @@ -11,12 +12,20 @@ import ( "go.uber.org/zap" ) +// prints err to standard logger and calls os.Exit(1). func fatalOnErr(err error) { if err != nil { log.Fatal(err) } } +// prints err with details to standard logger and calls os.Exit(1). +func fatalOnErrDetails(details string, err error) { + if err != nil { + log.Fatal(fmt.Errorf("%s: %w", details, err)) + } +} + func main() { configFile := flag.String("config", "", "path to config") flag.Parse() diff --git a/cmd/neofs-node/netmap.go b/cmd/neofs-node/netmap.go index 216aeb3dd..249e6f181 100644 --- a/cmd/neofs-node/netmap.go +++ b/cmd/neofs-node/netmap.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "fmt" netmapSDK "github.com/nspcc-dev/neofs-api-go/pkg/netmap" netmapV2 "github.com/nspcc-dev/neofs-api-go/v2/netmap" @@ -111,9 +110,7 @@ func bootstrapNode(c *cfg) { initState(c) err := c.cfgNetmap.wrapper.AddPeer(c.toOnlineLocalNodeInfo()) - if err != nil { - fatalOnErr(fmt.Errorf("bootstrap error: %w", err)) - } + fatalOnErrDetails("bootstrap error", err) } func addNetmapNotificationHandler(c *cfg, sTyp string, h event.Handler) { @@ -138,14 +135,10 @@ func setNetmapNotificationParser(c *cfg, sTyp string, p event.Parser) { func initState(c *cfg) { epoch, err := c.cfgNetmap.wrapper.Epoch() - if err != nil { - fatalOnErr(fmt.Errorf("could not initialize current epoch number: %w", err)) - } + fatalOnErrDetails("could not initialize current epoch number", err) ni, err := c.netmapLocalNodeState(epoch) - if err != nil { - fatalOnErr(fmt.Errorf("could not init network state: %w", err)) - } + fatalOnErrDetails("could not init network state", err) c.handleNodeInfoStatus(ni)