author | Kim Alvefur <zash@zash.se> |
Mon, 28 Sep 2020 18:36:00 +0200 | |
changeset 11101 | f23cf8e2e2ff |
parent 11096 | bd13aa89262d |
child 11104 | 3aa06cdd2dc8 |
permissions | -rw-r--r-- |
10105 | 1 |
local errors = require "util.error" |
2 |
||
3 |
describe("util.error", function () |
|
4 |
describe("new()", function () |
|
5 |
it("works", function () |
|
6 |
local err = errors.new("bork", "bork bork"); |
|
7 |
assert.not_nil(err); |
|
8 |
assert.equal("cancel", err.type); |
|
9 |
assert.equal("undefined-condition", err.condition); |
|
10 |
assert.same("bork bork", err.context); |
|
11 |
end); |
|
12 |
||
13 |
describe("templates", function () |
|
14 |
it("works", function () |
|
15 |
local templates = { |
|
16 |
["fail"] = { |
|
17 |
type = "wait", |
|
18 |
condition = "internal-server-error", |
|
10369
744ca71a49f7
util.error: Add well-known field 'code' in error templates
Kim Alvefur <zash@zash.se>
parents:
10105
diff
changeset
|
19 |
code = 555; |
10105 | 20 |
}; |
21 |
}; |
|
22 |
local err = errors.new("fail", { traceback = "in some file, somewhere" }, templates); |
|
23 |
assert.equal("wait", err.type); |
|
24 |
assert.equal("internal-server-error", err.condition); |
|
10369
744ca71a49f7
util.error: Add well-known field 'code' in error templates
Kim Alvefur <zash@zash.se>
parents:
10105
diff
changeset
|
25 |
assert.equal(555, err.code); |
10105 | 26 |
assert.same({ traceback = "in some file, somewhere" }, err.context); |
27 |
end); |
|
28 |
end); |
|
29 |
||
30 |
end); |
|
31 |
||
32 |
describe("is_err()", function () |
|
33 |
it("works", function () |
|
34 |
assert.truthy(errors.is_err(errors.new())); |
|
35 |
assert.falsy(errors.is_err("not an error")); |
|
36 |
end); |
|
37 |
end); |
|
38 |
||
39 |
describe("coerce", function () |
|
40 |
it("works", function () |
|
41 |
local ok, err = errors.coerce(nil, "it dun goofed"); |
|
42 |
assert.is_nil(ok); |
|
43 |
assert.truthy(errors.is_err(err)) |
|
44 |
end); |
|
45 |
end); |
|
46 |
||
47 |
describe("from_stanza", function () |
|
48 |
it("works", function () |
|
49 |
local st = require "util.stanza"; |
|
50 |
local m = st.message({ type = "chat" }); |
|
11096
bd13aa89262d
util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11093
diff
changeset
|
51 |
local e = st.error_reply(m, "modify", "bad-request", nil, "error.example"):tag("extra", { xmlns = "xmpp:example.test" }); |
10105 | 52 |
local err = errors.from_stanza(e); |
53 |
assert.truthy(errors.is_err(err)); |
|
54 |
assert.equal("modify", err.type); |
|
55 |
assert.equal("bad-request", err.condition); |
|
56 |
assert.equal(e, err.context.stanza); |
|
11093
35d2260644d9
util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11085
diff
changeset
|
57 |
assert.equal("error.example", err.context.by); |
11096
bd13aa89262d
util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11093
diff
changeset
|
58 |
assert.not_nil(err.extra.tag); |
10105 | 59 |
end); |
60 |
end); |
|
61 |
||
62 |
describe("__tostring", function () |
|
63 |
it("doesn't throw", function () |
|
64 |
assert.has_no.errors(function () |
|
65 |
-- See 6f317e51544d |
|
66 |
tostring(errors.new()); |
|
67 |
end); |
|
68 |
end); |
|
69 |
end); |
|
70 |
||
11085
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
71 |
describe("extra", function () |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
72 |
it("keeps some extra fields", function () |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10369
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:
10369
diff
changeset
|
74 |
assert.is_table(err.extra); |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10369
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:
10369
diff
changeset
|
76 |
end); |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
77 |
end) |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10369
diff
changeset
|
78 |
|
11101
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
79 |
describe("init", function() |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
80 |
it("basics works", function() |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
81 |
local reg = errors.init("test", { |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
82 |
broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("}; |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
83 |
nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"}; |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
84 |
}); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
85 |
|
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
86 |
local broke = reg.new("broke"); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
87 |
assert.equal("cancel", broke.type); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
88 |
assert.equal("internal-server-error", broke.condition); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
89 |
assert.equal("It broke :(", broke.text); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
90 |
assert.equal("test", broke.source); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
91 |
|
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
92 |
local nope = reg.new("nope"); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
93 |
assert.equal("auth", nope.type); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
94 |
assert.equal("not-authorized", nope.condition); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
95 |
assert.equal("Can't let you do that Dave", nope.text); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
96 |
end); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
97 |
end); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11096
diff
changeset
|
98 |
|
10105 | 99 |
end); |
100 |