From f5752cd2312f1c5344656e5044a997fbfdca473a Mon Sep 17 00:00:00 2001 From: Reuh Date: Mon, 17 May 2021 15:19:55 +0200 Subject: [PATCH] Allow rewriting without new traceback --- README.md | 2 +- candran.can | 4 ++-- candran.lua | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a3ff232..397d002 100644 --- a/README.md +++ b/README.md @@ -597,7 +597,7 @@ Please note that Candran can only wrap code directly called from Candran; if an If you want Candran to always wrap errors, you will need to wrap your whole code in a `xpcall`: `xpcall(func, candran.messageHandler)`. -* ```candran.messageHandler(message)```: the error message handler used by Candran. Given `message` the Lua error string, returns full Candran traceback where soure files and lines are rewritten to their Candran source. You can use it as is in xpcall as a message handler. +* ```candran.messageHandler(message[, noTraceback])```: the error message handler used by Candran. Given `message` the Lua error string, returns full Candran traceback where soure files and lines are rewritten to their Candran source. You can use it as is in xpcall as a message handler. If `noTraceback` is `true`, Candran will only rewrite `message` and not add a new traceback. Also note that the Candran message handler will add a new, rewritten, stacktrace to the error message; it can't replace the default Lua one. You will therefore see two stacktraces when raising an error, the last one being the Lua one and can be ignored. diff --git a/candran.can b/candran.can index 3b433ac..57f115d 100644 --- a/candran.can +++ b/candran.can @@ -265,8 +265,8 @@ end --- Candran error message handler. -- Use it in xpcall to rewrite stacktraces to display Candran source file lines instead of compiled Lua lines. -function candran.messageHandler(message) - if not message:match("\nstack traceback:\n") then +function candran.messageHandler(message, noTraceback) + if not noTraceback and not message:match("\nstack traceback:\n") then message = debug.traceback(message, 2) end return message:gsub("(\n?%s*)([^\n]-)%:(%d+)%:", function(indentation, source, line) diff --git a/candran.lua b/candran.lua index 514e2d8..51a26ad 100644 --- a/candran.lua +++ b/candran.lua @@ -6586,8 +6586,8 @@ else -- candran.can:260 return f() -- candran.can:262 end -- candran.can:262 end -- candran.can:262 -candran["messageHandler"] = function(message) -- candran.can:268 -if not message:match("\ +candran["messageHandler"] = function(message, noTraceback) -- candran.can:268 +if not noTraceback and not message:match("\ stack traceback:\ ") then -- candran.can:269 message = debug["traceback"](message, 2) -- candran.can:270