I plan on using vim-plug with NeoVim. So, my init.lua file will have function calls such as
vim.fn['plug#begin'](vim.fn.stdpath('data') .. '/plugged')
vim.fn['plug#']('hoob3rt/lualine.nvim')
However, I don't want to assume vim-plug is definitely installed. I want my init.lua file to degrade gracefully if vim-plug is not installed, rather than throwing an error
E5113: Error while calling lua chunk: Vim:E117: Unknown function: plug#begin
stack traceback:
[C]: in function 'plug#begin'
/Users/andy/.config/nvim/init.lua:8: in main chunk
How can I check if the vim-plug functions exist before attempting to call them?
I tried print(vim.fn['plug#begin']) but that for some reason prints a non-null value: function: 0x0104ba36f0, even though the function doesn't exist.
I tried print(vim.fn['plug#begin']) but that for some reason prints a non-null value: function: 0x0104ba36f0, even though the function doesn't exist.
Presumably it's returning a function that throws the error you are getting. I would thus recommend using pcall:
local success, error = pcall(vim.fn['plug#begin'], vim.fn.stdpath('data') .. '/plugged')
if not success then --[[fail gracefully]] end
caveat: this will catch any error, so you'll probably want to perform some check like if error:find"Unknown function" then ... end to only catch this specific error.
could someone help me figure out what to look for in this stacktrace? I've never touched Erlang, so I don't really know where to start.
We're using v19.09 of ejabberd, and getting this error log for many users:
2020-03-14 15:29:31.096 [error] <0.2029.48>#mod_http_api:handle:257 REST API Error: set_presence([{<<"user">>,<<"53265363">>},{<<"host">>,<<"chat.us.com">>},{<<"resource">>,<<"ssid-3465">>},{<<"type">>,<<"available">>},{<<"show">>,<<"chat">>},{<<"status">>,<<"{\"current_lobby_id\":\"none\",\"status\":\"{\\r\
\\t\\\"rich_presence\\\": \\\"{\\\\r\\\
\\\\t\\\\\\\"rich_presence\\\\\\\": \\\\\\\"?s=PlayerStatusInMap?p0=21?p1=2?p2=3\\\\\\\",\\\\r\\\
\\\\t\\\\\\\"session_id\\\\\\\": \\\\\\\"ssid-3465-38a44f60-9df7-4c79-8c8e-1cc99e5039dd\\\\\\\"\\\\r\\\
}\\\",\\r\
\\t\\\"current_lobby_id\\\": \\\"{\\\\r\\\
\\\\t\\\\\\\"lobby_id\\\\\\\": \\\\\\\"c791cfaa-9fd3-4d0d-9d5c-1aeb9efb12e2#hangouts.chat.us.com\\\\\\\",\\\\r\\\
\\\\t\\\\\\\"privacy\\\\\\\": \\\\\\\"friends_only\\\\\\\"\\\\r\\\
}\\\"\\r\
}\",\"region\":\"\"}">>},{<<"priority">>,<<"0">>}]) -> exit:{noproc,{p1_server,call,[none,{set_presence,{presence,<<>>,available,<<>>,{jid,<<"953265363">>,<<"chat.us.com">>,<<"oak/ssid-3465-1cc99e5039dd">>,<<"1953265363">>,<<"chat.us.com">>,<<"4c79-8c8e-1cc99e5039dd">>},{jid,<<"1953265363">>,<<"chat.us.com">>,<<>>,<<"1953265363">>,<<"chat.us.com">>,<<>>},chat,[{text,<<"en">>,<<"{\"current_lobby_id\":\"none\",\"status\":\"{\\r\
\\t\\\"rich_presence\\\": \\\"{\\\\r\\\
\\\\t\\\\\\\"rich_presence\\\\\\\": \\\\\\\"?s=PlayerStatusInMap?p0=21?p1=2?p2=3\\\\\\\",\\\\r\\\
\\\\t\\\\\\\"session_id\\\\\\\": \\\\\\\"ssid-348c8e-1cc99e5039dd\\\\\\\"\\\\r\\\
}\\\",\\r\
\\t\\\"current_lobby_id\\\": \\\"{\\\\r\\\
\\\\t\\\\\\\"lobby_id\\\\\\\": \\\\\\\"c791cfaa-9fd3-4d0d-9d5c-1aeb9efb12e2#hangouts.chat.us.com\\\\\\\",\\\\r\\\
\\\\t\\\\\\\"privacy\\\\\\\": \\\\\\\"friends_only\\\\\\\"\\\\r\\\
}\\\"\\r\
}\",\"region\":\"\"}">>}],0,[],#{}}},1000]}} [{p1_server,call,3,[{file,"src/p1_server.erl"},{line,210}]},{mod_http_api,handle2,4,[{file,"src/mod_http_api.erl"},{line,267}]},{mod_http_api,handle,4,[{file,"src/mod_http_api.erl"},{line,228}]},{mod_http_api,perform_call,4,[{file,"src/mod_http_api.erl"},{line,188}]},{mod_http_api,process,2,[{file,"src/mod_http_api.erl"},{line,141}]},{ejabberd_http,process,2,[{file,"src/ejabberd_http.erl"},{line,366}]},{ejabberd_http,process_request,1,[{file,"src/ejabberd_http.erl"},{line,488}]},{ejabberd_http,process_header,2,[{file,"src/ejabberd_http.erl"},{line,286}]}]
Start with the actual error, which is buried in the middle:
exit:{noproc,{p1_server,call,[none,{set_presence,.....
So there is a noproc error, which occurred during a call to p1_server:call, where the first argument is none. (The call would look like p1_server:call(none, {set_presence,...}).) That is, we're asking the process called none to execute the command set_presence, which fails because there is no process registered with the name none.
Now let's look at the stack trace. I added line breaks:
[{p1_server,call,3,[{file,"src/p1_server.erl"},{line,210}]},
{mod_http_api,handle2,4,[{file,"src/mod_http_api.erl"},{line,267}]},
{mod_http_api,handle,4,[{file,"src/mod_http_api.erl"},{line,228}]},
{mod_http_api,perform_call,4,[{file,"src/mod_http_api.erl"},{line,188}]},
{mod_http_api,process,2,[{file,"src/mod_http_api.erl"},{line,141}]},
{ejabberd_http,process,2,[{file,"src/ejabberd_http.erl"},{line,366}]},
{ejabberd_http,process_request,1,[{file,"src/ejabberd_http.erl"},{line,488}]},
{ejabberd_http,process_header,2,[{file,"src/ejabberd_http.erl"},{line,286}]}]
The first line is where it discovers that there is no such process, on line 210 in p1_server.erl. But we're more interested in why we're passing none as the argument in the first place. The stack trace suggests that's happening somewhere in mod_http_api.erl, but at that point I'm getting lost - it seems like it's extracting the arguments from the command, but I don't understand where the none is coming from...
So for my pcall statements, I've been doing something like this
local status, err = pcall(fn)
if not status then
print(err)
print(debug.stacktrace())
end
This works fine for some basic stuff but the issue is that debug.stacktrace() returns the CURRENT relative stack trace, not the stack trace of the error. If the error within fn happened 10 levels down in the stack, then I wouldn't know where exactly it occurred, just that this pcall block is failing. I was wondering if there was a way to get the stack trace of the pcall and not the current stack trace. I tried debug.stacktrace(err) but it didn't make a difference.
You need to use xpcall to provide a custom function that will add the stacktrace to the error message. From PiL:
Frequently, when an error happens, we want more debug information than
only the location where the error occurred. At least, we want a
traceback, showing the complete stack of calls leading to the error.
When pcall returns its error message, it destroys part of the stack
(the part that went from it to the error point). Consequently, if we
want a traceback, we must build it before pcall returns. To do that,
Lua provides the xpcall function. Besides the function to be called,
it receives a second argument, an error handler function. In case of
errors, Lua calls that error handler before the stack unwinds, so that
it can use the debug library to gather any extra information it wants
about the error.
You may want to check this patch that extends pcall to include stacktrace.
As suggested in the comments, you can use local ok, res = xpcall(f, debug.traceback, args...) with Lua 5.2+ or LuaJIT (with Lua 5.2 compatibility turned on) and used a patch mentioned above for Lua 5.1.
The basic problem is (roughly) that pcall must unwind the stack so that your error handling code is reached. This gives two obvious ways to tackle the problem: Create the stack trace before unwinding, or move the (potentially) error-throwing code out of the way, so the stack frames don't have to be removed.
The first is handled by xpcall. This sets an error handler that can create a message while the stack is still intact. (Note that there are some situations where xpcall will not call the handler,1 so it's not suitable for cleanup code! But for stack traces, it's generally good enough.)
The second option (this works always2) is to preserve the stack by moving the code to a different coroutine. Instead of
local ok, r1, r2, etc = pcall( f, ... )
do
local co = coroutine.create( f )
local ok, r1, r2, etc = coroutine.resume( f, ... )
and now the stack (in co) is still preserved and can be queried by debug.traceback( co ) or other debug functions.
If you want the full stack trace, you'll then have to collect both the stack trace inside the coroutine and the stack trace outside of it (where you currently are) and then combine both while dropping the first line of the latter:
local full_tb = debug.traceback( co )
.. debug.traceback( ):sub( 17 ) -- drop 'stack traceback:' line
1 One situation in which the handler isn't called is for OOMs:
g = ("a"):rep( 1024*1024*1024 ) -- a gigabyte of 'a's
-- fail() tries to create a 32GB string – make it larger if that doesn't OOM
fail = load( "return "..("g"):rep( 32, ".." ), "(replicator)" )
-- plain call errors without traceback
fail()
--> not enough memory
-- xpcall does not call the handler either:
xpcall( fail, function(...) print( "handler:", ... ) return ... end, "foo" )
--> false not enough memory
-- (for comparison: here, the handler is called)
xpcall( error, function(...) print( "handler:", ... ) return ... end, "foo" )
--> handler: foo
-- false foo
-- coroutine preserves the stack anyway:
do
local co = coroutine.create( fail )
print( "result:", coroutine.resume( fail ) )
print( debug.traceback( co ) .. debug.traceback( ):sub( 17 ) )
end
--> result: false not enough memory
--> stack traceback:
-- [string "(replicator)"]:1: in function 'fail'
-- stdin:4: in main chunk
-- [C]: in ?
2 Well, at least as long as Lua itself doesn't crash.