[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-02-16 18:53:43 +03:00 committed by LeL
parent ced854bc2e
commit 8a2a096680

View file

@ -107,13 +107,9 @@ func (s *RequestMessageStreamer) CloseAndRecv() (ResponseMessage, error) {
return nil, err return nil, err
} }
var st apistatus.ServerInternal // specific API status should be set according to error
apistatus.WriteInternalServerErr(&st, err)
resp = s.respCons() resp = s.respCons()
setStatusV2(resp, st) setStatusV2(resp, err)
} }
if err = signResponse(s.key, resp, s.statusSupported); err != nil { if err = signResponse(s.key, resp, s.statusSupported); err != nil {
@ -164,13 +160,9 @@ func (s *SignService) HandleServerStreamRequest(
return err return err
} }
var st apistatus.ServerInternal // specific API status should be set according to error
apistatus.WriteInternalServerErr(&st, err)
resp := blankResp() resp := blankResp()
setStatusV2(resp, st) setStatusV2(resp, err)
_ = signResponse(s.key, resp, false) // panics or returns nil with false arg _ = 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 return nil, err
} }
var st apistatus.ServerInternal // specific API status should be set according to error
apistatus.WriteInternalServerErr(&st, err)
resp = blankResp() resp = blankResp()
setStatusV2(resp, st) setStatusV2(resp, err)
} }
// sign the response // sign the response
@ -229,8 +217,13 @@ func isStatusSupported(req RequestMessage) bool {
return mjr > 2 || mjr == 2 && version.GetMinor() >= 11 return mjr > 2 || mjr == 2 && version.GetMinor() >= 11
} }
func setStatusV2(resp ResponseMessage, st apistatus.Status) { func setStatusV2(resp ResponseMessage, err error) {
session.SetStatus(resp, apistatus.ToStatusV2(st)) // 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. // signs response with private key via signature.SignServiceMessage.