Skip to content

Commit 654f134

Browse files
committed
Exceptions: documents and introduce type hierarchy #52
1 parent c513873 commit 654f134

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

objectbox/__init__.py

Lines changed: 1 addition & 1 deletion
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 CoreException, NotFoundException, version_core, DebugFlags
22+
from objectbox.c import DbException, CoreException, NotFoundException, version_core, DebugFlags
2323
from objectbox.version import Version
2424
from objectbox.condition import PropertyQueryCondition
2525
from objectbox.query import Query

objectbox/c.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,15 @@ class OBX_query(ctypes.Structure):
246246
C.obx_last_error_code.restype = obx_err
247247

248248

249-
class CoreException(Exception):
249+
class DbException(Exception):
250+
""" Base class for database exceptions. """
251+
pass
252+
253+
254+
# TODO rename?
255+
class CoreException(DbException):
256+
""" A database exception having a ``code`` attribute for error details. """
257+
250258
codes = {
251259
0: "SUCCESS",
252260
404: "NOT_FOUND",
@@ -287,8 +295,11 @@ def last():
287295
return CoreException(C.obx_last_error())
288296

289297

290-
class NotFoundException(Exception):
291-
pass
298+
class NotFoundException(CoreException):
299+
""" Raised when an object is not found. """
300+
301+
def __init__(self):
302+
super().__init__(404)
292303

293304

294305
def check_obx_err(code: obx_err, func, args) -> obx_err:

tests/test_basics.py

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

1414
import objectbox
15+
from objectbox.c import NotFoundException, CoreException
1516
from tests.common import create_test_store
1617

1718

@@ -32,3 +33,10 @@ def test_version():
3233
def test_open():
3334
store = create_test_store()
3435
store.close()
36+
37+
38+
def test_not_found_exception():
39+
try:
40+
raise NotFoundException()
41+
except CoreException as e:
42+
assert e.code == 404

0 commit comments

Comments
 (0)