diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-12-30 12:55:58 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-12-30 12:55:58 +0000 |
commit | 38462e35420ca9e93af018210be940353b32d508 (patch) | |
tree | d7b050c876cd829fd602928010a3e8db8b9d4b78 /util | |
parent | 93c5ebb743337c82103ea94166065106a9f5b641 (diff) | |
download | prosody-38462e35420ca9e93af018210be940353b32d508.tar.gz prosody-38462e35420ca9e93af018210be940353b32d508.zip |
util.error: Add new util library for structured errors
Diffstat (limited to 'util')
-rw-r--r-- | util/error.lua | 40 |
1 files changed, 40 insertions, 0 deletions
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; +} |