From 464121c5b7092f2521d21be390b01173e28fbd00 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 30 Dec 2018 12:55:58 +0000 Subject: util.error: Add new util library for structured errors --- util/error.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 util/error.lua (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua new file mode 100644 index 00000000..ed61793f --- /dev/null +++ b/util/error.lua @@ -0,0 +1,40 @@ +local error_mt = { __name = "error" }; + +function error_mt:__tostring() + return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text); +end + +local function is_err(e) + return getmetatable(e) == error_mt; +end + +local function new(e, context, registry) + local template = (registry and registry[e]) or e or {}; + return setmetatable({ + type = template.type or "cancel"; + condition = template.condition or "undefined-condition"; + text = template.text; + + context = context or template.context or { _error_id = e }; + }, error_mt); +end + +local function coerce(ok, err, ...) + if ok or is_err(err) then + return ok, err, ...; + end + + local new_err = setmetatable({ + native = err; + + type = "cancel"; + condition = "undefined-condition"; + }, error_mt); + return ok, new_err, ...; +end + +return { + new = new; + coerce = coerce; + is_err = is_err; +} -- cgit v1.2.3 From 3b3af4805c4323fe915ea4ac5aa09110a19e7676 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 30 Dec 2018 20:30:59 +0100 Subject: util.error: Add a function for creating an error object from an error stanza --- util/error.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index ed61793f..344dd274 100644 --- a/util/error.lua +++ b/util/error.lua @@ -33,8 +33,20 @@ local function coerce(ok, err, ...) return ok, new_err, ...; end +local function from_stanza(stanza, context) + local error_type, condition, text = stanza:get_error(); + return setmetatable({ + type = error_type or "cancel"; + condition = condition or "undefined-condition"; + text = text; + + context = context or { stanza = stanza }; + }, error_mt); +end + return { new = new; coerce = coerce; is_err = is_err; + from_stanza = from_stanza; } -- cgit v1.2.3 From 078178a3bac5c0ed69d629119d1c84bac3eda480 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 10 Jul 2019 17:04:36 +0200 Subject: util.error: Fix traceback due to missing text field --- util/error.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 344dd274..23551fe2 100644 --- a/util/error.lua +++ b/util/error.lua @@ -1,7 +1,7 @@ local error_mt = { __name = "error" }; function error_mt:__tostring() - return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text); + return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or ""); end local function is_err(e) -- cgit v1.2.3 From 03adb505558555cd2449bea0ae83c0a89c82a399 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 1 Nov 2019 18:31:12 +0100 Subject: util.error: Add well-known field 'code' in error templates Intended to be for HTTP-ish numeric status codes --- util/error.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 23551fe2..9ebfa6ab 100644 --- a/util/error.lua +++ b/util/error.lua @@ -14,6 +14,7 @@ local function new(e, context, registry) type = template.type or "cancel"; condition = template.condition or "undefined-condition"; text = template.text; + code = template.code or 500; context = context or template.context or { _error_id = e }; }, error_mt); -- cgit v1.2.3 From a55d83ceb62bf0e8280829629782bc367ab185b8 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 8 Dec 2019 17:00:45 +0100 Subject: util.error: Write down some thoughts in comments --- util/error.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 9ebfa6ab..461bf385 100644 --- a/util/error.lua +++ b/util/error.lua @@ -8,6 +8,13 @@ local function is_err(e) return getmetatable(e) == error_mt; 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? +-- Translations? +-- Should the `type` be restricted to the stanza error types or free-form? +-- What to set `type` to for stream errors or SASL errors? Those don't have a 'type' attr. + local function new(e, context, registry) local template = (registry and registry[e]) or e or {}; return setmetatable({ -- cgit v1.2.3 From 87d0125802ce38410e5d429c90760802dec0397c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 14 Dec 2019 20:28:44 +0100 Subject: util.error: Move default for numeric error code to net.http.server Stanza errors can also have numbers but these are a legacy thing and rarely used, except in MUC. HTTP errors on the other hand always have a number. --- util/error.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 461bf385..ca960dd9 100644 --- a/util/error.lua +++ b/util/error.lua @@ -21,7 +21,7 @@ local function new(e, context, registry) type = template.type or "cancel"; condition = template.condition or "undefined-condition"; text = template.text; - code = template.code or 500; + code = template.code; context = context or template.context or { _error_id = e }; }, error_mt); -- cgit v1.2.3 From 75079963714b6633847f05d015e1adcfde11a8f6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 28 Aug 2020 12:40:59 +0100 Subject: util.error: Allow optional tracebacks to be injected on errors This allows extra debug info to be provided for development purposes. --- util/error.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index ca960dd9..b83302fa 100644 --- a/util/error.lua +++ b/util/error.lua @@ -8,6 +8,14 @@ local function is_err(e) 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 @@ end 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 @@ return { coerce = coerce; is_err = is_err; from_stanza = from_stanza; + configure = configure; } -- cgit v1.2.3 From 5c04c5ce383eca1e9b544885c67a976251ca12db Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 28 Aug 2020 12:51:40 +0100 Subject: util.error: Add configuration for including traceback in tostring() --- util/error.lua | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index b83302fa..ffdc4eec 100644 --- a/util/error.lua +++ b/util/error.lua @@ -1,6 +1,15 @@ + +-- Library configuration (see configure()) +local auto_inject_traceback = false; +local display_tracebacks = false; + + local error_mt = { __name = "error" }; function error_mt:__tostring() + if display_tracebacks and self.context.traceback then + return ("error<%s:%s:%s:%s>"):format(self.type, self.condition, self.text or "", self.context.traceback); + end return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or ""); end @@ -8,9 +17,10 @@ local function is_err(e) return getmetatable(e) == error_mt; end -local auto_inject_traceback = false; - local function configure(opt) + if opt.display_tracebacks ~= nil then + display_tracebacks = opt.display_tracebacks; + end if opt.auto_inject_traceback ~= nil then auto_inject_traceback = opt.auto_inject_traceback; end -- cgit v1.2.3 From df7cf5c388e5be03dfc5f57229b8f1ee7b355ded Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 28 Aug 2020 13:54:16 +0200 Subject: util.error: Add a 'source' parameter where origin module can be mentioned --- util/error.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index ffdc4eec..600c9e5e 100644 --- a/util/error.lua +++ b/util/error.lua @@ -33,7 +33,7 @@ end -- Should the `type` be restricted to the stanza error types or free-form? -- What to set `type` to for stream errors or SASL errors? Those don't have a 'type' attr. -local function new(e, context, registry) +local function new(e, context, registry, source) local template = (registry and registry[e]) or e or {}; context = context or template.context or { _error_id = e }; @@ -48,6 +48,7 @@ local function new(e, context, registry) code = template.code; context = context or template.context or { _error_id = e }; + source = source; }, error_mt); end -- cgit v1.2.3 From a114e8cf0237266237412c98de3fbb2610e7a31b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 28 Aug 2020 13:55:05 +0200 Subject: util.error: Add a wrapper for common parameters Lets you set up source and registry once per module --- util/error.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 600c9e5e..c46f8790 100644 --- a/util/error.lua +++ b/util/error.lua @@ -52,6 +52,12 @@ local function new(e, context, registry, source) }, error_mt); end +local function init(source, registry) + return function (e, context) + return new(e, context, registry, source); + end +end + local function coerce(ok, err, ...) if ok or is_err(err) then return ok, err, ...; @@ -79,6 +85,7 @@ end return { new = new; + init = init; coerce = coerce; is_err = is_err; from_stanza = from_stanza; -- cgit v1.2.3 From 646bd1da3df6ca12ea02e036ed2af9b8e9eb173d Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 25 Sep 2020 12:18:18 +0100 Subject: util.error: Add unique 'instance_id' to error objects --- util/error.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index c46f8790..2c96bc03 100644 --- a/util/error.lua +++ b/util/error.lua @@ -1,3 +1,4 @@ +local id = require "util.id"; -- Library configuration (see configure()) local auto_inject_traceback = false; @@ -42,6 +43,7 @@ local function new(e, context, registry, source) end return setmetatable({ + instance_id = id.short(); type = template.type or "cancel"; condition = template.condition or "undefined-condition"; text = template.text; -- cgit v1.2.3 From 73e87f3901e29b91662717b6cddb49b6f5d27858 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 25 Sep 2020 12:19:30 +0100 Subject: util.error: Simplify error creation - remove ability to set context from templates, and remove default context --- util/error.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 2c96bc03..ade1bb3b 100644 --- a/util/error.lua +++ b/util/error.lua @@ -36,7 +36,7 @@ end local function new(e, context, registry, source) local template = (registry and registry[e]) or e or {}; - context = context or template.context or { _error_id = e }; + context = context or {}; if auto_inject_traceback then context.traceback = debug.traceback("error stack", 2); -- cgit v1.2.3 From 9c709d29fd4e37aeb41a9959889347e8d579273b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 25 Sep 2020 12:27:45 +0100 Subject: util.error: Minor tweaks to error creation code to prepare for future changes --- util/error.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index ade1bb3b..4079a876 100644 --- a/util/error.lua +++ b/util/error.lua @@ -42,16 +42,19 @@ local function new(e, context, registry, source) context.traceback = debug.traceback("error stack", 2); end - return setmetatable({ + local error_instance = setmetatable({ instance_id = id.short(); + type = template.type or "cancel"; condition = template.condition or "undefined-condition"; text = template.text; code = template.code; - context = context or template.context or { _error_id = e }; + context = context; source = source; }, error_mt); + + return error_instance; end local function init(source, registry) -- cgit v1.2.3 From 891a03885a2b183425f7318a89e8d91319b55681 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 25 Sep 2020 12:32:43 +0100 Subject: util.error: Have init() return an object to allow API extensibility via additional methods --- util/error.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 4079a876..00192273 100644 --- a/util/error.lua +++ b/util/error.lua @@ -58,9 +58,11 @@ local function new(e, context, registry, source) end local function init(source, registry) - return function (e, context) - return new(e, context, registry, source); - end + return { + new = function (e, context) + return new(e, context, registry, source); + end; + }; end local function coerce(ok, err, ...) -- cgit v1.2.3 From 2595c39a8a79ea65a100709877548efeb0c1e061 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 25 Sep 2020 12:38:58 +0100 Subject: util.error: Switch coerce() to use new() and change 'native' to context field 'wrapped_error' --- util/error.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 00192273..976bc355 100644 --- a/util/error.lua +++ b/util/error.lua @@ -70,12 +70,10 @@ local function coerce(ok, err, ...) return ok, err, ...; end - local new_err = setmetatable({ - native = err; + local new_err = new({ + type = "cancel", condition = "undefined-condition" + }, { wrapped_error = err }); - type = "cancel"; - condition = "undefined-condition"; - }, error_mt); return ok, new_err, ...; end -- cgit v1.2.3 From a34633771db29e751058f93b1abbfd6801283364 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 25 Sep 2020 16:39:22 +0100 Subject: util.error: Simplify error creation flow --- util/error.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 976bc355..cfb35350 100644 --- a/util/error.lua +++ b/util/error.lua @@ -35,7 +35,19 @@ end -- What to set `type` to for stream errors or SASL errors? Those don't have a 'type' attr. local function new(e, context, registry, source) - local template = (registry and registry[e]) or e or {}; + local template = registry and registry[e]; + if not template then + if type(e) == "table" then + template = { + code = e.code; + type = e.type; + condition = e.condition; + text = e.text; + }; + else + template = {}; + end + end context = context or {}; if auto_inject_traceback then -- cgit v1.2.3 From ed5841e42b9c6b2ae31ba5f9a5767e67e2fb71d4 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 26 Sep 2020 17:16:34 +0200 Subject: util.error: Add well-known field 'extra' A place for various extra fields and edge cases of the stanza error data model, e.g. the URI field of --- util/error.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index cfb35350..e0b1f9a6 100644 --- a/util/error.lua +++ b/util/error.lua @@ -43,6 +43,7 @@ local function new(e, context, registry, source) type = e.type; condition = e.condition; text = e.text; + extra = e.extra; }; else template = {}; @@ -61,6 +62,7 @@ local function new(e, context, registry, source) condition = template.condition or "undefined-condition"; text = template.text; code = template.code; + extra = template.extra; context = context; source = source; -- cgit v1.2.3 From 0354452a9a537d141e61d09b553e1ceadf97cf42 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 26 Sep 2020 18:13:27 +0200 Subject: util.error: Extract error originator from stanza errors --- util/error.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index e0b1f9a6..c2b7d149 100644 --- a/util/error.lua +++ b/util/error.lua @@ -93,12 +93,17 @@ end local function from_stanza(stanza, context) local error_type, condition, text = stanza:get_error(); + local error_tag = stanza:get_child("error"); + context = context or {}; + context.stanza = stanza; + context.by = error_tag.attr.by; return setmetatable({ type = error_type or "cancel"; condition = condition or "undefined-condition"; text = text; - context = context or { stanza = stanza }; + context = context; + }, error_mt); end -- cgit v1.2.3 From 8627f2e4d560a5fa4a2f9acf638f6535b9df8f26 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 26 Sep 2020 18:14:10 +0200 Subject: util.error: Default error originator to stanza sender The @by attribute is primarily useful for errors caused by intermediate entities. --- util/error.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index c2b7d149..0fe6cfa2 100644 --- a/util/error.lua +++ b/util/error.lua @@ -96,7 +96,8 @@ local function from_stanza(stanza, context) local error_tag = stanza:get_child("error"); context = context or {}; context.stanza = stanza; - context.by = error_tag.attr.by; + context.by = error_tag.attr.by or stanza.attr.from; + return setmetatable({ type = error_type or "cancel"; condition = condition or "undefined-condition"; -- cgit v1.2.3 From 3c7cbd0e2cbf471addcc332f2a8ba5cd635ae845 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 26 Sep 2020 18:15:27 +0200 Subject: util.error: Add special case handling of with an URI --- util/error.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 0fe6cfa2..c0b87d86 100644 --- a/util/error.lua +++ b/util/error.lua @@ -102,6 +102,9 @@ local function from_stanza(stanza, context) type = error_type or "cancel"; condition = condition or "undefined-condition"; text = text; + extra = condition == "gone" and { + uri = error_tag:get_child_text("gone", "urn:ietf:params:xml:ns:xmpp-stanzas"); + } or nil; context = context; -- cgit v1.2.3 From a51d5912465d8129ab062572d1f2e2bdddf967e6 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 26 Sep 2020 18:15:49 +0200 Subject: util.error: Collect Application-Specific Conditions from stanza errors --- util/error.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index c0b87d86..92567248 100644 --- a/util/error.lua +++ b/util/error.lua @@ -92,7 +92,7 @@ local function coerce(ok, err, ...) end local function from_stanza(stanza, context) - local error_type, condition, text = stanza:get_error(); + local error_type, condition, text, extra_tag = stanza:get_error(); local error_tag = stanza:get_child("error"); context = context or {}; context.stanza = stanza; @@ -102,8 +102,9 @@ local function from_stanza(stanza, context) type = error_type or "cancel"; condition = condition or "undefined-condition"; text = text; - extra = condition == "gone" and { + extra = (extra_tag or condition == "gone") and { uri = error_tag:get_child_text("gone", "urn:ietf:params:xml:ns:xmpp-stanzas"); + tag = extra_tag; } or nil; context = context; -- cgit v1.2.3 From 60a6ee0d30769c9066fae19b7db7dbf92ea777b7 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 27 Sep 2020 00:17:48 +0200 Subject: util.error: Pass converted stanza errors throguh new() In order to benefit from common processing --- util/error.lua | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 92567248..fd6deb80 100644 --- a/util/error.lua +++ b/util/error.lua @@ -91,14 +91,14 @@ local function coerce(ok, err, ...) return ok, new_err, ...; end -local function from_stanza(stanza, context) +local function from_stanza(stanza, context, source) local error_type, condition, text, extra_tag = stanza:get_error(); local error_tag = stanza:get_child("error"); context = context or {}; context.stanza = stanza; context.by = error_tag.attr.by or stanza.attr.from; - return setmetatable({ + return new({ type = error_type or "cancel"; condition = condition or "undefined-condition"; text = text; @@ -106,10 +106,7 @@ local function from_stanza(stanza, context) uri = error_tag:get_child_text("gone", "urn:ietf:params:xml:ns:xmpp-stanzas"); tag = extra_tag; } or nil; - - context = context; - - }, error_mt); + }, context, nil, source); end return { -- cgit v1.2.3 From 04be6c2e839894756eaf4a3aa53a823508f14b12 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 28 Sep 2020 01:55:35 +0200 Subject: util.error: Turns out wasn't alone, there's also --- util/error.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index fd6deb80..44eb59a2 100644 --- a/util/error.lua +++ b/util/error.lua @@ -98,12 +98,17 @@ local function from_stanza(stanza, context, source) context.stanza = stanza; context.by = error_tag.attr.by or stanza.attr.from; + local uri; + if condition == "gone" or condition == "redirect" then + uri = error_tag:get_child_text(condition, "urn:ietf:params:xml:ns:xmpp-stanzas"); + end + return new({ type = error_type or "cancel"; condition = condition or "undefined-condition"; text = text; - extra = (extra_tag or condition == "gone") and { - uri = error_tag:get_child_text("gone", "urn:ietf:params:xml:ns:xmpp-stanzas"); + extra = (extra_tag or uri) and { + uri = uri; tag = extra_tag; } or nil; }, context, nil, source); -- cgit v1.2.3 From d101532cfcafd983a09ba5bbc990b5fe26c75d83 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 28 Sep 2020 19:26:48 +0200 Subject: util.error: Expose source and registry as fields on the registry object For access, e.g. to identify and compare errors later --- util/error.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 44eb59a2..47d4e7b6 100644 --- a/util/error.lua +++ b/util/error.lua @@ -73,6 +73,8 @@ end local function init(source, registry) return { + source = source; + registry = registry; new = function (e, context) return new(e, context, registry, source); end; -- cgit v1.2.3 From 5da983d8bdc574f9358a16eb08d700dde3ca7026 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 28 Sep 2020 18:39:51 +0200 Subject: util.error: Add a "compact mode" for registries Inspired by the older registry in pubsub.lib.lua --- util/error.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 47d4e7b6..2e4118ec 100644 --- a/util/error.lua +++ b/util/error.lua @@ -58,11 +58,14 @@ local function new(e, context, registry, source) local error_instance = setmetatable({ instance_id = id.short(); - type = template.type or "cancel"; - condition = template.condition or "undefined-condition"; - text = template.text; + type = template.type or template[1] or "cancel"; + condition = template.condition or template[2] or "undefined-condition"; + text = template.text or template[3]; code = template.code; - extra = template.extra; + extra = template.extra or (registry and registry.namespace and template[4] and { + namespace = registry.namespace; + condition = template[4] + }); context = context; source = source; -- cgit v1.2.3 From 9dbdb91c471ea765936a15af02b62e4cbf7ba7a2 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 28 Sep 2020 22:13:04 +0200 Subject: util.error: Expand compact registries into normal form internally Also the exposed form on the table returned from init() --- util/error.lua | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 2e4118ec..5bd0f80e 100644 --- a/util/error.lua +++ b/util/error.lua @@ -58,14 +58,11 @@ local function new(e, context, registry, source) local error_instance = setmetatable({ instance_id = id.short(); - type = template.type or template[1] or "cancel"; - condition = template.condition or template[2] or "undefined-condition"; - text = template.text or template[3]; + type = template.type or "cancel"; + condition = template.condition or "undefined-condition"; + text = template.text; code = template.code; - extra = template.extra or (registry and registry.namespace and template[4] and { - namespace = registry.namespace; - condition = template[4] - }); + extra = template.extra; context = context; source = source; @@ -74,7 +71,36 @@ local function new(e, context, registry, source) return error_instance; end -local function init(source, registry) +-- compact --> normal form +local function expand_registry(namespace, registry) + local mapped = {} + for err,template in pairs(registry) do + local e = { + type = template[1]; + condition = template[2]; + text = template[3]; + }; + if namespace and template[4] then + e.extra = { namespace = namespace, condition = template[4] }; + end + mapped[err] = e; + end + return mapped; +end + +local function init(source, namespace, registry) + if type(namespace) == "table" then + -- registry can be given as second argument if namespace is either not used + registry, namespace = namespace, nil; + if type(registry.namespace) == "string" then + -- error templates are always type table, so this can't be one + namespace, registry.namespace = registry.namespace, nil; + end + end + local _, protoerr = next(registry, nil); + if protoerr and type(next(protoerr)) == "number" then + registry = expand_registry(namespace, registry); + end return { source = source; registry = registry; -- cgit v1.2.3 From 33070035bf3e92548e69418e357bec5ca55f6266 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 28 Sep 2020 23:48:02 +0200 Subject: util.error: Drop registry initialization with namespace as key Enough complexity with compact vs normal and with/without namespace --- util/error.lua | 4 ---- 1 file changed, 4 deletions(-) (limited to 'util/error.lua') diff --git a/util/error.lua b/util/error.lua index 5bd0f80e..132389f7 100644 --- a/util/error.lua +++ b/util/error.lua @@ -92,10 +92,6 @@ local function init(source, namespace, registry) if type(namespace) == "table" then -- registry can be given as second argument if namespace is either not used registry, namespace = namespace, nil; - if type(registry.namespace) == "string" then - -- error templates are always type table, so this can't be one - namespace, registry.namespace = registry.namespace, nil; - end end local _, protoerr = next(registry, nil); if protoerr and type(next(protoerr)) == "number" then -- cgit v1.2.3