Skip to content

Commit 9da8d62

Browse files
loryrutagreenrobot
authored andcommitted
Add exceptions.py #64
Throwing exceptions of different types according to code
1 parent f9fc86a commit 9da8d62

File tree

4 files changed

+272
-54
lines changed

4 files changed

+272
-54
lines changed

objectbox/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from objectbox.model.entity import Entity
2020
from objectbox.model.properties import Id, String, Index, Bool, Int8, Int16, Int32, Int64, Float32, Float64, Bytes, BoolVector, Int8Vector, Int16Vector, Int32Vector, Int64Vector, Float32Vector, Float64Vector, CharVector, BoolList, Int8List, Int16List, Int32List, Int64List, Float32List, Float64List, CharList, Date, DateNano, Flex, HnswIndex, VectorDistanceType, HnswFlags
2121
from objectbox.model.model import Model
22-
from objectbox.c import DbException, CoreException, NotFoundException, version_core, DebugFlags
22+
from objectbox.c import DbException, CoreException, version_core, DebugFlags
2323
from objectbox.version import Version
2424
from objectbox.condition import PropertyQueryCondition
2525
from objectbox.query import Query
@@ -67,7 +67,6 @@
6767
'Store',
6868
'ObjectBox',
6969
'CoreException',
70-
'NotFoundException',
7170
'version',
7271
'version_info',
7372
'DebugFlags',

objectbox/c.py

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -260,77 +260,89 @@ class DbException(Exception):
260260
pass
261261

262262

263+
class CoreExceptionCode(IntEnum):
264+
OBX_SUCCESS = 0
265+
OBX_NOT_FOUND = 404
266+
OBX_NO_SUCCESS = 1001
267+
OBX_TIMEOUT = 1002
268+
OBX_ERROR_ILLEGAL_STATE = 10001
269+
OBX_ERROR_ILLEGAL_ARGUMENT = 10002
270+
OBX_ERROR_ALLOCATION = 10003
271+
OBX_ERROR_NUMERIC_OVERFLOW = 10004
272+
OBX_ERROR_FEATURE_NOT_AVAILABLE = 10005
273+
OBX_ERROR_SHUTTING_DOWN = 10006
274+
OBX_ERROR_IO = 10007
275+
OBX_ERROR_BACKUP_FILE_INVALID = 10008
276+
OBX_ERROR_NO_ERROR_INFO = 10097
277+
OBX_ERROR_GENERAL = 10098
278+
OBX_ERROR_UNKNOWN = 10099
279+
OBX_ERROR_DB_FULL = 10101
280+
OBX_ERROR_MAX_READERS_EXCEEDED = 10102
281+
OBX_ERROR_STORE_MUST_SHUTDOWN = 10103
282+
OBX_ERROR_MAX_DATA_SIZE_EXCEEDED = 10104
283+
OBX_ERROR_DB_GENERAL = 10198
284+
OBX_ERROR_STORAGE_GENERAL = 10199
285+
OBX_ERROR_UNIQUE_VIOLATED = 10201
286+
OBX_ERROR_NON_UNIQUE_RESULT = 10202
287+
OBX_ERROR_PROPERTY_TYPE_MISMATCH = 10203
288+
OBX_ERROR_ID_ALREADY_EXISTS = 10210
289+
OBX_ERROR_ID_NOT_FOUND = 10211
290+
OBX_ERROR_TIME_SERIES = 10212
291+
OBX_ERROR_CONSTRAINT_VIOLATED = 10299
292+
OBX_ERROR_STD_ILLEGAL_ARGUMENT = 10301
293+
OBX_ERROR_STD_OUT_OF_RANGE = 10302
294+
OBX_ERROR_STD_LENGTH = 10303
295+
OBX_ERROR_STD_BAD_ALLOC = 10304
296+
OBX_ERROR_STD_RANGE = 10305
297+
OBX_ERROR_STD_OVERFLOW = 10306
298+
OBX_ERROR_STD_OTHER = 10399
299+
OBX_ERROR_SCHEMA = 10501
300+
OBX_ERROR_FILE_CORRUPT = 10502
301+
OBX_ERROR_FILE_PAGES_CORRUPT = 10503
302+
OBX_ERROR_SCHEMA_OBJECT_NOT_FOUND = 10504
303+
OBX_ERROR_TREE_MODEL_INVALID = 10601
304+
OBX_ERROR_TREE_VALUE_TYPE_MISMATCH = 10602
305+
OBX_ERROR_TREE_PATH_NON_UNIQUE = 10603
306+
OBX_ERROR_TREE_PATH_ILLEGAL = 10604
307+
OBX_ERROR_TREE_OTHER = 10699
308+
309+
263310
# TODO rename?
264311
class CoreException(DbException):
265-
""" A database exception having a ``code`` attribute for error details. """
266-
267-
codes = {
268-
0: "SUCCESS",
269-
404: "NOT_FOUND",
270-
10001: "ILLEGAL_STATE",
271-
10002: "ILLEGAL_ARGUMENT",
272-
10003: "ALLOCATION",
273-
10097: "NO_ERROR_INFO",
274-
10098: "GENERAL",
275-
10099: "UNKNOWN",
276-
10101: "DB_FULL",
277-
10102: "MAX_READERS_EXCEEDED",
278-
10103: "STORE_MUST_SHUTDOWN",
279-
10199: "STORAGE_GENERAL",
280-
10201: "UNIQUE_VIOLATED",
281-
10202: "NON_UNIQUE_RESULT",
282-
10203: "PROPERTY_TYPE_MISMATCH",
283-
10299: "CONSTRAINT_VIOLATED",
284-
10301: "STD_ILLEGAL_ARGUMENT",
285-
10302: "STD_OUT_OF_RANGE",
286-
10303: "STD_LENGTH",
287-
10304: "STD_BAD_ALLOC",
288-
10305: "STD_RANGE",
289-
10306: "STD_OVERFLOW",
290-
10399: "STD_OTHER",
291-
10501: "SCHEMA",
292-
10502: "FILE_CORRUPT"
293-
}
294-
295-
def __init__(self, code: int):
296-
self.code = code
312+
"""A database exception having a ``code`` attribute for error details."""
313+
code = CoreExceptionCode.OBX_NO_SUCCESS # Re-defined by the derived classes
314+
315+
def __init__(self):
297316
self.message = py_str(C.obx_last_error_message())
298-
name = self.codes[code] if code in self.codes else "n/a"
299-
super(CoreException, self).__init__("%d (%s) - %s" % (code, name, self.message))
317+
super(CoreException, self).__init__("%d (%s) - %s" % (self.code.value, self.code.name, self.message))
300318

301319
@staticmethod
302320
def last():
303-
""" Creates a CoreException of the last error that was generated in core. """
321+
"""Creates a CoreException of the last error that was generated in core."""
304322
return CoreException(C.obx_last_error())
305323

306324

307-
class NotFoundException(CoreException):
308-
""" Raised when an object is not found. """
309-
310-
def __init__(self):
311-
super().__init__(404)
312-
313-
314325
def check_obx_err(code: obx_err, func, args) -> obx_err:
315326
""" Raises an exception if obx_err is not successful. """
316-
if code == 404:
317-
raise NotFoundException()
318-
elif code != 0:
319-
raise CoreException(code)
327+
if code != CoreExceptionCode.OBX_SUCCESS:
328+
from objectbox.exceptions import create_core_exception
329+
raise create_core_exception(code)
320330
return code
321331

322332

323-
def check_obx_qb_cond(code: obx_qb_cond, func, args) -> obx_qb_cond:
333+
def check_obx_qb_cond(qb_cond: obx_qb_cond, func, args) -> obx_qb_cond:
324334
""" Raises an exception if obx_qb_cond is not successful. """
325-
if code == 0:
326-
raise CoreException(code)
327-
return code
335+
if qb_cond == 0:
336+
from objectbox.exceptions import create_core_exception
337+
raise create_core_exception(C.obx_last_error_code())
338+
return qb_cond
328339

329340

330341
# assert that the returned pointer/int is non-empty
331342
def check_result(result, func, args):
332343
if not result:
333-
raise CoreException(C.obx_last_error_code())
344+
from objectbox.exceptions import create_core_exception
345+
raise create_core_exception(C.obx_last_error_code())
334346
return result
335347

336348

objectbox/exceptions.py

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
from typing import Dict, Type
2+
3+
from objectbox.c import CoreException, CoreExceptionCode
4+
5+
6+
class NotFoundException(CoreException):
7+
"""Raised when an object is not found."""
8+
code = CoreExceptionCode.OBX_NOT_FOUND
9+
10+
11+
class NoSuccessException(CoreException):
12+
code = CoreExceptionCode.OBX_NO_SUCCESS
13+
14+
15+
class TimeoutException(CoreException):
16+
code = CoreExceptionCode.OBX_TIMEOUT
17+
18+
19+
class IllegalStateError(CoreException):
20+
code = CoreExceptionCode.OBX_ERROR_ILLEGAL_STATE
21+
22+
23+
class IllegalArgumentError(CoreException):
24+
code = CoreExceptionCode.OBX_ERROR_ILLEGAL_ARGUMENT
25+
26+
27+
class AllocationError(CoreException):
28+
code = CoreExceptionCode.OBX_ERROR_ALLOCATION
29+
30+
31+
class NumericOverflowError(CoreException):
32+
code = CoreExceptionCode.OBX_ERROR_NUMERIC_OVERFLOW
33+
34+
35+
class FeatureNotAvailable(CoreException):
36+
code = CoreExceptionCode.OBX_ERROR_FEATURE_NOT_AVAILABLE
37+
38+
39+
class ShuttingDownError(CoreException):
40+
code = CoreExceptionCode.OBX_ERROR_SHUTTING_DOWN
41+
42+
43+
class IoError(CoreException):
44+
code = CoreExceptionCode.OBX_ERROR_IO
45+
46+
47+
class BackupFileInvalidError(CoreException):
48+
code = CoreExceptionCode.OBX_ERROR_BACKUP_FILE_INVALID
49+
50+
51+
class NoErrorInfoError(CoreException):
52+
code = CoreExceptionCode.OBX_ERROR_NO_ERROR_INFO
53+
54+
55+
class GeneralError(CoreException):
56+
code = CoreExceptionCode.OBX_ERROR_GENERAL
57+
58+
59+
class UnknownError(CoreException):
60+
code = CoreExceptionCode.OBX_ERROR_UNKNOWN
61+
62+
63+
class DbFullError(CoreException):
64+
code = CoreExceptionCode.OBX_ERROR_DB_FULL
65+
66+
67+
class MaxReadersExceededError(CoreException):
68+
code = CoreExceptionCode.OBX_ERROR_MAX_READERS_EXCEEDED
69+
70+
71+
class StoreMustShutdownError(CoreException):
72+
code = CoreExceptionCode.OBX_ERROR_STORE_MUST_SHUTDOWN
73+
74+
75+
class MaxDataSizeExceededError(CoreException):
76+
code = CoreExceptionCode.OBX_ERROR_MAX_DATA_SIZE_EXCEEDED
77+
78+
79+
class DbGeneralError(CoreException):
80+
code = CoreExceptionCode.OBX_ERROR_DB_GENERAL
81+
82+
83+
class StorageGeneralError(CoreException):
84+
code = CoreExceptionCode.OBX_ERROR_STORAGE_GENERAL
85+
86+
87+
class UniqueViolatedError(CoreException):
88+
code = CoreExceptionCode.OBX_ERROR_UNIQUE_VIOLATED
89+
90+
91+
class NonUniqueResultError(CoreException):
92+
code = CoreExceptionCode.OBX_ERROR_NON_UNIQUE_RESULT
93+
94+
95+
class PropertyTypeMismatchError(CoreException):
96+
code = CoreExceptionCode.OBX_ERROR_PROPERTY_TYPE_MISMATCH
97+
98+
99+
class IdAlreadyExistsError(CoreException):
100+
code = CoreExceptionCode.OBX_ERROR_ID_ALREADY_EXISTS
101+
102+
103+
class IdNotFoundError(CoreException):
104+
code = CoreExceptionCode.OBX_ERROR_ID_NOT_FOUND
105+
106+
107+
class TimeSeriesError(CoreException):
108+
code = CoreExceptionCode.OBX_ERROR_TIME_SERIES
109+
110+
111+
class ConstraintViolatedError(CoreException):
112+
code = CoreExceptionCode.OBX_ERROR_CONSTRAINT_VIOLATED
113+
114+
115+
class StdIllegalArgumentError(CoreException):
116+
code = CoreExceptionCode.OBX_ERROR_STD_ILLEGAL_ARGUMENT
117+
118+
119+
class StdOutOfRangeError(CoreException):
120+
code = CoreExceptionCode.OBX_ERROR_STD_OUT_OF_RANGE
121+
122+
123+
class StdLengthError(CoreException):
124+
code = CoreExceptionCode.OBX_ERROR_STD_LENGTH
125+
126+
127+
class StdBadAllocError(CoreException):
128+
code = CoreExceptionCode.OBX_ERROR_STD_BAD_ALLOC
129+
130+
131+
class StdRangeError(CoreException):
132+
code = CoreExceptionCode.OBX_ERROR_STD_RANGE
133+
134+
135+
class StdOverflowError(CoreException):
136+
code = CoreExceptionCode.OBX_ERROR_STD_OVERFLOW
137+
138+
139+
class StdOtherError(CoreException):
140+
code = CoreExceptionCode.OBX_ERROR_STD_OTHER
141+
142+
143+
class SchemaError(CoreException):
144+
code = CoreExceptionCode.OBX_ERROR_SCHEMA
145+
146+
147+
class FileCorruptError(CoreException):
148+
code = CoreExceptionCode.OBX_ERROR_FILE_CORRUPT
149+
150+
151+
class FilePagesCorruptError(CoreException):
152+
code = CoreExceptionCode.OBX_ERROR_FILE_PAGES_CORRUPT
153+
154+
155+
class SchemaObjectNotFoundError(CoreException):
156+
code = CoreExceptionCode.OBX_ERROR_SCHEMA_OBJECT_NOT_FOUND
157+
158+
159+
class TreeModelInvalidError(CoreException):
160+
code = CoreExceptionCode.OBX_ERROR_TREE_MODEL_INVALID
161+
162+
163+
class TreeValueTypeMismatchError(CoreException):
164+
code = CoreExceptionCode.OBX_ERROR_TREE_VALUE_TYPE_MISMATCH
165+
166+
167+
class TreePathNonUniqueError(CoreException):
168+
code = CoreExceptionCode.OBX_ERROR_TREE_PATH_NON_UNIQUE
169+
170+
171+
class TreePathIllegalError(CoreException):
172+
code = CoreExceptionCode.OBX_ERROR_TREE_PATH_ILLEGAL
173+
174+
175+
class TreeOtherError(CoreException):
176+
code = CoreExceptionCode.OBX_ERROR_TREE_OTHER
177+
178+
179+
obx_core_exceptions_map: Dict[int, Type] = {}
180+
181+
182+
def _init_core_exceptions_map():
183+
import inspect
184+
import sys
185+
186+
def is_core_exception_subclass(element_) -> bool:
187+
valid = True
188+
valid &= inspect.isclass(element_)
189+
valid &= hasattr(element_, "code")
190+
return valid
191+
192+
this_module = sys.modules[__name__]
193+
for name, element in inspect.getmembers(this_module):
194+
if is_core_exception_subclass(element):
195+
obx_core_exceptions_map[element.code] = element
196+
197+
198+
_init_core_exceptions_map()
199+
200+
201+
def create_core_exception(code: int) -> CoreException:
202+
if code == CoreExceptionCode.OBX_SUCCESS:
203+
raise Exception(f"Can't create a CoreException for code: OBX_SUCCESS")
204+
elif code not in obx_core_exceptions_map:
205+
raise Exception(f"Unrecognized CoreException code: {code}")
206+
return obx_core_exceptions_map[code]()

tests/test_basics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# limitations under the License.
1313

1414
import objectbox
15-
from objectbox.c import NotFoundException, CoreException
15+
from objectbox.c import CoreException
16+
from objectbox.exceptions import NotFoundException
1617
from tests.common import create_test_store
1718

1819

0 commit comments

Comments
 (0)