Comparison

util/error.lua @ 11051:08539aa129ee

util.error: Add configuration for including traceback in tostring()
author Matthew Wild <mwild1@gmail.com>
date Fri, 28 Aug 2020 12:51:40 +0100
parent 11050:51be24b16e8a
child 11053:04ad9555c799
comparison
equal deleted inserted replaced
11050:51be24b16e8a 11051:08539aa129ee
1
2 -- Library configuration (see configure())
3 local auto_inject_traceback = false;
4 local display_tracebacks = false;
5
6
1 local error_mt = { __name = "error" }; 7 local error_mt = { __name = "error" };
2 8
3 function error_mt:__tostring() 9 function error_mt:__tostring()
10 if display_tracebacks and self.context.traceback then
11 return ("error<%s:%s:%s:%s>"):format(self.type, self.condition, self.text or "", self.context.traceback);
12 end
4 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or ""); 13 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or "");
5 end 14 end
6 15
7 local function is_err(e) 16 local function is_err(e)
8 return getmetatable(e) == error_mt; 17 return getmetatable(e) == error_mt;
9 end 18 end
10 19
11 local auto_inject_traceback = false;
12
13 local function configure(opt) 20 local function configure(opt)
21 if opt.display_tracebacks ~= nil then
22 display_tracebacks = opt.display_tracebacks;
23 end
14 if opt.auto_inject_traceback ~= nil then 24 if opt.auto_inject_traceback ~= nil then
15 auto_inject_traceback = opt.auto_inject_traceback; 25 auto_inject_traceback = opt.auto_inject_traceback;
16 end 26 end
17 end 27 end
18 28