From 21a2f49b01e654db1d96027e15a2f7344ebd0fbd Mon Sep 17 00:00:00 2001 From: An Tran Date: Wed, 19 Mar 2025 14:09:14 +1000 Subject: [PATCH] [http_ng] Fix regression In PR #1503 we return nil and error in case of an error, however some code paths do not handle the error but instead check the error field of the response object and lead to unexpected behavior. With this PR, we will return a response object with the error field set. --- gateway/src/resty/http_ng/backend/resty.lua | 2 +- spec/resty/http_ng/backend/resty_spec.lua | 7 +++++-- spec/resty/http_ng_spec.lua | 8 ++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/gateway/src/resty/http_ng/backend/resty.lua b/gateway/src/resty/http_ng/backend/resty.lua index 0dfb748ef..0cc7a3b71 100644 --- a/gateway/src/resty/http_ng/backend/resty.lua +++ b/gateway/src/resty/http_ng/backend/resty.lua @@ -37,7 +37,7 @@ backend.send = function(_, request) local httpc, err = http_proxy.new(request) if err then - return nil, err + return response.error(request, err) end if httpc then diff --git a/spec/resty/http_ng/backend/resty_spec.lua b/spec/resty/http_ng/backend/resty_spec.lua index 9f4e5ff7c..dba755bfe 100755 --- a/spec/resty/http_ng/backend/resty_spec.lua +++ b/spec/resty/http_ng/backend/resty_spec.lua @@ -37,8 +37,11 @@ describe('resty backend', function() local req = { method = method, url = 'http://0.0.0.0:0/' } local response, err = backend:send(req) - assert.falsy(response) - assert.equal("connection refused", err) + assert.falsy(err) + assert.truthy(response.error) + assert.equal("connection refused", response.error) + assert.falsy(response.ok) + assert.same(req, response.request) end) context('http proxy is set', function() diff --git a/spec/resty/http_ng_spec.lua b/spec/resty/http_ng_spec.lua index 1fdcb8b85..b14eafa0b 100755 --- a/spec/resty/http_ng_spec.lua +++ b/spec/resty/http_ng_spec.lua @@ -224,18 +224,18 @@ describe('http_ng', function() end) describe('when there is error #network', function() - local response, err + local response before_each(function() http = http_ng.new{ backend = resty_backend } - response, err = http.get('http://127.0.0.1:1') + response = http.get('http://127.0.0.1:1') end) it('is not ok', function() - assert.falsy(response) + assert.equal(false, response.ok) end) it('has error', function() - assert.equal("connection refused", err) + assert.equal("connection refused", response.error) end) end)