From 2a6fe47e5e093440d7db1e272bebc929f37d1995 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 11 Jan 2022 15:32:29 +0300 Subject: [PATCH] [#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 --- client/common.go | 15 ++++++++++++--- client/opts.go | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/client/common.go b/client/common.go index 1a73303f..aa1d0427 100644 --- a/client/common.go +++ b/client/common.go @@ -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 } diff --git a/client/opts.go b/client/opts.go index 7986a7b0..60a38943 100644 --- a/client/opts.go +++ b/client/opts.go @@ -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 + } +}