[#114] client: Add WithNeoFSErrorParsing option

This option make client parse all NeoFS error statuses and return them as
errors from method calls (not as part of result structure). This is done
for applications that want to handle errors as it is customary in golang.
Default behaviour (construction client without new option) has not been
changed.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2022-01-11 15:32:29 +03:00 committed by LeL
parent 13d72efd60
commit 2a6fe47e5e
2 changed files with 27 additions and 3 deletions

View file

@ -58,7 +58,8 @@ type processResponseV2Res struct {
//
// Actions:
// * verify signature (internal);
// * call response callback (internal).
// * call response callback (internal);
// * unwrap status error (optional).
func (c *Client) processResponseV2(res *processResponseV2Res, prm processResponseV2Prm) bool {
// verify response structure
if isInvalidSignatureV2(res, prm.resp) {
@ -71,10 +72,18 @@ func (c *Client) processResponseV2(res *processResponseV2Res, prm processRespons
return true
}
// set result status
// get result status
st := apistatus.FromStatusV2(prm.resp.GetMetaHeader().GetStatus())
// unwrap unsuccessful status and return it
// as error if client has been configured so
unsuccessfulStatus := !apistatus.IsSuccessful(st)
if unsuccessfulStatus && c.opts.parseNeoFSErrors {
res.cliErr = apistatus.ErrFromStatus(st)
return true
}
res.statusRes.setStatus(st)
return !apistatus.IsSuccessful(st)
return unsuccessfulStatus
}

View file

@ -35,6 +35,12 @@ type (
rawOpts []client.Option
cbRespInfo func(ResponseMetaInfo) error
// defines if client parses erroneous NeoFS
// statuses and returns them as `error`
//
// default is false
parseNeoFSErrors bool
}
v2SessionReqInfo struct {
@ -195,3 +201,12 @@ func WithGRPCConnection(grpcConn *grpc.ClientConn) Option {
opts.rawOpts = append(opts.rawOpts, client.WithGRPCConn(grpcConn))
}
}
// WithNeoFSErrorParsing returns option that makes client parse
// erroneous NeoFS statuses and return them as `error` of the method
// call.
func WithNeoFSErrorParsing() Option {
return func(opts *clientOptions) {
opts.parseNeoFSErrors = true
}
}