File: error.test.lua

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (277 lines) | stat: -rw-r--r-- 6,655 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
env = require('test_run')
test_run = env.new()

space = box.schema.space.create('tweedledum')
index = space:create_index('primary', { type = 'hash' })

box.error({code = 123, reason = 'test'})
box.error(box.error.ILLEGAL_PARAMS, "bla bla")
box.error()
e = box.error.last()
e
e:unpack()
e.type
e.code
e.message
tostring(e)
e = nil
box.error.clear()
box.error.last()
space = box.space.tweedledum

--
-- gh-2080: box.error() crashes with wrong parameters
box.error(box.error.UNSUPPORTED, "x", "x%s")
box.error(box.error.UNSUPPORTED, "x")
box.error(box.error.UNSUPPORTED)

--
-- gh-3031: allow to create an error object with no throwing it.
--
e = box.error.new(box.error.UNKNOWN)
e
e = box.error.new(box.error.CREATE_SPACE, "space", "error")
e
box.error.new()

--
-- gh-4489: box.error has __concat metamethod
--
test_run:cmd("push filter '(.builtin/.*.lua):[0-9]+' to '\\1'")
e = box.error.new(box.error.UNKNOWN)
'left side: ' .. e
e .. ': right side'
e .. nil
nil .. e
e .. box.NULL
box.NULL .. e
123 .. e
e .. 123
e .. e
e .. {}
{} .. e
-1ULL .. e
e .. -1ULL
1LL .. e
e .. 1LL
e = nil

--
-- System errors expose errno as a field.
--
_, err = require('fio').open('not_existing_file')
type(err.errno)
-- Errors not related to the standard library do
-- not expose errno.
err = box.error.new(box.error.PROC_LUA, "errno")
type(err.errno)

t = {}
test_run:cmd("setopt delimiter ';'")

for k,v in pairs(box.error) do
   if type(v) == 'number' then
    t[v] = 'box.error.'..tostring(k)
   end
end;
t;

test_run:cmd("setopt delimiter ''");

-- gh-4778: don't add created via box.error.new() errors to
-- Tarantool's diagnostic area.
--
err = box.error.new({code = 111, reason = "cause"})
assert(box.error.last() ~= err)
box.error.set(err)
assert(box.error.last() == err)
-- Consider wrong or tricky inputs to box.error.set()
--
box.error.set(1)
box.error.set(nil)
box.error.set(box.error.last())
assert(box.error.last() == err)
-- Check that box.error.new() does not set error to diag.
--
box.error.clear()
err = box.error.new(1, "cause")
assert(box.error.last() == nil)

-- box.error.new() does not accept error objects.
--
box.error.new(err)

-- box.error() is supposed to re-throw last diagnostic error.
-- Make sure it does not fail if there's no errors at all
-- (in diagnostics area).
--
box.error.clear()
box.error()

space:drop()

-- gh-1148: errors can be arranged into list (so called
-- stacked diagnostics).
--
e1 = box.error.new({code = 111, reason = "cause"})
assert(e1.prev == nil)
e1:set_prev(e1)
assert(e1.prev == nil)
e2 = box.error.new({code = 111, reason = "cause of cause"})
e1:set_prev(e2)
assert(e1.prev == e2)
e2:set_prev(e1)
assert(e2.prev == nil)
-- At this point stack is following: e1 -> e2
-- Let's test following cases:
-- 1. e3 -> e2, e1 -> NULL (e3:set_prev(e2))
-- 2. e1 -> e3, e2 -> NULL (e1:set_prev(e3))
-- 3. e3 -> e1 -> e2 (e3:set_prev(e1))
-- 4. e1 -> e2 -> e3 (e2:set_prev(e3))
--
e3 = box.error.new({code = 111, reason = "another cause"})
e3:set_prev(e2)
assert(e3.prev == e2)
assert(e2.prev == nil)
assert(e1.prev == nil)

-- Reset stack to e1 -> e2 and test case 2.
--
e1:set_prev(e2)
assert(e2.prev == nil)
assert(e3.prev == nil)
e1:set_prev(e3)
assert(e2.prev == nil)
assert(e1.prev == e3)
assert(e3.prev == nil)

-- Reset stack to e1 -> e2 and test case 3.
--
e1:set_prev(e2)
assert(e1.prev == e2)
assert(e2.prev == nil)
assert(e3.prev == nil)
e3:set_prev(e1)
assert(e1.prev == e2)
assert(e2.prev == nil)
assert(e3.prev == e1)

-- Unlink errors and test case 4.
--
e1:set_prev(nil)
e2:set_prev(nil)
e3:set_prev(nil)
e1:set_prev(e2)
e2:set_prev(e3)
assert(e1.prev == e2)
assert(e2.prev == e3)
assert(e3.prev == nil)

-- Test circle detecting. At the moment stack is
-- following: e1 -> e2 -> e3
--
e3:set_prev(e1)
assert(e3.prev == nil)
e3:set_prev(e2)
assert(e3.prev == nil)

-- Test splitting list into two ones.
-- After that we will get two lists: e1->e2->e5 and e3->e4
--
e4 = box.error.new({code = 111, reason = "yet another cause"})
e5 = box.error.new({code = 111, reason = "and another one"})
e3:set_prev(e4)
e2:set_prev(e5)
assert(e1.prev == e2)
assert(e2.prev == e5)
assert(e3.prev == e4)
assert(e5.prev == nil)
assert(e4.prev == nil)

-- Another splitting option: e1->e2 and e5->e3->e4
-- But firstly restore to one single list e1->e2->e3->e4
--
e2:set_prev(e3)
e5:set_prev(e3)
assert(e1.prev == e2)
assert(e2.prev == nil)
assert(e5.prev == e3)
assert(e3.prev == e4)
assert(e4.prev == nil)

-- In case error is destroyed, it unrefs reference counter
-- of its previous error. In turn, box.error.clear() refs/unrefs
-- only head and doesn't touch other errors.
--
e2:set_prev(nil)
box.error.set(e1)
assert(box.error.last() == e1)
assert(box.error.last().prev == e2)
box.error.clear()
assert(box.error.last() == nil)
assert(e1.prev == e2)
assert(e2.code == 111)
box.error.set(e1)
box.error.clear()
assert(e1.prev == e2)

-- Set middle of an error stack into the diagnostics area.
e1:set_prev(e2)
e2:set_prev(e3)
box.error.set(e2)
assert(e1.prev == nil)
assert(e2.prev == e3)

-- gh-4829: always promote error created via box.error() to
-- diagnostic area.
e1 = box.error.new({code = 111, reason = "cause"})
box.error({code = 111, reason = "err"})
box.error.last()
box.error(e1)
assert(box.error.last() == e1)
--
-- gh-4398: custom error type.
--
-- Try no code.
e = box.error.new({type = 'TestType', reason = 'Test reason'})
e:unpack()
-- Try code not the same as used by default.
e = box.error.new({type = 'TestType', reason = 'Test reason', code = 123})
e:unpack()
-- Try to omit message.
e = box.error.new({type = 'TestType'})
e:unpack()
-- Try too long type name.
e = box.error.new({type = string.rep('a', 128)})
#e.type

-- gh-4887: accessing 'prev' member also refs it so that after
-- error is gone, its 'prev' is staying alive.
--
lua_code = [[function(tuple) local json = require('json') return json.encode(tuple) end]]
box.schema.func.create('runtimeerror', {body = lua_code, is_deterministic = true, is_sandboxed = true})
s = box.schema.space.create('withdata')
pk = s:create_index('pk')
idx = s:create_index('idx', {func = box.func.runtimeerror.id, parts = {{1, 'string'}}})

function test_func() return pcall(s.insert, s, {1}) end
ok, err = test_func()
preve = err.prev
gc_err = setmetatable({preve}, {__mode = 'v'})
err:set_prev(nil)
err.prev
collectgarbage('collect')
--  Still one reference to err.prev so it should not be collected.
--
gc_err
preve = nil
collectgarbage('collect')
gc_err

s:drop()
box.schema.func.drop('runtimeerror')

-- gh-4903: add format string usage for a CustomError message
--
err = box.error.new('TestType', 'Message arg1: %s. Message arg2: %u', '1', 2)
err.message