run_command: add prototype wrapper around command parsing.

This commit is contained in:
Starbeamrainbowlabs 2024-10-14 18:51:33 +01:00
parent 39b3ef428d
commit 938a617dc3
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 38 additions and 20 deletions

View file

@ -40,11 +40,13 @@ local function send_error(player_name, cmdname, msg, stack_trace)
"[//", cmdname, "] Error: ", "[//", cmdname, "] Error: ",
msg, msg,
"\n", "\n",
"Please report this by opening an issue on GitHub! Bug report link:\n", "Please report this by opening an issue on GitHub! Bug report link (ctrl + click):\n",
"https://github.com/sbrl/Minetest-WorldEditAdditions/issues/new?title=", "https://github.com/sbrl/Minetest-WorldEditAdditions/issues/new?title=",
wea_c.format.escape(stack_trace:match("^[^\n]+")), -- extract 1st line & escape wea_c.format.escape(stack_trace:match("^[^\n]+")), -- extract 1st line & escape
"&body=", "&body=",
wea_c.format.escape([[## Describe the bug wea_c.format.escape(table.concat({
[[## Describe the bug
What's the bug? Be clear and detailed but concise in our explanation. Don't forget to include any context, error messages, logs, and screenshots required to understand the issue if applicable. What's the bug? Be clear and detailed but concise in our explanation. Don't forget to include any context, error messages, logs, and screenshots required to understand the issue if applicable.
## Reproduction steps ## Reproduction steps
@ -63,16 +65,20 @@ Steps to reproduce the behaviour:
Please add any other additional specific system information here too if you think it would help. Please add any other additional specific system information here too if you think it would help.
## Stack trace ## Stack trace
- Command name: ]]), - **Command name:** ]],
wea_c.format.escape(cmdname), cmdname,
wea_c.format.escape("```\n"), "\n",
wea_c.format.escape(stack_trace), "```\n",
wea_c.format.escape("\n```"), stack_trace,
"```\n",
}, "")),
"\n",
"-------------------------------------\n", "-------------------------------------\n",
"*** Stack trace ***\n", "*** Stack trace ***\n",
stack_trace, stack_trace,
"\n", "\n",
"-------------------------------------" "-------------------------------------\n"
}, "") }, "")
print("DEBUG:player_notify player_name", player_name, "msg_compiled", msg_compiled) print("DEBUG:player_notify player_name", player_name, "msg_compiled", msg_compiled)
@ -157,21 +163,25 @@ local function run_command(cmdname, options, player_name, paramtext)
wea_c:emit("pre-parse", tbl_event) wea_c:emit("pre-parse", tbl_event)
local parse_result, error_message local parse_result
local did_error = false -- local did_error = false
xpcall(function() local success_xpcall, error_message = xpcall(function()
parse_result = { options.parse(paramtext)} parse_result = { options.parse(paramtext) }
end, function(error_raw) end, debug.traceback
--[[function(error_raw)
did_error = true did_error = true
error_message = error_raw error_message = error_raw
print("DEBUG:parse_result>>error", error_raw, "stack trace", debug.traceback()) print("DEBUG:parse_result>>error", error_raw, "stack trace", debug.traceback())
end) end]]--
if did_error then )
if not success_xpcall then
print("DEBUG:parse_result EXIT_DUE_TO_ERROR") print("DEBUG:parse_result EXIT_DUE_TO_ERROR")
send_error(player_name, cmdname, "The command crashed when parsing the arguments.", error_message) send_error(player_name, cmdname, "The command crashed when parsing the arguments.", error_message)
print("DEBUG:parse_result EXIT_DUE_TO_ERROR __final_call__") print("DEBUG:parse_result EXIT_DUE_TO_ERROR __final_call__")
return false return false
end -- handling is wrapped with xpcall() end
local success = table.remove(parse_result, 1) local success = table.remove(parse_result, 1)
if not success then if not success then

View file

@ -3,16 +3,24 @@
-- decodeURIComponent() implementation -- decodeURIComponent() implementation
-- Ref https://stackoverflow.com/a/78225561/1460422 -- Ref https://stackoverflow.com/a/78225561/1460422
-- Adapted by @sbrl to:
-- - Print leading 0 behind escape codes as it should
-- - Also escape ' and #
-- TODO this doesn't work. It replaces \n with %A instead of %0A, though we don't know if that's a problem or not -- TODO this doesn't work. It replaces \n with %A instead of %0A, though we don't know if that's a problem or not
-- it also doesn't handle quotes even though we've clearly got them in the Lua pattern -- it also doesn't handle quotes even though we've clearly got them in the Lua pattern
local function _escape_char(char) local function _escape_char(char)
print("_escape_char char", char, "result", string.format('%%%0X', string.byte(char))) print("_escape_char char", char, "result", string.format('%%%02X', string.byte(char)))
return string.format('%%%0X', string.byte(char)) return string.format('%%%02X', string.byte(char))
end end
local function escape(uri) --- Escape the given string for use in a url.
return (string.gsub(uri, "[^%a%d%-_%.!~%*'%(%);/%?:@&=%+%$,#]", _escape_char)) -- In other words, like a space turns into %20.
-- Similar to Javascript's `encodeURIComponent()`.
-- @param string str The string to escape.
-- @returns string The escaped string.
local function escape(str)
return (string.gsub(str, "[^%a%d%-_%.!~%*%(%);/%?:@&=%+%$,]", _escape_char))
end end
return escape return escape