mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-10 07:33:09 -06:00
Don't return typed nils
This commit is contained in:
parent
5c6fb34cee
commit
4ddc569cf5
|
|
@ -225,9 +225,12 @@ func federationClientError(err error) *api.FederationClientError {
|
|||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if ferr, ok := err.(*api.FederationClientError); ok {
|
||||
switch ferr := err.(type) {
|
||||
case api.FederationClientError:
|
||||
return &ferr
|
||||
case *api.FederationClientError:
|
||||
return ferr
|
||||
} else {
|
||||
default:
|
||||
return &api.FederationClientError{
|
||||
Err: err.Error(),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,9 +30,9 @@ import (
|
|||
// PostJSON performs a POST request with JSON on an internal HTTP API.
|
||||
// The error will match the errtype if returned from the remote API, or
|
||||
// will be a different type if there was a problem reaching the API.
|
||||
func PostJSON[reqtype, restype any, errtype error](
|
||||
func PostJSON[reqtype, restype, errtype any](
|
||||
ctx context.Context, span opentracing.Span, httpClient *http.Client,
|
||||
apiURL string, request *reqtype, response *restype,
|
||||
apiURL string, request *reqtype, response *restype, reserr *errtype,
|
||||
) error {
|
||||
jsonBytes, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
|
|
@ -71,11 +71,10 @@ func PostJSON[reqtype, restype any, errtype error](
|
|||
return err
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
var errorBody errtype
|
||||
if err = json.NewDecoder(res.Body).Decode(&errorBody); err != nil {
|
||||
if err = json.NewDecoder(res.Body).Decode(reserr); err != nil {
|
||||
return fmt.Errorf("HTTP %d from %s", res.StatusCode, apiURL)
|
||||
}
|
||||
return errorBody
|
||||
return nil
|
||||
}
|
||||
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
|
||||
return fmt.Errorf("json.NewDecoder.Decode: %w", err)
|
||||
|
|
|
|||
|
|
@ -67,10 +67,7 @@ func MakeInternalProxyAPI[reqtype, restype any](metricsName string, f func(conte
|
|||
if err != nil {
|
||||
return util.JSONResponse{
|
||||
Code: http.StatusInternalServerError,
|
||||
JSON: &InternalAPIError{
|
||||
Type: reflect.TypeOf(err).String(),
|
||||
Message: fmt.Sprintf("%s", err),
|
||||
},
|
||||
JSON: err,
|
||||
}
|
||||
}
|
||||
return util.JSONResponse{
|
||||
|
|
@ -80,11 +77,17 @@ func MakeInternalProxyAPI[reqtype, restype any](metricsName string, f func(conte
|
|||
})
|
||||
}
|
||||
|
||||
func CallInternalRPCAPI[req, res any](name, url string, client *http.Client, ctx context.Context, request *req, response *res) error {
|
||||
func CallInternalRPCAPI[reqtype, restype any](name, url string, client *http.Client, ctx context.Context, request *reqtype, response *restype) error {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, name)
|
||||
defer span.Finish()
|
||||
|
||||
return PostJSON[req, res, InternalAPIError](ctx, span, client, url, request, response)
|
||||
var reserr *InternalAPIError
|
||||
if err := PostJSON(ctx, span, client, url, request, response, reserr); err != nil {
|
||||
return err
|
||||
} else if reserr != nil {
|
||||
return reserr
|
||||
}
|
||||
return nil // must be untyped nil
|
||||
}
|
||||
|
||||
func CallInternalProxyAPI[req, res any, errtype error](name, url string, client *http.Client, ctx context.Context, request *req) (res, error) {
|
||||
|
|
@ -92,5 +95,11 @@ func CallInternalProxyAPI[req, res any, errtype error](name, url string, client
|
|||
defer span.Finish()
|
||||
|
||||
var response res
|
||||
return response, PostJSON[req, res, errtype](ctx, span, client, url, request, &response)
|
||||
var reserr *errtype
|
||||
if err := PostJSON(ctx, span, client, url, request, &response, &reserr); err != nil {
|
||||
return response, err
|
||||
} else if reserr != nil {
|
||||
return response, *reserr
|
||||
}
|
||||
return response, nil // must be untyped nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue