Skip to content

Commit

Permalink
console: handle internal.format_yaml exceptions
Browse files Browse the repository at this point in the history
Currently the call to internal.format_yaml in output_handlers["yaml"]
is covered by pcall only for `status == true`, however the opposite
case also can raise an exception. This patch adds the missed exception
handling. Two distinct calls are required, because it is not possible to
assing variadic arguments (...) to a variable if some of them is nil.
If the first call fails, internal.format_yaml will be called for the
second time. Hopefully, it will not fail during formatting of the error
message received from libyaml.

Before:
```
tarantool> require('net.box').self:call('\x80')
LuajitError: builtin/box/console.lua:710: expected SCALAR, SEQUENCE-START,
    MAPPING-START, or ALIAS fatal error, exiting the event loop
~/test$ echo $?
0
~/test$
```

After:
```
tarantool> require('net.box').self:call('\x80')
---
- error: 'console: an exception occurred when formatting the output:
    expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS'
...

tarantool>
```

Part of tarantool#6781
Part of tarantool#6934

NO_DOC=bugfix
NO_TEST=see later commits
NO_CHANGELOG=see later commits
  • Loading branch information
Gumix authored and mkokryashkin committed Sep 9, 2022
1 parent 87a2248 commit e5407da
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/box/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,24 @@ local output_handlers = { }
local output_eos = { ["yaml"] = '\n...\n', ["lua"] = ';' }

output_handlers["yaml"] = function(status, opts, ...)
local err
local err, ok, res
-- Using pcall, because serializer can raise an exception
if status then
-- serializer can raise an exception
status, err = pcall(internal.format_yaml, ...)
if status then
return err
else
err = 'console: an exception occurred when formatting the output: '..
tostring(err)
end
ok, res = pcall(internal.format_yaml, ...)
else
err = ...
if err == nil then
err = box.NULL
end
ok, res = pcall(internal.format_yaml, { error = err })
end
if ok then
return res
else
local m = 'console: exception while formatting the output: "%s"'
err = m:format(tostring(res))
return internal.format_yaml({ error = err })
end
return internal.format_yaml({ error = err })
end

--
Expand Down

0 comments on commit e5407da

Please sign in to comment.