|
| 1 | +""" |
| 2 | +Additional Tarantool type definitions. |
| 3 | +""" |
| 4 | + |
| 5 | +import typing |
| 6 | +from dataclasses import dataclass |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class BoxErrorStackUnit(): |
| 10 | + """ |
| 11 | + Type representing Tarantool `box.error`_ single MP_ERROR_STACK |
| 12 | + object. |
| 13 | +
|
| 14 | + .. _box.error: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_error/error/ |
| 15 | + """ |
| 16 | + |
| 17 | + type: str |
| 18 | + """ |
| 19 | + Type that implies source, for example ``"ClientError"``. |
| 20 | + """ |
| 21 | + |
| 22 | + file: str |
| 23 | + """ |
| 24 | + Source code file where error was caught. |
| 25 | + """ |
| 26 | + |
| 27 | + line: int |
| 28 | + """ |
| 29 | + Line number in source code file. |
| 30 | + """ |
| 31 | + |
| 32 | + message: str |
| 33 | + """ |
| 34 | + Text of reason. |
| 35 | + """ |
| 36 | + |
| 37 | + errno: int |
| 38 | + """ |
| 39 | + Ordinal number of the error. |
| 40 | + """ |
| 41 | + |
| 42 | + errcode: int |
| 43 | + """ |
| 44 | + Number of the error as defined in ``errcode.h``. |
| 45 | + """ |
| 46 | + |
| 47 | + fields: typing.Optional[dict] = None |
| 48 | + """ |
| 49 | + Additional fields depending on error type. For example, if |
| 50 | + :attr:`~tarantool.types.BoxErrorStackComponent.type` |
| 51 | + is ``"AccessDeniedError"``, then it will include ``"object_type"``, |
| 52 | + ``"object_name"``, ``"access_type"``. |
| 53 | + """ |
| 54 | + |
| 55 | +@dataclass |
| 56 | +class BoxError(): |
| 57 | + """ |
| 58 | + Type representing Tarantool `box.error`_ object. |
| 59 | +
|
| 60 | + .. _box.error: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_error/error/ |
| 61 | + """ |
| 62 | + |
| 63 | + stack: list |
| 64 | + """ |
| 65 | + Array of :class:`~tarantool.types.BoxErrorStackUnit` objects. |
| 66 | + """ |
| 67 | + |
| 68 | +MP_ERROR_STACK = 0x00 |
| 69 | +MP_ERROR_TYPE = 0x00 |
| 70 | +MP_ERROR_FILE = 0x01 |
| 71 | +MP_ERROR_LINE = 0x02 |
| 72 | +MP_ERROR_MESSAGE = 0x03 |
| 73 | +MP_ERROR_ERRNO = 0x04 |
| 74 | +MP_ERROR_ERRCODE = 0x05 |
| 75 | +MP_ERROR_FIELDS = 0x06 |
| 76 | + |
| 77 | +def decode_box_error(mp_map): |
| 78 | + """ |
| 79 | + Decode MessagePack map received from Tarantool to `box.error`_ |
| 80 | + object representation. |
| 81 | +
|
| 82 | + :param mp_map: Error MessagePack map received from Tarantool. |
| 83 | + :type mp_map: :obj:`dict` |
| 84 | +
|
| 85 | + :rtype: :class:`~tarantool.types.BoxError` |
| 86 | + """ |
| 87 | + |
| 88 | + encoded_stack = mp_map.get(MP_ERROR_STACK, []) |
| 89 | + stack = [] |
| 90 | + |
| 91 | + for item in encoded_stack: |
| 92 | + stack.append(BoxErrorStackUnit( |
| 93 | + item.get(MP_ERROR_TYPE), |
| 94 | + item.get(MP_ERROR_FILE), |
| 95 | + item.get(MP_ERROR_LINE), |
| 96 | + item.get(MP_ERROR_MESSAGE), |
| 97 | + item.get(MP_ERROR_ERRNO), |
| 98 | + item.get(MP_ERROR_ERRCODE), |
| 99 | + item.get(MP_ERROR_FIELDS), |
| 100 | + )) |
| 101 | + |
| 102 | + return BoxError(stack) |
0 commit comments