# HG changeset patch # User Matthew Wild # Date 1598614859 -3600 # Node ID 51be24b16e8aebdb44061e67bb82bbaa23f443cc # Parent f103f59ea2b56453ad94a244bc308a2d12fa965c util.error: Allow optional tracebacks to be injected on errors This allows extra debug info to be provided for development purposes. diff -r f103f59ea2b5 -r 51be24b16e8a util/error.lua --- 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; } diff -r f103f59ea2b5 -r 51be24b16e8a util/startup.lua --- a/util/startup.lua Tue Aug 25 15:59:04 2020 +0100 +++ b/util/startup.lua Fri Aug 28 12:40:59 2020 +0100 @@ -546,6 +546,10 @@ return true; end +function startup.init_errors() + require "util.error".configure(config.get("*", "error_library")); +end + function startup.make_host(hostname) return { type = "local", @@ -577,6 +581,7 @@ startup.force_console_logging(); startup.init_logging(); startup.init_gc(); + startup.init_errors(); startup.setup_plugindir(); -- startup.setup_plugin_install_path(); startup.setup_datadir(); @@ -600,6 +605,7 @@ startup.read_config(); startup.init_logging(); startup.init_gc(); + startup.init_errors(); startup.sanity_check(); startup.sandbox_require(); startup.set_function_metatable();