From 938a617dc35892c11073bd954ed2221d8bcef266 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Mon, 14 Oct 2024 18:51:33 +0100 Subject: [PATCH] run_command: add prototype wrapper around command parsing. --- worldeditadditions_core/core/run_command.lua | 42 ++++++++++++------- .../utils/format/escape.lua | 16 +++++-- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/worldeditadditions_core/core/run_command.lua b/worldeditadditions_core/core/run_command.lua index a2bac27..9ec36c3 100644 --- a/worldeditadditions_core/core/run_command.lua +++ b/worldeditadditions_core/core/run_command.lua @@ -40,11 +40,13 @@ local function send_error(player_name, cmdname, msg, stack_trace) "[//", cmdname, "] Error: ", msg, "\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=", wea_c.format.escape(stack_trace:match("^[^\n]+")), -- extract 1st line & escape "&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. ## 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. ## Stack trace -- Command name: ]]), - wea_c.format.escape(cmdname), - wea_c.format.escape("```\n"), - wea_c.format.escape(stack_trace), - wea_c.format.escape("\n```"), +- **Command name:** ]], + cmdname, + "\n", + "```\n", + stack_trace, + "```\n", + }, "")), + + "\n", "-------------------------------------\n", "*** Stack trace ***\n", stack_trace, "\n", - "-------------------------------------" + "-------------------------------------\n" }, "") 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) - local parse_result, error_message - local did_error = false - xpcall(function() - parse_result = { options.parse(paramtext)} - end, function(error_raw) + local parse_result + -- local did_error = false + local success_xpcall, error_message = xpcall(function() + parse_result = { options.parse(paramtext) } + end, debug.traceback + + --[[function(error_raw) did_error = true error_message = error_raw print("DEBUG:parse_result>>error", error_raw, "stack trace", debug.traceback()) - end) - if did_error then + end]]-- + ) + + if not success_xpcall then print("DEBUG:parse_result EXIT_DUE_TO_ERROR") send_error(player_name, cmdname, "The command crashed when parsing the arguments.", error_message) print("DEBUG:parse_result EXIT_DUE_TO_ERROR __final_call__") return false - end -- handling is wrapped with xpcall() + end local success = table.remove(parse_result, 1) if not success then diff --git a/worldeditadditions_core/utils/format/escape.lua b/worldeditadditions_core/utils/format/escape.lua index 42b2baa..ccde77c 100644 --- a/worldeditadditions_core/utils/format/escape.lua +++ b/worldeditadditions_core/utils/format/escape.lua @@ -3,16 +3,24 @@ -- decodeURIComponent() implementation -- 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 -- it also doesn't handle quotes even though we've clearly got them in the Lua pattern local function _escape_char(char) - print("_escape_char char", char, "result", string.format('%%%0X', string.byte(char))) - return string.format('%%%0X', string.byte(char)) + print("_escape_char char", char, "result", string.format('%%%02X', string.byte(char))) + return string.format('%%%02X', string.byte(char)) end -local function escape(uri) - return (string.gsub(uri, "[^%a%d%-_%.!~%*'%(%);/%?:@&=%+%$,#]", _escape_char)) +--- Escape the given string for use in a url. +-- 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 return escape \ No newline at end of file