[v4] support error handling flow like gRPC (#158)
Some checks failed
sync / sync (push) Failing after 1h5m56s
test / test (push) Failing after 1h9m11s
coverage / build (push) Failing after 1h9m22s

* add status package with tests and integrate into response parsing
* improve unit-tests
* improve readme
This commit is contained in:
2025-09-30 12:28:39 +05:00
committed by GitHub
parent 9bb2f8cffa
commit 6209a03044
7 changed files with 308 additions and 26 deletions

View File

@@ -16,6 +16,8 @@ import (
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/selector"
"go.unistack.org/micro-client-http/v4/status"
)
func (c *Client) fnCall(ctx context.Context, req client.Request, rsp any, opts ...client.CallOption) error {
@@ -246,6 +248,8 @@ func (c *Client) parseRsp(ctx context.Context, hrsp *http.Response, rsp any, opt
return nil
}
s := status.New(hrsp.StatusCode)
var mappedErr any
errMap, ok := errorMapFromOpts(opts)
@@ -257,17 +261,12 @@ func (c *Client) parseRsp(ctx context.Context, hrsp *http.Response, rsp any, opt
}
if !ok || mappedErr == nil {
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
return s.Err()
}
if err = cf.Unmarshal(buf, mappedErr); err != nil {
return errors.InternalServerError("go.micro.client", "unmarshal response: %v", err)
}
if v, ok := mappedErr.(error); ok {
return v
}
// if the error map item does not implement the error interface, wrap it
return &Error{err: mappedErr}
return s.WithDetails(mappedErr).Err()
}