Skip to content

Commit

Permalink
Update docs for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyaksenov committed Aug 16, 2023
1 parent 0684ec7 commit 161c7d1
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 4 deletions.
45 changes: 45 additions & 0 deletions doc/code_snippets/test/errors/error_list_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local status, err = pcall(function()
local base_server_error = box.error.new({ code = 500,
reason = 'Base server error',
type = 'BaseServerError' })
local storage_server_error = box.error.new({ code = 507,
reason = 'Not enough storage',
type = 'StorageServerError' })

base_server_error:set_prev(storage_server_error)
--[[
---
...
--]]

box.error(base_server_error)
--[[
---
- error: Base server error
...
--]]

box.error.last().prev:unpack()
--[[
---
- code: 507
base_type: CustomError
type: StorageServerError
custom_type: StorageServerError
message: Not enough storage
trace:
- file: '[string "storage_server_error = box.error.new({ code =..."]'
line: 1
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_error_is_raised = function()
luatest.assert_equals(err:unpack().type, 'BaseServerError')
luatest.assert_equals(err:unpack().message, 'Base server error')
luatest.assert_equals(err.prev:unpack().type, 'StorageServerError')
luatest.assert_equals(err.prev:unpack().message, 'Not enough storage')
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local status, err = pcall(function()
box.error('CustomConnectionError', '%s cannot connect to the port %u', 'client', 8080)
--[[
---
- error: client cannot connect to the port 8080
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_error_is_raised = function()
luatest.assert_equals(err:unpack().type, 'CustomConnectionError')
luatest.assert_equals(err:unpack().message, 'client cannot connect to the port 8080')
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local status, err = pcall(function()
box.error('CustomConnectionError', 'cannot connect to the given port')
--[[
---
- error: cannot connect to the given port
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_error_is_raised = function()
luatest.assert_equals(err:unpack().type, 'CustomConnectionError')
luatest.assert_equals(err:unpack().message, 'cannot connect to the given port')
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local status, err = pcall(function()
box.error { code = 500,
reason = 'Internal server error',
type = 'CustomInternalError' }
--[[
---
- error: Internal server error
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_error_is_raised = function()
luatest.assert_equals(err:unpack().type, 'CustomInternalError')
luatest.assert_equals(err:unpack().message, 'Internal server error')
end
15 changes: 15 additions & 0 deletions doc/code_snippets/test/errors/raise_error_table_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local status, err = pcall(function()
box.error { code = 500, reason = 'Custom server error' }
--[[
---
- error: Custom server error
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_error_is_raised = function()
luatest.assert_equals(err:unpack().message, 'Custom server error')
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local status, err = pcall(function()
local custom_error = box.error.new('CustomInternalError', 'Internal server error')

box.error(custom_error)
--[[
---
- error: Internal server error
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_error_is_raised = function()
luatest.assert_equals(err:unpack().type, 'CustomInternalError')
luatest.assert_equals(err:unpack().message, 'Internal server error')
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local status, err = pcall(function()
local custom_error = box.error.new({ code = 500,
reason = 'Internal server error',
type = 'CustomInternalError' })

box.error(custom_error)
--[[
---
- error: Internal server error
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_error_is_raised = function()
luatest.assert_equals(err:unpack().type, 'CustomInternalError')
luatest.assert_equals(err:unpack().message, 'Internal server error')
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local status, err = pcall(function()
local custom_error = box.error.new(box.error.CREATE_SPACE, 'my_space', 'the space already exists')

box.error(custom_error)
--[[
---
- error: 'Failed to create space ''my_space'': the space already exists'
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_tarantool_error_is_raised = function()
luatest.assert_equals(err:unpack().message, "Failed to create space 'my_space': the space already exists")
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local status, err = pcall(function()
local custom_error = box.error.new(box.error.NO_SUCH_USER, 'John')

box.error(custom_error)
--[[
---
- error: User 'John' is not found
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_tarantool_error_is_raised = function()
luatest.assert_equals(err:unpack().message, "User 'John' is not found")
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local status, err = pcall(function()
box.error(box.error.CREATE_SPACE, 'my_space', 'the space already exists')
--[[
---
- error: 'Failed to create space ''my_space'': the space already exists'
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_tarantool_error_is_raised = function()
luatest.assert_equals(err:unpack().message, "Failed to create space 'my_space': the space already exists")
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local status, err = pcall(function()
box.error(box.error.NO_SUCH_USER, 'John')
--[[
---
- error: User 'John' is not found
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_tarantool_error_is_raised = function()
luatest.assert_equals(err:unpack().message, "User 'John' is not found")
end
44 changes: 44 additions & 0 deletions doc/code_snippets/test/errors/set_error_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local status, err = pcall(function()
-- Create two errors --
local error1 = box.error.new({ code = 500, reason = 'Custom error 1' })
local error2 = box.error.new({ code = 505, reason = 'Custom error 2' })

-- Raise the first error --
box.error(error1)
--[[
---
- error: Custom error 1
...
--]]

-- Get the last error --
box.error.last()
--[[
---
- Custom error 1
...
--]]

-- Set the second error as the last error --
box.error.set(error2)
--[[
---
...
--]]

-- Get the last error --
box.error.last()
--[[
---
- Custom error 2
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_error_is_raised = function()
luatest.assert_equals(status, false)
luatest.assert_equals(err:unpack().message, 'Custom error 1')
end
50 changes: 50 additions & 0 deletions doc/code_snippets/test/errors/unpack_clear_error_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
local status, err = pcall(function()
-- Create an error --
local custom_error = box.error.new({ code = 500,
reason = 'Internal server error',
type = 'CustomInternalError' })

-- Raise the error --
box.error(custom_error)
--[[
---
- error: Internal server error
...
--]]

-- Get error details --
box.error.last():unpack()
--[[
---
- code: 500
base_type: CustomError
type: CustomInternalError
custom_type: CustomInternalError
message: Internal server error
trace:
- file: '[string "custom_error = box.error.new({ code = 500, re..."]'
line: 1
...
--]]

-- Clear the errors --
box.error.clear()
--[[
---
...
--]]
box.error.last()
--[[
---
- null
...
--]]
end)

-- Tests
local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_error_is_raised = function()
luatest.assert_equals(err:unpack().type, 'CustomInternalError')
luatest.assert_equals(err:unpack().message, 'Internal server error')
end
2 changes: 1 addition & 1 deletion doc/reference/reference_lua/box_error/error.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ box.error()

Throw an error. This method emulates a request error, with text based on one
of the pre-defined Tarantool errors defined in the file `errcode.h
<https://github.com/tarantool/tarantool/blob/2.1/src/box/errcode.h>`_ in
<https://github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`_ in
the source tree. Lua constants which correspond to those Tarantool errors are
defined as members of ``box.error``, for example ``box.error.NO_SUCH_USER == 45``.

Expand Down
2 changes: 1 addition & 1 deletion doc/reference/reference_lua/errcodes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ more descriptive than error codes, are not present in server responses. The actu
message may contain a filename, a detailed reason or operating system error code.
All such messages, however, are logged in the error log. Below are general
descriptions of some popular codes. A complete list of errors can be found in file
`errcode.h <https://github.com/tarantool/tarantool/blob/master/src/box/error.h>`_ in the source tree.
`errcode.h <https://github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`_ in the source tree.

.. container:: table

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ msgstr "(целое число) числовой код ошибки, задае
msgid ""
"Throw an error. This method emulates a request error, with text based on one"
" of the pre-defined Tarantool errors defined in the file `errcode.h "
"<https://github.com/tarantool/tarantool/blob/2.1/src/box/errcode.h>`_ in the"
"<https://github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`_ in the"
" source tree. Lua constants which correspond to those Tarantool errors are "
"defined as members of ``box.error``, for example ``box.error.NO_SUCH_USER =="
" 45``."
msgstr ""
"Выдача ошибки. Моделирование ошибки запроса с текстом на основе одной из "
"ошибок Tarantool, заданных в файле `errcode.h "
"<https://github.com/tarantool/tarantool/blob/2.1/src/box/errcode.h>`_ в "
"<https://github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`_ в "
"исходном дереве. Lua-постоянные, которые соответствуют этим ошибкам в "
"Tarantool, определяются как элементы ``box.error``, например "
"``box.error.NO_SUCH_USER == 45``."
Expand Down

0 comments on commit 161c7d1

Please sign in to comment.