Diff

util/error.lua @ 11221:b0a563716334

util.error: Add coerce and wrap methods to registry(?) objects
author Matthew Wild <mwild1@gmail.com>
date Wed, 09 Dec 2020 13:55:10 +0000
parent 11207:4e060ae8520b
child 11222:4b39691a274e
line wrap: on
line diff
--- a/util/error.lua	Wed Dec 09 13:54:21 2020 +0000
+++ b/util/error.lua	Wed Dec 09 13:55:10 2020 +0000
@@ -98,12 +98,31 @@
 	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