[#788] cli: Wrap/sync errors

Add context to error messages. Sync error
messages for errors with the same context.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-09-01 19:33:48 +03:00 committed by Alex Vanin
parent ba234699cd
commit 3c848b2cad
5 changed files with 75 additions and 55 deletions

View file

@ -355,10 +355,10 @@ var getExtendedACLCmd = &cobra.Command{
if containerJSON {
data, err = eaclTable.MarshalJSON()
exitOnErr(cmd, errf("can't enode to JSON: %w", err))
exitOnErr(cmd, errf("can't encode to JSON: %w", err))
} else {
data, err = eaclTable.Marshal()
exitOnErr(cmd, errf("can't enode to binary: %w", err))
exitOnErr(cmd, errf("can't encode to binary: %w", err))
}
cmd.Println("dumping data to file:", containerPathTo)
@ -367,7 +367,7 @@ var getExtendedACLCmd = &cobra.Command{
printJSONMarshaler(cmd, sig, "signature")
err = os.WriteFile(containerPathTo, data, 0644)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not write eACL to file: %w", err))
},
}
@ -509,12 +509,12 @@ func getSessionToken(path string) (*session.Token, error) {
if path != "" {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("could not open file with session token: %w", err)
}
tok = session.NewToken()
if err = tok.UnmarshalJSON(data); err != nil {
return nil, err
return nil, fmt.Errorf("could not ummarshal session token from file: %w", err)
}
}
@ -618,7 +618,12 @@ func parseNonce(nonce string) (uuid.UUID, error) {
return result, nil
}
return uuid.Parse(nonce)
uid, err := uuid.Parse(nonce)
if err != nil {
return uuid.UUID{}, fmt.Errorf("could not parse nonce: %w", err)
}
return uid, nil
}
func parseContainerID(idStr string) (*cid.ID, error) {

View file

@ -100,10 +100,10 @@ func healthCheck(cmd *cobra.Command, _ []string) {
req.SetBody(new(control.HealthCheckRequest_Body))
err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not sign message: %w", err))
resp, err := control.HealthCheck(cli.Raw(), req)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -113,7 +113,7 @@ func healthCheck(cmd *cobra.Command, _ []string) {
return sign.GetKey(), sign.GetSign()
},
)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("invalid response signature: %w", err))
cmd.Printf("Network status: %s\n", resp.GetBody().GetNetmapStatus())
cmd.Printf("Health status: %s\n", resp.GetBody().GetHealthStatus())
@ -128,7 +128,7 @@ func healthCheckIR(cmd *cobra.Command, key *ecdsa.PrivateKey, c client.Client) {
exitOnErr(cmd, errf("could not sign request: %w", err))
resp, err := ircontrol.HealthCheck(c.Raw(), req)
exitOnErr(cmd, errf("rpc failure: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -166,13 +166,13 @@ func setNetmapStatus(cmd *cobra.Command, _ []string) {
body.SetStatus(status)
err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not sign request: %w", err))
cli, err := getSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.SetNetmapStatus(cli.Raw(), req)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -182,7 +182,7 @@ func setNetmapStatus(cmd *cobra.Command, _ []string) {
return sign.GetKey(), sign.GetSign()
},
)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("invalid response signature: %w", err))
cmd.Println("Network status update request successfully sent.")
}
@ -223,13 +223,13 @@ var dropObjectsCmd = &cobra.Command{
body.SetAddressList(binAddrList)
err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not sign request: %w", err))
cli, err := getSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.DropObjects(cli.Raw(), req)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -239,7 +239,7 @@ var dropObjectsCmd = &cobra.Command{
return sign.GetKey(), sign.GetSign()
},
)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("invalid response signature: %w", err))
cmd.Println("Objects were successfully marked to be removed.")
},
@ -257,13 +257,13 @@ var snapshotCmd = &cobra.Command{
req.SetBody(new(control.NetmapSnapshotRequest_Body))
err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not sign request: %w", err))
cli, err := getSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.NetmapSnapshot(cli.Raw(), req)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -273,7 +273,7 @@ var snapshotCmd = &cobra.Command{
return sign.GetKey(), sign.GetSign()
},
)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("invalid response signature: %w", err))
prettyPrintNetmap(cmd, resp.GetBody().GetNetmap(), netmapSnapshotJSON)
},

View file

@ -263,7 +263,7 @@ func putObject(cmd *cobra.Command, _ []string) {
client.WithBearer(btok),
)...,
)
exitOnErr(cmd, errf("can't put object: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
cmd.Printf("[%s] Object successfully stored\n", filename)
cmd.Printf(" ID: %s\n CID: %s\n", oid, cid)
@ -271,7 +271,7 @@ func putObject(cmd *cobra.Command, _ []string) {
func deleteObject(cmd *cobra.Command, _ []string) {
key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err))
exitOnErr(cmd, err)
objAddr, err := getObjectAddress(cmd)
exitOnErr(cmd, err)
@ -289,7 +289,7 @@ func deleteObject(cmd *cobra.Command, _ []string) {
client.WithBearer(btok),
)...,
)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("rpc error: %w", err))
cmd.Println("Object removed successfully.")
cmd.Printf(" ID: %s\n CID: %s\n", tombstoneAddr.ObjectID(), tombstoneAddr.ContainerID())
@ -297,7 +297,7 @@ func deleteObject(cmd *cobra.Command, _ []string) {
func getObject(cmd *cobra.Command, _ []string) {
key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err))
exitOnErr(cmd, err)
objAddr, err := getObjectAddress(cmd)
exitOnErr(cmd, err)
@ -340,7 +340,7 @@ func getObject(cmd *cobra.Command, _ []string) {
return
}
exitOnErr(cmd, fmt.Errorf("can't get object: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
}
if filename != "" {
@ -357,7 +357,7 @@ func getObject(cmd *cobra.Command, _ []string) {
func getObjectHeader(cmd *cobra.Command, _ []string) {
key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err))
exitOnErr(cmd, err)
objAddr, err := getObjectAddress(cmd)
exitOnErr(cmd, err)
@ -386,7 +386,7 @@ func getObjectHeader(cmd *cobra.Command, _ []string) {
return
}
exitOnErr(cmd, fmt.Errorf("can't get object header: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
}
err = saveAndPrintHeader(cmd, obj, cmd.Flag("file").Value.String())
@ -395,7 +395,7 @@ func getObjectHeader(cmd *cobra.Command, _ []string) {
func searchObject(cmd *cobra.Command, _ []string) {
key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err))
exitOnErr(cmd, err)
cid, err := getCID(cmd)
exitOnErr(cmd, err)
@ -415,7 +415,7 @@ func searchObject(cmd *cobra.Command, _ []string) {
client.WithBearer(btok),
)...,
)
exitOnErr(cmd, errf("can't search object: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
cmd.Printf("Found %d objects.\n", len(ids))
for _, id := range ids {
cmd.Println(id)
@ -436,7 +436,7 @@ func getObjectHash(cmd *cobra.Command, _ []string) {
strSalt := strings.TrimPrefix(cmd.Flag(getRangeHashSaltFlag).Value.String(), "0x")
salt, err := hex.DecodeString(strSalt)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not decode salt: %w", err))
ctx := context.Background()
cli, tok, err := initSession(ctx, key)
@ -451,7 +451,7 @@ func getObjectHash(cmd *cobra.Command, _ []string) {
client.WithBearer(btok),
)...,
)
exitOnErr(cmd, errf("can't get object header: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
switch typ {
case hashSha256:
cmd.Println(hex.EncodeToString(obj.PayloadChecksum().Sum()))
@ -474,7 +474,7 @@ func getObjectHash(cmd *cobra.Command, _ []string) {
client.WithBearer(btok),
)...,
)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("rpc error: %w", err))
for i := range res {
cmd.Printf("Offset=%d (Length=%d)\t: %s\n", ranges[i].GetOffset(), ranges[i].GetLength(),
hex.EncodeToString(res[i][:]))
@ -486,7 +486,7 @@ func getObjectHash(cmd *cobra.Command, _ []string) {
client.WithBearer(btok),
)...,
)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("rpc error: %w", err))
for i := range res {
cmd.Printf("Offset=%d (Length=%d)\t: %s\n", ranges[i].GetOffset(), ranges[i].GetLength(),
hex.EncodeToString(res[i][:]))
@ -526,13 +526,13 @@ func parseSearchFilters(cmd *cobra.Command) (object.SearchFilters, error) {
case 1:
data, err := os.ReadFile(words[0])
if err != nil {
return nil, err
return nil, fmt.Errorf("could not read attributes filter from file: %w", err)
}
subFs := object.NewSearchFilters()
if err := subFs.UnmarshalJSON(data); err != nil {
return nil, err
return nil, fmt.Errorf("could not unmarshal attributes filter from file: %w", err)
}
fs = append(fs, subFs...)
@ -567,7 +567,7 @@ func parseSearchFilters(cmd *cobra.Command) (object.SearchFilters, error) {
if oid != "" {
id := object.NewID()
if err := id.Parse(oid); err != nil {
return nil, err
return nil, fmt.Errorf("could not parse object ID: %w", err)
}
fs.AddObjectIDFilter(object.MatchStringEqual, id)
@ -618,16 +618,24 @@ func parseObjectAttrs(cmd *cobra.Command) ([]*object.Attribute, error) {
func getCID(cmd *cobra.Command) (*cid.ID, error) {
id := cid.New()
err := id.Parse(cmd.Flag("cid").Value.String())
return id, err
err := id.Parse(cmd.Flag("cid").Value.String())
if err != nil {
return nil, fmt.Errorf("could not parse container ID: %w", err)
}
return id, nil
}
func getOID(cmd *cobra.Command) (*object.ID, error) {
oid := object.NewID()
err := oid.Parse(cmd.Flag("oid").Value.String())
return oid, err
err := oid.Parse(cmd.Flag("oid").Value.String())
if err != nil {
return nil, fmt.Errorf("could not parse object ID: %w", err)
}
return oid, nil
}
func getObjectAddress(cmd *cobra.Command) (*object.Address, error) {
@ -687,16 +695,16 @@ func getHashType(cmd *cobra.Command) (string, error) {
func saveAndPrintHeader(cmd *cobra.Command, obj *object.Object, filename string) error {
bs, err := marshalHeader(cmd, obj)
if err != nil {
return err
return fmt.Errorf("could not marshal header: %w", err)
}
if len(bs) != 0 {
if filename == "" {
cmd.Println(string(bs))
return nil
}
err := os.WriteFile(filename, bs, os.ModePerm)
err = os.WriteFile(filename, bs, os.ModePerm)
if err != nil {
return err
return fmt.Errorf("could not write header to file: %w", err)
}
cmd.Printf("[%s] Header successfully saved.", filename)
}

View file

@ -288,6 +288,9 @@ func getSDKClient(key *ecdsa.PrivateKey) (client.Client, error) {
}
c, err := client.New(options...)
if err != nil {
return nil, fmt.Errorf("coult not init api client:%w", err)
}
return c, err
}

View file

@ -3,6 +3,7 @@ package cmd
import (
"context"
"errors"
"fmt"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
@ -128,7 +129,7 @@ func sgBearerToken(cmd *cobra.Command) (*token.BearerToken, error) {
func putSG(cmd *cobra.Command, _ []string) {
key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err))
exitOnErr(cmd, err)
ownerID, err := getOwnerID(key)
exitOnErr(cmd, err)
@ -142,7 +143,7 @@ func putSG(cmd *cobra.Command, _ []string) {
id := objectSDK.NewID()
err = id.Parse(sgMembers[i])
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not parse object ID: %w", err))
members = append(members, id)
}
@ -161,10 +162,10 @@ func putSG(cmd *cobra.Command, _ []string) {
c: cli,
bearerToken: bearerToken,
}, cid, members)
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not collect storage group members: %w", err))
sgContent, err := sg.Marshal()
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not marshal storage group: %w", err))
obj := objectSDK.NewRaw()
obj.SetContainerID(cid)
@ -180,7 +181,7 @@ func putSG(cmd *cobra.Command, _ []string) {
client.WithBearer(bearerToken),
)...,
)
exitOnErr(cmd, errf("can't put storage group: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
cmd.Println("Storage group successfully stored")
cmd.Printf(" ID: %s\n CID: %s\n", oid, cid)
@ -189,13 +190,16 @@ func putSG(cmd *cobra.Command, _ []string) {
func getSGID() (*objectSDK.ID, error) {
oid := objectSDK.NewID()
err := oid.Parse(sgID)
if err != nil {
return nil, fmt.Errorf("could not parse storage group ID: %w", err)
}
return oid, err
return oid, nil
}
func getSG(cmd *cobra.Command, _ []string) {
key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err))
exitOnErr(cmd, err)
cid, err := getCID(cmd)
exitOnErr(cmd, err)
@ -223,12 +227,12 @@ func getSG(cmd *cobra.Command, _ []string) {
client.WithBearer(bearerToken),
)...,
)
exitOnErr(cmd, errf("can't get storage group: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
sg := storagegroupAPI.New()
err = sg.Unmarshal(obj.Payload())
exitOnErr(cmd, err)
exitOnErr(cmd, errf("could not unmarshal storage group: %w", err))
cmd.Printf("Expiration epoch: %d\n", sg.ExpirationEpoch())
cmd.Printf("Group size: %d\n", sg.ValidationDataSize())
@ -245,7 +249,7 @@ func getSG(cmd *cobra.Command, _ []string) {
func listSG(cmd *cobra.Command, _ []string) {
key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err))
exitOnErr(cmd, err)
cid, err := getCID(cmd)
exitOnErr(cmd, err)
@ -267,7 +271,7 @@ func listSG(cmd *cobra.Command, _ []string) {
client.WithBearer(bearerToken),
)...,
)
exitOnErr(cmd, errf("can't search storage groups: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
cmd.Printf("Found %d storage groups.\n", len(ids))
@ -278,7 +282,7 @@ func listSG(cmd *cobra.Command, _ []string) {
func delSG(cmd *cobra.Command, _ []string) {
key, err := getKey()
exitOnErr(cmd, errf("can't fetch private key: %w", err))
exitOnErr(cmd, err)
cid, err := getCID(cmd)
exitOnErr(cmd, err)
@ -306,7 +310,7 @@ func delSG(cmd *cobra.Command, _ []string) {
client.WithBearer(bearerToken),
)...,
)
exitOnErr(cmd, errf("can't get storage group: %w", err))
exitOnErr(cmd, errf("rpc error: %w", err))
cmd.Println("Storage group removed successfully.")
cmd.Printf(" Tombstone: %s\n", tombstone.ObjectID())