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
commitdbdf490e62e1651fdd73b706c30c8d9ab3cfe7b0 (patch)
tree1b1f3de5bdfcbc5c0d94f33fa947717ca41eba97 /util/error.lua
parent0829bb1a04b87483db4be6695f2f97fe13899c12 (diff)
downloadprosody-dbdf490e62e1651fdd73b706c30c8d9ab3cfe7b0.tar.gz
prosody-dbdf490e62e1651fdd73b706c30c8d9ab3cfe7b0.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