Skip to content

Commit 6ccf204

Browse files
code-health: expose DatabaseError properties
Provide explicit handles to extract DatabaseError exceptions properties. Before this patch, one could extract Tarantool error code only through internal `args` property. Unfortunately, current exceptions API is inconsistent. Code uses `DatabaseError(code, message)`, `NetworkError(message)` and `NetworkError(exc)`, while NetworkError is a child class of DatabaseError. This patch shouldn't break current behavior. Part of #206
1 parent f32e9cb commit 6ccf204

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

tarantool/error.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ class DatabaseError(Error):
4141
Exception raised for errors that are related to the database.
4242
"""
4343

44+
def __init__(self, *args):
45+
"""
46+
:param args: ``(code, message)`` or ``(message,)``.
47+
:type args: :obj:`tuple`
48+
"""
49+
50+
super().__init__(*args)
51+
52+
if (len(args) == 2) and isinstance(args[0], int) and isinstance(args[1], (str, bytes)):
53+
self.code = args[0]
54+
self.message = args[1]
55+
elif (len(args) == 1) and isinstance(args[0], (str, bytes)):
56+
self.code = 0
57+
self.message = args[0]
58+
else:
59+
self.code = 0
60+
self.message = ''
61+
4462

4563
class DataError(DatabaseError):
4664
"""
@@ -206,8 +224,6 @@ def __init__(self, message, schema_version):
206224
"""
207225

208226
super(SchemaReloadException, self).__init__(109, message)
209-
self.code = 109
210-
self.message = message
211227
self.schema_version = schema_version
212228

213229
def __str__(self):

0 commit comments

Comments
 (0)