From e875910c5db89d3f1752b878357f0a164a1936a2 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 9 Dec 2020 13:55:10 +0000 Subject: util.error: Add coerce and wrap methods to registry(?) objects --- util/error.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'util') 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 -- cgit v1.2.3