aboutsummaryrefslogtreecommitdiffstats
path: root/util/error.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2020-12-09 13:55:10 +0000
committerMatthew Wild <mwild1@gmail.com>2020-12-09 13:55:10 +0000
commite875910c5db89d3f1752b878357f0a164a1936a2 (patch)
tree1b1f3de5bdfcbc5c0d94f33fa947717ca41eba97 /util/error.lua
parent3de8b69f1cea1bbfdc29dceb4ca1991d583dcc74 (diff)
downloadprosody-e875910c5db89d3f1752b878357f0a164a1936a2.tar.gz
prosody-e875910c5db89d3f1752b878357f0a164a1936a2.zip
util.error: Add coerce and wrap methods to registry(?) objects
Diffstat (limited to 'util/error.lua')
-rw-r--r--util/error.lua19
1 files changed, 19 insertions, 0 deletions
diff --git a/util/error.lua b/util/error.lua
index ed3ef4f4..173d4b7d 100644
--- a/util/error.lua
+++ b/util/error.lua
@@ -98,12 +98,31 @@ local function init(source, namespace, registry)
if protoerr and type(next(protoerr)) == "number" then
registry = expand_registry(namespace, registry);
end
+
+ local function wrap(e, context)
+ if is_err(e) then
+ return e;
+ end
+ local err = new(registry[e] or {
+ type = "cancel", condition = "undefined-condition"
+ }, context, registry, source);
+ err.context.wrapped_error = e;
+ return err;
+ end
+
return {
source = source;
registry = registry;
new = function (e, context)
return new(e, context, registry, source);
end;
+ coerce = function (ok, err, ...)
+ if ok then
+ return ok, err, ...;
+ end
+ return nil, wrap(err);
+ end;
+ wrap = wrap;
};
end