This is an automated email from the ASF dual-hosted git repository. tokers pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push: new 50e9bfa feat(ext-plugin): handle error response (#4179) 50e9bfa is described below commit 50e9bfa54ed00dc042ffdbe7bdd183bc09b66c2e Author: 罗泽轩 <spacewander...@gmail.com> AuthorDate: Thu May 6 10:00:12 2021 +0800 feat(ext-plugin): handle error response (#4179) Signed-off-by: spacewander <spacewander...@gmail.com> --- apisix/plugins/ext-plugin/init.lua | 27 ++++++++++++++++++---- t/lib/ext-plugin.lua | 46 ++++++++++++++++++++++++-------------- t/plugin/ext-plugin/sanity.t | 18 +++++++++++++++ 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/apisix/plugins/ext-plugin/init.lua b/apisix/plugins/ext-plugin/init.lua index 924e9f4..9dde610 100644 --- a/apisix/plugins/ext-plugin/init.lua +++ b/apisix/plugins/ext-plugin/init.lua @@ -19,6 +19,8 @@ local flatbuffers = require("flatbuffers") local prepare_conf_req = require("A6.PrepareConf.Req") local prepare_conf_resp = require("A6.PrepareConf.Resp") local text_entry = require("A6.TextEntry") +local err_resp = require("A6.Err.Resp") +local err_code = require("A6.Err.Code") local constants = require("apisix.constants") local core = require("apisix.core") local helper = require("apisix.plugins.ext-plugin.helper") @@ -117,6 +119,23 @@ end _M.send = send +local err_to_msg +do + local map = { + [err_code.BAD_REQUEST] = "bad request", + [err_code.SERVICE_UNAVAILABLE] = "service unavailable", + [err_code.CONF_TOKEN_NOT_FOUND] = "conf token not found", + } + + function err_to_msg(resp) + local buf = flatbuffers.binaryArray.New(resp) + local resp = err_resp.GetRootAsResp(buf, 0) + local code = resp:Code() + return map[code] or str_format("unknown err %d", code) + end +end + + local function receive(sock) local hdr, err = sock:receive(4) if not hdr then @@ -127,10 +146,6 @@ local function receive(sock) end local ty = str_byte(hdr, 1) - if ty == constants.RPC_ERROR then - return nil, "TODO: handler err" - end - local resp local hi, mi, li = str_byte(hdr, 2, 4) local len = 256 * (256 * hi + mi) + li @@ -147,6 +162,10 @@ local function receive(sock) end end + if ty == constants.RPC_ERROR then + return nil, err_to_msg(resp) + end + return ty, resp end _M.receive = receive diff --git a/t/lib/ext-plugin.lua b/t/lib/ext-plugin.lua index 9b0a52d..be7f6e5 100644 --- a/t/lib/ext-plugin.lua +++ b/t/lib/ext-plugin.lua @@ -17,6 +17,8 @@ local ext = require("apisix.plugins.ext-plugin.init") local constants = require("apisix.constants") local flatbuffers = require("flatbuffers") +local err_code = require("A6.Err.Code") +local err_resp = require("A6.Err.Resp") local prepare_conf_req = require("A6.PrepareConf.Req") local prepare_conf_resp = require("A6.PrepareConf.Resp") @@ -35,25 +37,35 @@ function _M.go(case) ngx.log(ngx.WARN, "receive rpc call successfully") if ty == constants.RPC_PREPARE_CONF then - local buf = flatbuffers.binaryArray.New(data) - local pc = prepare_conf_req.GetRootAsReq(buf, 0) - - if case.with_conf then - local conf = pc:Conf(1) - assert(conf:Name(), "foo") - assert(conf:Value(), "bar") - local conf = pc:Conf(2) - assert(conf:Name(), "cat") - assert(conf:Value(), "dog") + if case.inject_error then + ty = constants.RPC_ERROR + err_resp.Start(builder) + err_resp.AddCode(builder, err_code.BAD_REQUEST) + local req = prepare_conf_req.End(builder) + builder:Finish(req) + data = builder:Output() + else - assert(pc:ConfLength() == 0) + local buf = flatbuffers.binaryArray.New(data) + local pc = prepare_conf_req.GetRootAsReq(buf, 0) + + if case.with_conf then + local conf = pc:Conf(1) + assert(conf:Name(), "foo") + assert(conf:Value(), "bar") + local conf = pc:Conf(2) + assert(conf:Name(), "cat") + assert(conf:Value(), "dog") + else + assert(pc:ConfLength() == 0) + end + + prepare_conf_resp.Start(builder) + prepare_conf_resp.AddConfToken(builder, 233) + local req = prepare_conf_req.End(builder) + builder:Finish(req) + data = builder:Output() end - - prepare_conf_resp.Start(builder) - prepare_conf_resp.AddConfToken(builder, 233) - local req = prepare_conf_req.End(builder) - builder:Finish(req) - data = builder:Output() end local ok, err = ext.send(sock, ty, data) diff --git a/t/plugin/ext-plugin/sanity.t b/t/plugin/ext-plugin/sanity.t index be275f4..f3646d4 100644 --- a/t/plugin/ext-plugin/sanity.t +++ b/t/plugin/ext-plugin/sanity.t @@ -316,3 +316,21 @@ hello world qr/get conf token: 233 conf: \[(\{"value":"bar","name":"foo"\}|\{"name":"foo","value":"bar"\}),(\{"value":"dog","name":"cat"\}|\{"name":"cat","value":"dog"\})\]/ --- no_error_log [error] + + + +=== TEST 11: handle error code +--- request +GET /hello +--- extra_stream_config + server { + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock; + + content_by_lua_block { + local ext = require("lib.ext-plugin") + ext.go({inject_error = true}) + } + } +--- error_code: 503 +--- error_log +failed to receive RPC_PREPARE_CONF: bad request