If you need to handle errors in Lua, you must use the pcall (protected call) function to encapsulate your code.
The pcall function calls its first argument in protected mode, so that it catches any errors while the function is running. If there are no errors, pcall returns true, plus any values returned by the call. Otherwise, it returns false, plus the error message.
local status, err = pcall(function()
error({code=121})
end)
print(err.code) --> 121