From 8a2a096680586ee08c93fdf8bfc29e83659b443b Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 16 Feb 2022 18:53:43 +0300 Subject: [PATCH] [#1175] services/sign: Convert `error` to status return In previous implementation `SignService` converted all `error` values to `INTERNAL` server failure status. That was done for simplification only. There is a need to transmit status errors as corresponding status messages. Make `SignService` to unwrap errors and convert them to status message during writing to the response. Non-status errors are converted to `INTERNAL` server failures. Status errors can also be wrapped in the depths of the executable code, so `SignService` tries to unwrap them. Signed-off-by: Leonard Lyubich --- pkg/services/util/sign.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/pkg/services/util/sign.go b/pkg/services/util/sign.go index 3b32536585..83fd18b9c4 100644 --- a/pkg/services/util/sign.go +++ b/pkg/services/util/sign.go @@ -107,13 +107,9 @@ func (s *RequestMessageStreamer) CloseAndRecv() (ResponseMessage, error) { return nil, err } - var st apistatus.ServerInternal // specific API status should be set according to error - - apistatus.WriteInternalServerErr(&st, err) - resp = s.respCons() - setStatusV2(resp, st) + setStatusV2(resp, err) } if err = signResponse(s.key, resp, s.statusSupported); err != nil { @@ -164,13 +160,9 @@ func (s *SignService) HandleServerStreamRequest( return err } - var st apistatus.ServerInternal // specific API status should be set according to error - - apistatus.WriteInternalServerErr(&st, err) - resp := blankResp() - setStatusV2(resp, st) + setStatusV2(resp, err) _ = signResponse(s.key, resp, false) // panics or returns nil with false arg @@ -204,13 +196,9 @@ func (s *SignService) HandleUnaryRequest(ctx context.Context, req interface{}, h return nil, err } - var st apistatus.ServerInternal // specific API status should be set according to error - - apistatus.WriteInternalServerErr(&st, err) - resp = blankResp() - setStatusV2(resp, st) + setStatusV2(resp, err) } // sign the response @@ -229,8 +217,13 @@ func isStatusSupported(req RequestMessage) bool { return mjr > 2 || mjr == 2 && version.GetMinor() >= 11 } -func setStatusV2(resp ResponseMessage, st apistatus.Status) { - session.SetStatus(resp, apistatus.ToStatusV2(st)) +func setStatusV2(resp ResponseMessage, err error) { + // unwrap error + for e := errors.Unwrap(err); e != nil; e = errors.Unwrap(err) { + err = e + } + + session.SetStatus(resp, apistatus.ToStatusV2(apistatus.ErrToStatus(err))) } // signs response with private key via signature.SignServiceMessage.