Diff

util/error.lua @ 11050:51be24b16e8a

util.error: Allow optional tracebacks to be injected on errors This allows extra debug info to be provided for development purposes.
author Matthew Wild <mwild1@gmail.com>
date Fri, 28 Aug 2020 12:40:59 +0100
parent 10501:e8186aba1583
child 11051:08539aa129ee
line wrap: on
line diff
--- a/util/error.lua	Tue Aug 25 15:59:04 2020 +0100
+++ b/util/error.lua	Fri Aug 28 12:40:59 2020 +0100
@@ -8,6 +8,14 @@
 	return getmetatable(e) == error_mt;
 end
 
+local auto_inject_traceback = false;
+
+local function configure(opt)
+	if opt.auto_inject_traceback ~= nil then
+		auto_inject_traceback = opt.auto_inject_traceback;
+	end
+end
+
 -- Do we want any more well-known fields?
 -- Or could we just copy all fields from `e`?
 -- Sometimes you want variable details in the `text`, how to handle that?
@@ -17,6 +25,12 @@
 
 local function new(e, context, registry)
 	local template = (registry and registry[e]) or e or {};
+	context = context or template.context or { _error_id = e };
+
+	if auto_inject_traceback then
+		context.traceback = debug.traceback("error stack", 2);
+	end
+
 	return setmetatable({
 		type = template.type or "cancel";
 		condition = template.condition or "undefined-condition";
@@ -57,4 +71,5 @@
 	coerce = coerce;
 	is_err = is_err;
 	from_stanza = from_stanza;
+	configure = configure;
 }