From 90dbf3d944b7a1d89f5a778b910565a8c192360c Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Mon, 13 Dec 2021 20:29:32 +0300 Subject: [PATCH] [#1039] cli: Make return codes depend on errors Cast client errors to well-known public `sdk` errors and return appropriate exit code. Signed-off-by: Pavel Karpy --- cmd/neofs-cli/modules/util.go | 39 +++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/cmd/neofs-cli/modules/util.go b/cmd/neofs-cli/modules/util.go index e0915446c..ab0daf7ed 100644 --- a/cmd/neofs-cli/modules/util.go +++ b/cmd/neofs-cli/modules/util.go @@ -16,6 +16,7 @@ import ( locodebolt "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/boltdb" continentsdb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/continents/geojson" csvlocode "github.com/nspcc-dev/neofs-node/pkg/util/locode/table/csv" + sdkstatus "github.com/nspcc-dev/neofs-sdk-go/client/status" "github.com/nspcc-dev/neofs-sdk-go/token" "github.com/nspcc-dev/neofs-sdk-go/version" "github.com/spf13/cobra" @@ -508,17 +509,33 @@ func errf(errFmt string, err error) error { return fmt.Errorf(errFmt, err) } -// exitOnErr calls exitOnErrCode with code 1. +// exitOnErr prints error and exits with a code that matches +// one of the common errors from sdk library. If no errors +// found, exits with 1 code. +// Does nothing if passed error in nil. func exitOnErr(cmd *cobra.Command, err error) { - exitOnErrCode(cmd, err, 1) -} - -// exitOnErrCode prints error via cmd and calls -// os.Exit with passed exit code. Does nothing -// if err is nil. -func exitOnErrCode(cmd *cobra.Command, err error, code int) { - if err != nil { - cmd.PrintErrln(err) - os.Exit(code) + if err == nil { + return } + + const ( + _ = iota + internal + ) + + var ( + code int + + internalErr = new(sdkstatus.ServerInternal) + ) + + switch { + case errors.As(err, &internalErr): + code = internal + default: + code = internal + } + + cmd.PrintErrln(err) + os.Exit(code) }