Question

my setup board uses lighttpd to provide web UI to the board. It uses lua and JS to draw logic.

What I am seeing is If I enter an URL as "IPofboard/somejunkhere"; its properly throwing "404 not found" But when I fire "IPofboard/somejunk.lp" (which is some junk lua file); It produces an "assert" error for file not found. Thats how lua works.

But I want to modify/override this assert to show same custom message as "404 not found" any idea?

I am new to lua. Is it even doable?

Was it helpful?

Solution

As lhf mentions, it is very easy to redefine any function in Lua, but I think this may not be what you need. The issue is that after you do

local origAssert = assert
assert = function(message) 
    do something (possibly using origAssert)
end

then every function call that uses assert will use your new assert function, which is probably not what you want. Instead, you can call your function in "protected" mode: this will trap the assertion as an error message, and you can then decide what to do. For example,

ok, ret1, ret2 = pcall(yourFunction, arg1)
if not ok then 
    do something, possibly print ret1 (the error message)
end

Same thing if you are requiring a module that does some initialization:

ok, module = pcall(require, yourModuleName)
if not ok then 
    print("ERROR:", module) -- if not ok then module is err message
end

OTHER TIPS

I'm not familiar with how lighttpd embeds Lua, but in Lua you can redefine anything, including functions from the standard Lua library, such as assert.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top