error: add custom error type
Co-authored-by:
Vladislav <Shpilevoy<v.shpilevoy@tarantool.org>
Part of #4398
@TarantoolBot document
Title: Custom error types for Lua errors
Errors can be created in 2 ways: `box.error.new()` and `box.error()`.
Both used to take either `code, reason, <reason string args>` or
`{code = code, reason = reason, ...}` arguments.
Now in the first option instead of code a user can specify a
string as its own error type. In the second option a user can
specify both code and type. For example:
```Lua
box.error('MyErrorType', 'Message')
box.error({type = 'MyErrorType', code = 1024, reason = 'Message'})
```
Or no-throw version:
```Lua
box.error.new('MyErrorType', 'Message')
box.error.new({type = 'MyErrorType', code = 1024, reason = 'Message'})
```
When a custom type is specified, it is shown in `err.type`
attribute. When it is not specified, `err.type` shows one of
built-in errors such as 'ClientError', 'OurOfMemory', etc.
Name length limit on the custom type is 63 bytes. All what is
longer is truncated.
Original error type can be checked using `err.base_type` member,
although normally it should not be used. For user-defined types
base type is 'CustomError'.
For example:
```
tarantool> e = box.error.new({type = 'MyErrorType', code = 1024, reason = 'Message'})
---
...
tarantool> e:unpack()
---
- code: 1024
trace:
- file: '[string "e = box.error.new({type = ''MyErrorType'', code..."]'
line: 1
type: MyErrorType
custom_type: MyErrorType
message: Message
base_type: CustomError
...
```
Showing
- extra/exports 1 addition, 0 deletionsextra/exports
- src/box/error.cc 81 additions, 20 deletionssrc/box/error.cc
- src/box/error.h 37 additions, 3 deletionssrc/box/error.h
- src/box/lua/error.cc 30 additions, 12 deletionssrc/box/lua/error.cc
- src/box/xrow.c 1 addition, 1 deletionsrc/box/xrow.c
- src/lua/error.lua 13 additions, 1 deletionsrc/lua/error.lua
- test/app/fiber.result 3 additions, 2 deletionstest/app/fiber.result
- test/box/error.result 61 additions, 4 deletionstest/box/error.result
- test/box/error.test.lua 15 additions, 0 deletionstest/box/error.test.lua
- test/engine/func_index.result 8 additions, 6 deletionstest/engine/func_index.result
Loading
Please register or sign in to comment