Skip to content

Commit

Permalink
优化访问后端接口出现错误时的错误消息
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-smile committed Aug 9, 2023
1 parent 385d36d commit a4fd97c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
7 changes: 6 additions & 1 deletion src/apisix/editions/ee/plugins/bk-components/bklogin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ function _M.get_username_by_bk_token(bk_token)

local result, _err = bk_components_utils.parse_response(res, err, true)
if result == nil then
core.log.error(string_format("failed to request %s, err: %s", url, _err))
core.log.error(
string_format(
"failed to request %s, err: %s, status: %s, response: %s", url, _err, res and res.status,
res and res.body
)
)
return nil, string_format("failed to request third-party api, url: %s, err: %s", url, _err)
end

Expand Down
9 changes: 7 additions & 2 deletions src/apisix/editions/ee/plugins/bk-components/ssm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ function _M.verify_access_token(access_token)

local result, _err = bk_components_utils.parse_response(res, err, true)
if result == nil then
core.log.error(string_format("failed to request %s, err: %s", url, _err))
core.log.error(
string_format(
"failed to request %s, err: %s, status: %s, response: %s", url, _err, res and res.status,
res and res.body
)
)
return nil, string_format("failed to request third-party api, url: %s, err: %s", url, _err)
end

if result.code ~= 0 then
return nil, string_format("ssm error message: %s", result.message)
return nil, string_format("ssm error message: %s, code: %s", result.message, result.code)
end

return {
Expand Down
3 changes: 1 addition & 2 deletions src/apisix/editions/ee/tests/bk-components/test-ssm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
-- We undertake not to change the open source license (MIT license) applicable
-- to the current version of the project delivered to anyone in the future.
--

local core = require("apisix.core")
local http = require("resty.http")
local ssm = require("apisix.plugins.bk-components.ssm")
Expand Down Expand Up @@ -79,7 +78,7 @@ describe(

local result, err = ssm.verify_access_token("fake-access-token")
assert.is_nil(result)
assert.is_equal(err, "ssm error message: error")
assert.is_equal(err, "ssm error message: error, code: 1")
end
)

Expand Down
59 changes: 42 additions & 17 deletions src/apisix/plugins/bk-components/bkauth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ function _M.verify_app_secret(app_code, app_secret)
)

if not (res and res.body) then
core.log.error(string_format("failed to request %s, err: %s", url, err))
return nil, string_format("failed to request third-party api, url: %s, err: %s", url, err)
core.log.error(string_format("failed to request %s, err: %s, response: nil", url, err))
return nil, string_format("failed to request third-party api, url: %s, err: %s, response: nil", url, err)
end

-- 响应格式正常,错误码 404,表示应用不存在
Expand All @@ -80,13 +80,22 @@ function _M.verify_app_secret(app_code, app_secret)

local result = core.json.decode(res.body)
if result == nil then
core.log.error(string_format("failed to request %s, response is not valid json, response: %s", url, res.body))
return nil, string_format("failed to request third-party api, response is not valid json, url: %s", url)
core.log.error(
string_format(
"failed to request %s, response is not valid json, status: %s, response: %s", url, res.status, res.body
)
)
return nil, string_format(
"failed to request third-party api, response is not valid json, url: %s, status: %s", url, res.status
)
end

if result.code ~= 0 or res.status ~= 200 then
core.log.error(string_format("failed to request %s, response: %s", url, res.body))
return nil, string_format("failed to request third-party api, bkauth error message: %s", result.message)
core.log.error(string_format("failed to request %s, status: %s, response: %s", url, res.status, res.body))
return nil, string_format(
"failed to request third-party api, bkauth error message: %s, status: %s, code: %s", result.message,
res.status, result.code
)
end

return {
Expand Down Expand Up @@ -117,8 +126,8 @@ function _M.list_app_secrets(app_code)
)

if not (res and res.body) then
core.log.error(string_format("failed to request %s, err: %s", url, err))
return nil, string_format("failed to request third-party api, url: %s, err: %s", url, err)
core.log.error(string_format("failed to request %s, err: %s, response: nil", url, err))
return nil, string_format("failed to request third-party api, url: %s, err: %s, response: nil", url, err)
end

-- 响应格式正常,错误码 404,表示应用不存在
Expand All @@ -130,13 +139,22 @@ function _M.list_app_secrets(app_code)

local result = core.json.decode(res.body)
if result == nil then
core.log.error(string_format("failed to request %s, response is not valid json, response: %s", url, res.body))
return nil, string_format("failed to request third-party api, response is not valid json, url: %s", url)
core.log.error(
string_format(
"failed to request %s, response is not valid json, status: %s, response: %s", url, res.status, res.body
)
)
return nil, string_format(
"failed to request third-party api, response is not valid json, url: %s, status: %s", url, res.status
)
end

if result.code ~= 0 or res.status ~= 200 then
core.log.error(string_format("failed to request %s, response: %s", url, res.body))
return nil, string_format("failed to request third-party api, bkauth error message: %s", result.message)
core.log.error(string_format("failed to request %s, status: %s, response: %s", url, res.status, res.body))
return nil, string_format(
"failed to request third-party api, bkauth error message: %s, status: %s, code: %s", result.message,
res.status, result.code
)
end

local app_secrets = {}
Expand Down Expand Up @@ -177,18 +195,25 @@ function _M.verify_access_token(access_token)
)

if not (res and res.body) then
core.log.error(string_format("failed to request %s, err: %s", url, err))
return nil, string_format("failed to request third-party api, url: %s, err: %s", url, err)
core.log.error(string_format("failed to request %s, err: %s, response: nil", url, err))
return nil, string_format("failed to request third-party api, url: %s, err: %s, response: nil", url, err)
end

local result = core.json.decode(res.body)
if result == nil then
core.log.error(string_format("failed to request %s, response is not valid json, response: %s", url, res.body))
return nil, string_format("failed to request third-party api, response is not valid json, url: %s", url)
core.log.error(
string_format(
"failed to request %s, response is not valid json, status: %s, response: %s", url, res.status, res.body
)
)
return nil, string_format(
"failed to request third-party api, response is not valid json, url: %s, status: %s", url, res.status
)
end

if result.code ~= 0 or res.status ~= 200 then
return nil, string_format("bkauth error message: %s", result.message)
return nil,
string_format("bkauth error message: %s, status: %s, code: %s", result.message, res.status, result.code)
end

-- data example
Expand Down
4 changes: 2 additions & 2 deletions src/apisix/tests/bk-components/test-bkauth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ describe(

local result, err = bkauth.verify_access_token("fake-token")
assert.is_nil(result)
assert.is_equal(err, "bkauth error message: error")
assert.is_equal(err, "bkauth error message: error, status: 200, code: 1")
end
)

Expand All @@ -346,7 +346,7 @@ describe(

local result, err = bkauth.verify_access_token("fake-token")
assert.is_nil(result)
assert.is_equal(err, "bkauth error message: error")
assert.is_equal(err, "bkauth error message: error, status: 500, code: 0")
end
)

Expand Down

0 comments on commit a4fd97c

Please sign in to comment.