openresty 执行错误,有人能帮我分析下原因吗?

738 views
Skip to first unread message

罗厚付

unread,
Jan 30, 2016, 1:30:26 AM1/30/16
to openresty
stack traceback:
coroutine 0:
/opt/openresty/lua/test.lua: in function </opt/openresty/lua/test.lua:1> while sending to client, client: *.*.*.* , server: _, request: "POST /index.php HTTP/1.0", host: "*.*.*"
2016/01/28 23:27:36 [error] 15773#0: *16809007 lua entry thread aborted: runtime error: /opt/openresty/lua/test.lua:82: attempt to index local 'res1_body' (a userdata value)


 81    local res1_body = cjson.decode(res1.body)
 82    if res1_body and res1_body.code ~= 1 then
 83       return print(res1.body)
 84    end


DeJiang Zhu

unread,
Jan 30, 2016, 4:50:07 AM1/30/16
to open...@googlegroups.com
Hello,

你是用 OpenResty 自带的 cjson 么,什么版本?
这个错误,我没碰到过,方便的话,提供下 res1.body 的值

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

罗厚付

unread,
Jan 30, 2016, 8:46:30 PM1/30/16
to openresty
是的,我用的是自带的cjson,由于cjson在decode时会抛出异常。我改成了cjson.safe
因为这个错误出现的概率不高,线上环境我不好调试。

local cjson = require("cjson.safe")
cjson.encode_empty_table_as_object(false)

没有做其他的设置了。


在 2016年1月30日星期六 UTC+8下午5:50:07,doujiang写道:

罗厚付

unread,
Jan 30, 2016, 8:50:40 PM1/30/16
to openresty

顺便问一句,lua一般要怎么调试,我只学了一个月,有问题就用ngx.log。
我都是用vi进行编码,请问在linux下有没有好的IDE。



在 2016年1月30日星期六 UTC+8下午5:50:07,doujiang写道:
Hello,

DeJiang Zhu

unread,
Jan 30, 2016, 9:18:19 PM1/30/16
to open...@googlegroups.com
Hello,

一般情况下,建议使用 pcall
local ok, data = pcall(cjson.decode, str)

同时,即使在线上你也可以加日志的
if type(re1_body) == "userdata" then
    -- log here
end

Yichun Zhang (agentzh)

unread,
Feb 3, 2016, 7:56:47 PM2/3/16
to openresty
Hello!
貌似 cjson.decode() 返回了 ngx.null 值(这是一个 userdata),即 res1.body
的内容很可能是"null". 你的判断应该这么写:

if res1_body and res1_body ~= ngx.null and res1_body.code ~= 1 then

Regards,
-agentzh

Zoom.Quiet

unread,
Feb 3, 2016, 9:32:35 PM2/3/16
to openresty
春哥神级回复, 7*24 永远直击关键代码...

> Regards,
> -agentzh
>
> --
> --
> 邮件来自列表“openresty”,专用于技术讨论!
> 订阅: 请发空白邮件到 openresty...@googlegroups.com
> 发言: 请发邮件到 open...@googlegroups.com
> 退订: 请发邮件至 openresty+...@googlegroups.com
> 归档: http://groups.google.com/group/openresty
> 官网: http://openresty.org/
> 仓库: https://github.com/agentzh/ngx_openresty
> 教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html



--
Life's Pathetic, Let's Pythonic! 人生苦短, Python是岸!
俺: http://zoomquiet.io
授: http://creativecommons.org/licenses/by-sa/2.5/cn/
怒: 冗余不做,日子甭过!备份不做,十恶不赦!
KM keep growing environment culture which promoting organization be learnning!

罗厚付

unread,
Feb 4, 2016, 2:35:19 AM2/4/16
to openresty

多谢春哥指导。我去了解下nil NULL ngx.null。


在 2016年2月4日星期四 UTC+8上午8:56:47,agentzh写道:

Guanglin Lv

unread,
Feb 4, 2016, 4:35:11 AM2/4/16
to openresty

hi

cjson库在decode的时候会把json的null转为一个空的userdata类型,并不是nil类型。你可以用cjson.null与其比较进行确认,当然ngx.null也是可以的;


Thanks.
 
Reply all
Reply to author
Forward
0 new messages