aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_error_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/util_error_spec.lua')
-rw-r--r--spec/util_error_spec.lua70
1 files changed, 70 insertions, 0 deletions
diff --git a/spec/util_error_spec.lua b/spec/util_error_spec.lua
index 34edd313..be176635 100644
--- a/spec/util_error_spec.lua
+++ b/spec/util_error_spec.lua
@@ -140,6 +140,76 @@ describe("util.error", function ()
nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"};
}, compact2.registry);
end);
+
+ describe(".wrap", function ()
+ local reg = errors.init("test", "spec", {
+ myerror = { "cancel", "internal-server-error", "Oh no" };
+ });
+ it("is exposed", function ()
+ assert.is_function(reg.wrap);
+ end);
+ it("returns errors according to the registry", function ()
+ local e = reg.wrap("myerror");
+ assert.equal("cancel", e.type);
+ assert.equal("internal-server-error", e.condition);
+ assert.equal("Oh no", e.text);
+ end);
+
+ it("passes through existing errors", function ()
+ local e = reg.wrap(reg.new({ type = "auth", condition = "forbidden" }));
+ assert.equal("auth", e.type);
+ assert.equal("forbidden", e.condition);
+ end);
+
+ it("wraps arbitrary values", function ()
+ local e = reg.wrap(123);
+ assert.equal("cancel", e.type);
+ assert.equal("undefined-condition", e.condition);
+ assert.equal(123, e.context.wrapped_error);
+ end);
+ end);
+
+ describe(".coerce", function ()
+ local reg = errors.init("test", "spec", {
+ myerror = { "cancel", "internal-server-error", "Oh no" };
+ });
+
+ it("is exposed", function ()
+ assert.is_function(reg.coerce);
+ end);
+
+ it("passes through existing errors", function ()
+ local function test()
+ return nil, errors.new({ type = "auth", condition = "forbidden" });
+ end
+ local ok, err = reg.coerce(test());
+ assert.is_nil(ok);
+ assert.is_truthy(errors.is_err(err));
+ assert.equal("forbidden", err.condition);
+ end);
+
+ it("passes through successful return values", function ()
+ local function test()
+ return 1, 2, 3, 4;
+ end
+ local one, two, three, four = reg.coerce(test());
+ assert.equal(1, one);
+ assert.equal(2, two);
+ assert.equal(3, three);
+ assert.equal(4, four);
+ end);
+
+ it("wraps non-error objects", function ()
+ local function test()
+ return nil, "myerror";
+ end
+ local ok, err = reg.coerce(test());
+ assert.is_nil(ok);
+ assert.is_truthy(errors.is_err(err));
+ assert.equal("internal-server-error", err.condition);
+ assert.equal("Oh no", err.text);
+ end);
+ end);
end);
end);