tarantool / tarantool/doc

Document `level` argument of `box.error` and `box.error.new`

Open
#4,107 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.1 errors reference
Dominant language
CSS
Stars
15
Forks
49
Avg merge
1d 13h
Merged PRs (30d)
3

Description

Related dev. issue(s): https://github.com/tarantool/tarantool/issues/9792

Product: Tarantool
Since: 3.1
Root document:

SME: @ locker

Details

Now, when used with a table argument, box.error and box.error.new
also accept an optional second argument called level. It has the same
meaning as the level argument of the built-in Lua function error:
it specifies how to get the error location. With level 1 (the default),
the error location is where box.error / box.error.new was called.
Level 2 points the error to where the function that called box.error /
box.error.new was called; and so on. Passing level 0 avoids addition
of location information to the error.

Example of using level with box.error:

local json = require('json')

local function inner(level)
    box.error({message = 'test'}, level)    -- line:4
end

local function outer(level)
    inner(level)                            -- line:8
end

local ok, err
ok, err = pcall(outer)
print(json.encode(err.trace))               -- prints line:4
ok, err = pcall(outer, 1)
print(json.encode(err.trace))               -- prints line:4
ok, err = pcall(outer, 2)
print(json.encode(err.trace))               -- prints line:8
ok, err = pcall(outer, 0)
print(json.encode(err.trace))               -- prints empty table

Example of using level with box.error.new:

local json = require('json')

local function inner(level)
    local err = box.error.new({message = 'test'}, level)    -- line:4
    return err
end

local function outer(level)
    local err = inner(level)                                -- line:9
    return err
end

local err
err = outer()
print(json.encode(err.trace))               -- prints line:4
err = outer(1)
print(json.encode(err.trace))               -- prints line:4
err = outer(2)
print(json.encode(err.trace))               -- prints line:9
ok, err = pcall(outer, 0)
print(json.encode(err.trace))               -- prints empty table

It is also possible to specify level when using box.error to
re-raise an error created earlier with box.error.new, for example:

local json = require('json')

local err0 = box.error.new{message = 'test'}    -- line:3

local function raise(err, level)
    box.error(err, level)                       -- line:6
end

ok, err = pcall(raise, err0)
print(json.encode(err.trace))               -- prints line:3

ok, err = pcall(raise, err0, 1)
print(json.encode(err.trace))               -- prints line:6

ok, err = pcall(raise, err0, 0)
print(json.encode(err.trace))               -- prints empty table

Requested by @ locker in https://github.com/tarantool/tarantool/commit/25cdabe40dc09485d9184a80d1eb972648339ed8.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the two root documents for box.error and box.error.new listed in the issue. Document the optional level argument, its defaults and location behavior, and incorporate the supplied examples as appropriate. Done means both references explain level 0, level 1, higher levels, and re-raising an existing error.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.