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 sudoerToken = ngx.req.get_headers().HtySudoerToken
local htyhostHeader = ngx.req.get_headers().HtyHost local htyhostHeader = ngx.req.get_headers().HtyHost
local function get_access_token() local function get_access_token()
local htyuc = ngx.var.htyuc 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) 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, '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, remote_url,
{ {
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书 ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
@@ -32,23 +31,21 @@ local function get_access_token()
["HtyHost"] = wx_domain, ["HtyHost"] = wx_domain,
["HtySudoerToken"] = sudoerToken, ["HtySudoerToken"] = sudoerToken,
} }
} })
)
ngx.log(ngx.INFO, "RESP -> ", pl.write(resp)) ngx.log(ngx.INFO, "RESP -> ", pl.write(resp))
ngx.log(ngx.ERR, "ERR -> ", req_err)
if resp == nil then if req_status == true then
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *HTYUC*", req_err) if 200 ~= resp.status then
end ngx.log(ngx.ERR, "GET ACCESS TOKEN *FAILED*", pl.write(resp_err))
ngx.say(pl.write(resp_err))
if 200 ~= resp.status then ngx.exit(resp.status)
ngx.log(ngx.ERR, "GET ACCESS TOKEN *FAILED*", req_err) else
ngx.say(req_err) ngx.log(ngx.ERR, "RESP IS OK -> ", pl.write(resp.body))
ngx.exit(resp.status) return cjson.decode(resp.body).d
end
else else
ngx.log(ngx.ERR, "RESP IS OK -> ", resp.body) ngx.log(ngx.ERR, "FAILED TO CONNECT TO *HTYUC* ->", pl.write(resp))
return cjson.decode(resp.body).d
end end
end end