frostfs-node/cmd/internal/common/exit.go
Anton Nikiforov 2b09564355 [#1889] Move netmap.go and exit.go from cli to cmd/internal/common
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
2023-02-06 17:26:34 +03:00

50 lines
888 B
Go

package common
import (
"errors"
"fmt"
"os"
sdkstatus "github.com/TrueCloudLab/frostfs-sdk-go/client/status"
"github.com/spf13/cobra"
)
// 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, errFmt string, err error) {
if err == nil {
return
}
if errFmt != "" {
err = fmt.Errorf(errFmt, err)
}
const (
_ = iota
internal
aclDenied
)
var (
code int
internalErr = new(sdkstatus.ServerInternal)
accessErr = new(sdkstatus.ObjectAccessDenied)
)
switch {
case errors.As(err, &internalErr):
code = internal
case errors.As(err, &accessErr):
code = aclDenied
err = fmt.Errorf("%w: %s", err, accessErr.Reason())
default:
code = internal
}
cmd.PrintErrln(err)
os.Exit(code)
}