Comparison

spec/util_error_spec.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 11102:5a0ff475ecfd
child 13080:031382b207ec
comparison
equal deleted inserted replaced
11220:9b25eecde9e6 11221:b0a563716334
138 assert.same({ 138 assert.same({
139 broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("}; 139 broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("};
140 nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"}; 140 nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"};
141 }, compact2.registry); 141 }, compact2.registry);
142 end); 142 end);
143
144 describe(".wrap", function ()
145 local reg = errors.init("test", "spec", {
146 myerror = { "cancel", "internal-server-error", "Oh no" };
147 });
148 it("is exposed", function ()
149 assert.is_function(reg.wrap);
150 end);
151 it("returns errors according to the registry", function ()
152 local e = reg.wrap("myerror");
153 assert.equal("cancel", e.type);
154 assert.equal("internal-server-error", e.condition);
155 assert.equal("Oh no", e.text);
156 end);
157
158 it("passes through existing errors", function ()
159 local e = reg.wrap(reg.new({ type = "auth", condition = "forbidden" }));
160 assert.equal("auth", e.type);
161 assert.equal("forbidden", e.condition);
162 end);
163
164 it("wraps arbitrary values", function ()
165 local e = reg.wrap(123);
166 assert.equal("cancel", e.type);
167 assert.equal("undefined-condition", e.condition);
168 assert.equal(123, e.context.wrapped_error);
169 end);
170 end);
171
172 describe(".coerce", function ()
173 local reg = errors.init("test", "spec", {
174 myerror = { "cancel", "internal-server-error", "Oh no" };
175 });
176
177 it("is exposed", function ()
178 assert.is_function(reg.coerce);
179 end);
180
181 it("passes through existing errors", function ()
182 local function test()
183 return nil, errors.new({ type = "auth", condition = "forbidden" });
184 end
185 local ok, err = reg.coerce(test());
186 assert.is_nil(ok);
187 assert.is_truthy(errors.is_err(err));
188 assert.equal("forbidden", err.condition);
189 end);
190
191 it("passes through successful return values", function ()
192 local function test()
193 return 1, 2, 3, 4;
194 end
195 local one, two, three, four = reg.coerce(test());
196 assert.equal(1, one);
197 assert.equal(2, two);
198 assert.equal(3, three);
199 assert.equal(4, four);
200 end);
201
202 it("wraps non-error objects", function ()
203 local function test()
204 return nil, "myerror";
205 end
206 local ok, err = reg.coerce(test());
207 assert.is_nil(ok);
208 assert.is_truthy(errors.is_err(err));
209 assert.equal("internal-server-error", err.condition);
210 assert.equal("Oh no", err.text);
211 end);
212 end);
143 end); 213 end);
144 214
145 end); 215 end);
146 216