Annotate

spec/util_error_spec.lua @ 11096:dd1713862c20

net.http.server: Default to HTTP result code 500 when promise is rejected
author Matthew Wild <mwild1@gmail.com>
date Mon, 28 Sep 2020 16:21:41 +0100
parent 11092:bd13aa89262d
child 11097:f23cf8e2e2ff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10101
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local errors = require "util.error"
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 describe("util.error", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 describe("new()", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local err = errors.new("bork", "bork bork");
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 assert.not_nil(err);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 assert.equal("cancel", err.type);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 assert.equal("undefined-condition", err.condition);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 assert.same("bork bork", err.context);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 describe("templates", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local templates = {
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 ["fail"] = {
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 type = "wait",
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 condition = "internal-server-error",
10365
744ca71a49f7 util.error: Add well-known field 'code' in error templates
Kim Alvefur <zash@zash.se>
parents: 10101
diff changeset
19 code = 555;
10101
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 };
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 };
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local err = errors.new("fail", { traceback = "in some file, somewhere" }, templates);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 assert.equal("wait", err.type);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 assert.equal("internal-server-error", err.condition);
10365
744ca71a49f7 util.error: Add well-known field 'code' in error templates
Kim Alvefur <zash@zash.se>
parents: 10101
diff changeset
25 assert.equal(555, err.code);
10101
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 assert.same({ traceback = "in some file, somewhere" }, err.context);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 describe("is_err()", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 assert.truthy(errors.is_err(errors.new()));
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 assert.falsy(errors.is_err("not an error"));
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 describe("coerce", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local ok, err = errors.coerce(nil, "it dun goofed");
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 assert.is_nil(ok);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 assert.truthy(errors.is_err(err))
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 describe("from_stanza", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 it("works", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local st = require "util.stanza";
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 local m = st.message({ type = "chat" });
11092
bd13aa89262d util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11089
diff changeset
51 local e = st.error_reply(m, "modify", "bad-request", nil, "error.example"):tag("extra", { xmlns = "xmpp:example.test" });
10101
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local err = errors.from_stanza(e);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 assert.truthy(errors.is_err(err));
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 assert.equal("modify", err.type);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 assert.equal("bad-request", err.condition);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 assert.equal(e, err.context.stanza);
11089
35d2260644d9 util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11081
diff changeset
57 assert.equal("error.example", err.context.by);
11092
bd13aa89262d util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11089
diff changeset
58 assert.not_nil(err.extra.tag);
10101
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 describe("__tostring", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 it("doesn't throw", function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 assert.has_no.errors(function ()
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 -- See 6f317e51544d
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 tostring(errors.new());
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
11081
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10365
diff changeset
71 describe("extra", function ()
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10365
diff changeset
72 it("keeps some extra fields", function ()
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10365
diff changeset
73 local err = errors.new({condition="gone",text="Sorry mate, it's all gone",extra={uri="file:///dev/null"}});
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10365
diff changeset
74 assert.is_table(err.extra);
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10365
diff changeset
75 assert.equal("file:///dev/null", err.extra.uri);
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10365
diff changeset
76 end);
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10365
diff changeset
77 end)
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 10365
diff changeset
78
10101
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end);
ba7636860bbc util.error: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80