Skip to content

Commit fbb361c

Browse files
committed
删除json模块默认的pcall封装,修改几个用到了的地方
1 parent ecb0680 commit fbb361c

3 files changed

Lines changed: 10 additions & 31 deletions

File tree

file.lua

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ function FILE.load(path,args)
5252
error("FILE.load: Compile error: "..err_mes)
5353
end
5454
elseif mode=='json' then
55-
local res=JSON.decode(s)
56-
if res then
57-
return res
58-
end
59-
error("FILE.load: Decode error")
55+
local res,data=pcall(JSON.decode,s)
56+
return res and data or error("FILE.load: Decode error")
6057
elseif mode=='string' then
6158
return s
6259
else
@@ -75,7 +72,7 @@ end
7572
function FILE.save(data,path,args)
7673
if not args then args='' end
7774
if STRING.sArg(args,'-d') and fs.getInfo(path) then
78-
error("FILE.save: Duplicate")
75+
error("FILE.save: File already exist")
7976
end
8077

8178
if type(data)=='table' then
@@ -86,13 +83,12 @@ function FILE.save(data,path,args)
8683
data='return'..TABLE.dumpDeflate(data)
8784
end
8885
if not data then
89-
error("FILE.save: Encode error")
86+
error("FILE.save: Luaon-encoding error")
9087
end
9188
else
92-
data=JSON.encode(data)
93-
if not data then
94-
error("FILE.save: Encode error")
95-
end
89+
local res
90+
res,data=pcall(JSON.encode,data)
91+
assert(res,"FILE.save: Json-encoding error")
9692
end
9793
else
9894
data=tostring(data)

http.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ function HTTP.request(arg)
102102

103103
if arg.body~=nil then
104104
assertf(type(arg.body)=='table',"HTTP.request(arg): arg.body need table, got %s",arg.body)
105-
arg.body=JSON.encode(arg.body)
105+
local res
106+
res,arg.body=pcall(JSON.encode,arg.body)
107+
assert(res,"HTTP.request(arg): arg.body json-encoding error")
106108
if not arg.headers then arg.headers={} end
107109
TABLE.cover({
108110
['Content-Type']="application/json",

init.lua

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,6 @@ COLOR= require'Zenitha.color'
128128
DEBUG= require'Zenitha.debug'
129129
LOG= require'Zenitha.log'
130130
JSON= require'Zenitha.json'
131-
do -- Add pcall & MSG for JSON lib
132-
local encode,decode=JSON.encode,JSON.decode
133-
function JSON.encode(val)
134-
local a,b=pcall(encode,val)
135-
if a then
136-
return b
137-
elseif MSG then
138-
MSG.traceback()
139-
end
140-
end
141-
function JSON.decode(str)
142-
local a,b=pcall(decode,str)
143-
if a then
144-
return b
145-
elseif MSG then
146-
MSG.traceback()
147-
end
148-
end
149-
end
150131

151132
-- Pure lua modules (complex, with update/draw)
152133
LANG= require'Zenitha.languages'

0 commit comments

Comments
 (0)