using pcall to catch exception

This commit is contained in:
2022-06-26 18:25:47 +08:00
parent d2abce7e82
commit 701eb48609
+13 -16
View File
@@ -5,7 +5,6 @@ local httpc = http.new()
local sudoerToken = ngx.req.get_headers().HtySudoerToken
local htyhostHeader = ngx.req.get_headers().HtyHost
local function get_access_token()
local htyuc = ngx.var.htyuc
@@ -21,9 +20,9 @@ local function get_access_token()
local remote_url = string.format("%s/api/v1/uc/wx_get_access_token", htyuc)
ngx.log(ngx.INFO, 'REMOTE URL -> ', remote_url)
ngx.log(ngx.INFO, "START REQUEST TO GET ACCESS TOKEN")
ngx.log(ngx.INFO, "START REQUEST TO GET ACCESS TOKEN -> ", pl.write(sudoerToken))
local resp, req_err = httpc:request_uri(
local req_status, resp, resp_err = pcall(httpc:request_uri,
remote_url,
{
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
@@ -32,23 +31,21 @@ local function get_access_token()
["HtyHost"] = wx_domain,
["HtySudoerToken"] = sudoerToken,
}
}
)
})
ngx.log(ngx.INFO, "RESP -> ", pl.write(resp))
ngx.log(ngx.ERR, "ERR -> ", req_err)
if resp == nil then
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *HTYUC*", req_err)
end
if 200 ~= resp.status then
ngx.log(ngx.ERR, "GET ACCESS TOKEN *FAILED*", req_err)
ngx.say(req_err)
ngx.exit(resp.status)
if req_status == true then
if 200 ~= resp.status then
ngx.log(ngx.ERR, "GET ACCESS TOKEN *FAILED*", pl.write(resp_err))
ngx.say(pl.write(resp_err))
ngx.exit(resp.status)
else
ngx.log(ngx.ERR, "RESP IS OK -> ", pl.write(resp.body))
return cjson.decode(resp.body).d
end
else
ngx.log(ngx.ERR, "RESP IS OK -> ", resp.body)
return cjson.decode(resp.body).d
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *HTYUC* ->", pl.write(resp))
end
end