json 解析的异常捕获

首先来看最最普通的一个 json 解析的例子(被解析的 json 字符串是错误的,缺少一个双引号):

  1. -- http://www.kyne.com.au/~mark/software/lua-cjson.php
  2. -- version: 2.1 devel
  3. local json = require("cjson")
  4. local str = [[ {"key:"value"} ]]
  5. local t = json.decode(str)
  6. ngx.say(" --> ", type(t))
  7. -- ... do the other things
  8. ngx.say("all fine")

代码执行错误日志如下:

  1. 2015/06/27 00:01:42 [error] 2714#0: *25 lua entry thread aborted: runtime error: ...ork/git/github.com/lua-resty-memcached-server/t/test.lua:8: Expected colon but found invalid token at character 9
  2. stack traceback:
  3. coroutine 0:
  4. [C]: in function 'decode'
  5. ...ork/git/github.com/lua-resty-memcached-server/t/test.lua:8: in function <...ork/git/github.com/lua-resty-memcached-server/t/test.lua:1>, client: 127.0.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "127.0.0.1:8001"

这可不是期望结果:decode 失败,500 错误直接退了。改良了一下代码:

  1. local json = require("cjson")
  2. local function _json_decode(str)
  3. return json.decode(str)
  4. end
  5. function json_decode( str )
  6. local ok, t = pcall(_json_decode, str)
  7. if not ok then
  8. return nil
  9. end
  10. return t
  11. end

如果需要在 Lua 中处理错误,必须使用函数 pcall(protected call)来包装需要执行的代码。
pcall 接收一个函数和要传递给后者的参数,并执行,执行结果:有错误、无错误;返回值 true 或者 false, errorinfo。pcall 以一种“保护模式”来调用第一个参数,因此 pcall 可以捕获函数执行中的任何错误。有兴趣的同学,请更多了解下 Lua 中的异常处理。

另外,可以使用 CJSON 2.1.0,该版本新增一个 cjson.safe 模块接口,该接口兼容 cjson 模块,并且在解析错误时不抛出异常,而是返回 nil

  1. local json = require("cjson.safe")
  2. local str = [[ {"key:"value"} ]]
  3. local t = json.decode(str)
  4. if t then
  5. ngx.say(" --> ", type(t))
  6. end