Search code examples
luaffiluajit

luaJIT error handling with FFI calls a non-existent method


What I want to avoid is to capture/ignore the exception when FFI calls a non-existent method.

For example, the following code calls the non_existent_method. However, pcall cannot handle the error.

local ffi = require "ffi"
local C = ffi.C

ffi.cdef [[
    int non_existent_method(int num);
]]

local ok, ret = pcall(C.non_existent_method, 1)

if not ok then
    return
end

I got the following error with OpenResty/lua-nginx-module.

lua entry thread aborted: runtime error: dlsym(RTLD_DEFAULT, awd): symbol not found

Solution

  • One possible solution would be wrapping the C.non_existent_method call with a lua function.

    For example

    
    local ok, ret = pcall(function()
        return C.non_existent_method(len)
    end)
    
    if not ok then
        ......
    end