file: kong/tools/utils.lua
funtion: load_module_if_exists()
一、注释
Try to load a module.
Will not throw an error if the module was not found, but will throw an error if the loading failed for another reason
二、原因
require加载模块时,加载错误就会报错,不区别对待下面两种情况
- 模块找不到
- 模块中的代码加载错误
load_module_if_exists()
这个函数对这两种情况做了区别,下面举例说明一下:
模块找不到
$ lua -e "zz = require('zz')"
lua: (command line):1: module 'zz' not found:
no field package.preload['zz']
no file '/usr/local/share/lua/5.2/zz.lua'
no file '/usr/local/share/lua/5.2/zz/init.lua'
no file '/usr/local/lib/lua/5.2/zz.lua'
no file '/usr/local/lib/lua/5.2/zz/init.lua'
no file './zz.lua'
no file '/usr/local/lib/lua/5.2/zz.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './zz.so'
stack traceback:
[C]: in function 'require'
(command line):1: in main chunk
[C]: in ?
模块代码加载语法错误
lua -e "require('zz')"
lua: ./zz.lua:5: attempt to perform arithmetic on field 'a' (a nil value)
stack traceback:
./zz.lua:5: in main chunk
[C]: in function 'require'
(command line):1: in main chunk
[C]: in ?
网友评论