[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-31 08:11:23 +03:00 committed by Leonard Lyubich
parent ead4513feb
commit 6b1916a529
3 changed files with 13 additions and 13 deletions

View file

@ -22,9 +22,7 @@ func initGRPC(c *cfg) {
if c.cfgGRPC.tlsEnabled { if c.cfgGRPC.tlsEnabled {
creds, err := credentials.NewServerTLSFromFile(c.cfgGRPC.tlsCertFile, c.cfgGRPC.tlsKeyFile) creds, err := credentials.NewServerTLSFromFile(c.cfgGRPC.tlsCertFile, c.cfgGRPC.tlsKeyFile)
if err != nil { fatalOnErrDetails("could not read credentials from file", err)
fatalOnErr(fmt.Errorf("could not read credentionals from file: %w", err))
}
serverOpts = append(serverOpts, grpc.Creds(creds)) serverOpts = append(serverOpts, grpc.Creds(creds))
} }

View file

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"flag" "flag"
"fmt"
"log" "log"
"github.com/nspcc-dev/neofs-node/misc" "github.com/nspcc-dev/neofs-node/misc"
@ -11,12 +12,20 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
// prints err to standard logger and calls os.Exit(1).
func fatalOnErr(err error) { func fatalOnErr(err error) {
if err != nil { if err != nil {
log.Fatal(err) 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() { func main() {
configFile := flag.String("config", "", "path to config") configFile := flag.String("config", "", "path to config")
flag.Parse() flag.Parse()

View file

@ -2,7 +2,6 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
netmapSDK "github.com/nspcc-dev/neofs-api-go/pkg/netmap" netmapSDK "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
netmapV2 "github.com/nspcc-dev/neofs-api-go/v2/netmap" netmapV2 "github.com/nspcc-dev/neofs-api-go/v2/netmap"
@ -111,9 +110,7 @@ func bootstrapNode(c *cfg) {
initState(c) initState(c)
err := c.cfgNetmap.wrapper.AddPeer(c.toOnlineLocalNodeInfo()) err := c.cfgNetmap.wrapper.AddPeer(c.toOnlineLocalNodeInfo())
if err != nil { fatalOnErrDetails("bootstrap error", err)
fatalOnErr(fmt.Errorf("bootstrap error: %w", err))
}
} }
func addNetmapNotificationHandler(c *cfg, sTyp string, h event.Handler) { 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) { func initState(c *cfg) {
epoch, err := c.cfgNetmap.wrapper.Epoch() epoch, err := c.cfgNetmap.wrapper.Epoch()
if err != nil { fatalOnErrDetails("could not initialize current epoch number", err)
fatalOnErr(fmt.Errorf("could not initialize current epoch number: %w", err))
}
ni, err := c.netmapLocalNodeState(epoch) ni, err := c.netmapLocalNodeState(epoch)
if err != nil { fatalOnErrDetails("could not init network state", err)
fatalOnErr(fmt.Errorf("could not init network state: %w", err))
}
c.handleNodeInfoStatus(ni) c.handleNodeInfoStatus(ni)