frostfs-node/cmd/internal/common/exit.go
Dmitrii Stepanov 721ba7181b
Some checks failed
DCO action / DCO (pull_request) Successful in 1m30s
Vulncheck / Vulncheck (pull_request) Successful in 3m41s
Build / Build Components (1.21) (pull_request) Successful in 4m17s
Build / Build Components (1.20) (pull_request) Successful in 4m30s
Tests and linters / Lint (pull_request) Successful in 6m37s
Tests and linters / Staticcheck (pull_request) Successful in 6m17s
Tests and linters / Tests (1.21) (pull_request) Successful in 8m51s
Tests and linters / Tests (1.20) (pull_request) Successful in 9m10s
Tests and linters / Tests with -race (pull_request) Successful in 9m1s
Tests and linters / gopls check (pull_request) Failing after 15m55s
[#9999] node: Move api-go & sdk-go to pkg
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2024-03-20 16:35:44 +03:00

53 lines
972 B
Go

package common
import (
"errors"
"fmt"
"os"
sdkstatus "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/sdk/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)
if cmd.PersistentPostRun != nil {
cmd.PersistentPostRun(cmd, nil)
}
os.Exit(code)
}