From 701eb486093b770d1245c9b8dc53eff93c07523d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=94=B7?= Date: Sun, 26 Jun 2022 18:25:47 +0800 Subject: [PATCH] using pcall to catch exception --- scripts/wx_download.lua | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/scripts/wx_download.lua b/scripts/wx_download.lua index 1e79367..3b979ed 100644 --- a/scripts/wx_download.lua +++ b/scripts/wx_download.lua @@ -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