Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: streamline error checking process #6103

Merged
merged 3 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apisix/cli/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local tonumber = tonumber
return function (apisix_home, pkg_cpath_org, pkg_path_org)
-- ulimit setting should be checked when APISIX starts
local res, err = util.execute_cmd("ulimit -n")
if not res or err ~= nil then
if not res then
error("failed to exec ulimit cmd \'ulimit -n \', err: " .. err)
end
local ulimit = tonumber(util.trim(res))
Expand Down
7 changes: 3 additions & 4 deletions apisix/cli/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ local function execute_cmd(cmd)
local data, err = t:read("*all")
t:close()

if err ~= nil then
if not data then
return nil, "failed to read execution result of: "
.. cmd .. ", error info: " .. err
end
Expand Down Expand Up @@ -78,12 +78,11 @@ function _M.read_file(file_path)
end

local data, err = file:read("*all")
if err ~= nil then
file:close()
file:close()
if not data then
return false, "failed to read file: " .. file_path .. ", error info:" .. err
end

file:close()
return data
end

Expand Down
6 changes: 3 additions & 3 deletions apisix/core/vault.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ local function get(key, skip_prefix)
core.log.info("fetching data from vault for key: ", key)

local res, err = make_request_to_vault("GET", key, skip_prefix)
if not res or err then
if not res then
return nil, "failed to retrtive data from vault kv engine " .. err
end

Expand All @@ -93,7 +93,7 @@ local function set(key, data, skip_prefix)
"and value: ", core.json.delay_encode(data, true))

local res, err = make_request_to_vault("POST", key, skip_prefix, data)
if not res or err then
if not res then
return nil, "failed to store data into vault kv engine " .. err
end

Expand All @@ -110,7 +110,7 @@ local function delete(key, skip_prefix)

local res, err = make_request_to_vault("DELETE", key, skip_prefix)

if not res or err then
if not res then
return nil, "failed to delete data into vault kv engine " .. err
end

Expand Down
2 changes: 1 addition & 1 deletion apisix/discovery/consul_kv/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ local function read_dump_srvs()
end

local entity, err = core.json.decode(data)
if err then
if not entity then
log.error("decoded dump data got error: ", err, ", file content: ", data)
return
end
Expand Down
9 changes: 2 additions & 7 deletions apisix/plugins/google-cloud-logging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ local function send_to_google(oauth, entries)
},
})

if err then
if not res then
return nil, "failed to write log to google, " .. err
end

Expand Down Expand Up @@ -214,12 +214,7 @@ function _M.log(conf, ctx)
return
end

local entry
entry, err = get_logger_entry(conf, ctx, oauth)
if err then
core.log.error(err)
return
end
local entry = get_logger_entry(conf, ctx, oauth)

if batch_processor_manager:add_entry(conf, entry) then
return
Expand Down
4 changes: 2 additions & 2 deletions apisix/plugins/jwt-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ local function get_secret(conf, consumer_name)
local secret = conf.secret
if conf.vault then
local res, err = vault.get(get_vault_path(consumer_name))
if not res or err then
if not res then
return nil, err
end

Expand Down Expand Up @@ -233,7 +233,7 @@ local function get_rsa_keypair(conf, consumer_name)
local vout = {}
if conf.vault then
local res, err = vault.get(get_vault_path(consumer_name))
if not res or err then
if not res then
return nil, nil, err
end

Expand Down
4 changes: 2 additions & 2 deletions apisix/plugins/opa.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ function _M.access(conf, ctx)
local res, err = httpc:request_uri(endpoint, params)

-- block by default when decision is unavailable
if not res or err then
if not res then
core.log.error("failed to process OPA decision, err: ", err)
return 403
end

-- parse the results of the decision
local data, err = core.json.decode(res.body)

if err or not data then
if not data then
core.log.error("invalid response body: ", res.body, " err: ", err)
return 503
end
Expand Down
2 changes: 1 addition & 1 deletion apisix/plugins/openwhisk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function _M.access(conf, ctx)

local res, err = httpc:request_uri(endpoint, params)

if not res or err then
if not res then
core.log.error("failed to process openwhisk action, err: ", err)
return 503
end
Expand Down
7 changes: 3 additions & 4 deletions apisix/plugins/splunk-hec-logging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,13 @@ local function send_to_splunk(conf, entries)
headers = request_headers,
})

if err then
if not res then
return false, "failed to write log to splunk, " .. err
end

if res.status ~= 200 then
local body
body, err = core.json.decode(res.body)
if err then
local body = core.json.decode(res.body)
if not body then
return false, "failed to send splunk, http status code: " .. res.status
else
return false, "failed to send splunk, " .. body.text
Expand Down
8 changes: 4 additions & 4 deletions apisix/plugins/wolf-rbac.lua
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ local function check_url_permission(server, appid, action, resName, client_ip, w
end

local body, err = json.decode(res.body)
if err then
if not body then
errmsg = 'check permission failed! parse response json failed!'
core.log.error( "json.decode(", res.body, ") failed! err:", err)
return {status = res.status, err = errmsg}
Expand Down Expand Up @@ -341,7 +341,7 @@ local function get_args()
1, true) then
local req_body = req_get_body_data()
args, err = json.decode(req_body)
if err then
if not args then
core.log.error("json.decode(", req_body, ") failed! ", err)
end
else
Expand Down Expand Up @@ -383,7 +383,7 @@ local function request_to_wolf_server(method, uri, headers, body)

core.log.info("request [", request_debug, "] ....")
local res, err = http_req(method, uri, core.json.encode(body), headers, timeout)
if err or not res then
if not res then
core.log.error("request [", request_debug, "] failed! err: ", err)
return core.response.exit(500,
fail_response("request to wolf-server failed! " .. tostring(err))
Expand All @@ -401,7 +401,7 @@ local function request_to_wolf_server(method, uri, headers, body)
)
end
local body, err = json.decode(res.body)
if err or not body then
if not body then
core.log.error("request [", request_debug, "] failed! err:", err)
return core.response.exit(500, fail_response("request to wolf-server failed!"))
end
Expand Down
2 changes: 1 addition & 1 deletion apisix/plugins/zipkin/reporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function _M.init_processor(self)
end

local processor, err = batch_processor:new(flush, process_conf)
if err then
if not processor then
return false, "create processor error: " .. err
end

Expand Down