diff --git a/federationapi/inthttp/server.go b/federationapi/inthttp/server.go index 09590a132..02d4bc3c6 100644 --- a/federationapi/inthttp/server.go +++ b/federationapi/inthttp/server.go @@ -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(), } diff --git a/internal/httputil/http.go b/internal/httputil/http.go index 17504d691..0788ac69d 100644 --- a/internal/httputil/http.go +++ b/internal/httputil/http.go @@ -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) diff --git a/internal/httputil/internalapi.go b/internal/httputil/internalapi.go index fea7ff188..a366631f8 100644 --- a/internal/httputil/internalapi.go +++ b/internal/httputil/internalapi.go @@ -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 }