From ca4a841afd047dfbbffe544caec696d1d294ee21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Saint-F=C3=A9lix?= Date: Wed, 27 Nov 2024 14:57:01 +0100 Subject: [PATCH] Check error in BaseClient.Perform (#922) (#927) * Check error in BaseClient.Perform The http.Resonse will be nil on errors, so return early. Fixes #913 * revert reordering of imports --------- Co-authored-by: Martin Tournoij --- elasticsearch.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/elasticsearch.go b/elasticsearch.go index c736ded991..8434bd3f52 100644 --- a/elasticsearch.go +++ b/elasticsearch.go @@ -341,9 +341,12 @@ func (c *BaseClient) Perform(req *http.Request) (*http.Response, error) { // Retrieve the original request. res, err := c.Transport.Perform(req) + if err != nil { + return nil, err + } // ResponseCheck, we run the header check on the first answer from ES. - if err == nil && (res.StatusCode >= 200 && res.StatusCode < 300) { + if res.StatusCode >= 200 && res.StatusCode < 300 { checkHeader := func() error { return genuineCheckHeader(res.Header) } if err := c.doProductCheck(checkHeader); err != nil { res.Body.Close() @@ -351,7 +354,7 @@ func (c *BaseClient) Perform(req *http.Request) (*http.Response, error) { } } - return res, err + return res, nil } // InstrumentationEnabled propagates back to the client the Instrumentation provided by the transport.