Software / code / prosody
Comparison
util/error.lua @ 11224:8143fd2f138b
util.error: Switch to util.debug traceback tables and remove display_tracebacks option
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 09 Dec 2020 14:51:40 +0000 |
| parent | 11223:b8589256b404 |
| child | 12975:d10957394a3c |
| child | 13078:6da83deb8d7f |
comparison
equal
deleted
inserted
replaced
| 11223:b8589256b404 | 11224:8143fd2f138b |
|---|---|
| 1 local id = require "util.id"; | 1 local id = require "util.id"; |
| 2 | |
| 3 local util_debug; -- only imported on-demand | |
| 2 | 4 |
| 3 -- Library configuration (see configure()) | 5 -- Library configuration (see configure()) |
| 4 local auto_inject_traceback = false; | 6 local auto_inject_traceback = false; |
| 5 local display_tracebacks = false; | |
| 6 | |
| 7 | 7 |
| 8 local error_mt = { __name = "error" }; | 8 local error_mt = { __name = "error" }; |
| 9 | 9 |
| 10 function error_mt:__tostring() | 10 function error_mt:__tostring() |
| 11 if display_tracebacks and self.context.traceback then | |
| 12 return ("error<%s:%s:%s:%s>"):format(self.type, self.condition, self.text or "", self.context.traceback); | |
| 13 end | |
| 14 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or ""); | 11 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or ""); |
| 15 end | 12 end |
| 16 | 13 |
| 17 local function is_error(e) | 14 local function is_error(e) |
| 18 return getmetatable(e) == error_mt; | 15 return getmetatable(e) == error_mt; |
| 19 end | 16 end |
| 20 | 17 |
| 21 local function configure(opt) | 18 local function configure(opt) |
| 22 if opt.display_tracebacks ~= nil then | |
| 23 display_tracebacks = opt.display_tracebacks; | |
| 24 end | |
| 25 if opt.auto_inject_traceback ~= nil then | 19 if opt.auto_inject_traceback ~= nil then |
| 26 auto_inject_traceback = opt.auto_inject_traceback; | 20 auto_inject_traceback = opt.auto_inject_traceback; |
| 21 if auto_inject_traceback then | |
| 22 util_debug = require "util.debug"; | |
| 23 end | |
| 27 end | 24 end |
| 28 end | 25 end |
| 29 | 26 |
| 30 -- Do we want any more well-known fields? | 27 -- Do we want any more well-known fields? |
| 31 -- Or could we just copy all fields from `e`? | 28 -- Or could we just copy all fields from `e`? |
| 51 end | 48 end |
| 52 end | 49 end |
| 53 context = context or {}; | 50 context = context or {}; |
| 54 | 51 |
| 55 if auto_inject_traceback then | 52 if auto_inject_traceback then |
| 56 context.traceback = debug.traceback("error stack", 2); | 53 context.traceback = util_debug.get_traceback_table(nil, 2); |
| 57 end | 54 end |
| 58 | 55 |
| 59 local error_instance = setmetatable({ | 56 local error_instance = setmetatable({ |
| 60 instance_id = id.short(); | 57 instance_id = id.short(); |
| 61 | 58 |