Skip to content

Commit f094be3

Browse files
Use better error handling for invalid returned pointers (#13)
1 parent 4846d9f commit f094be3

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1+
import "dart:ffi";
12
import "constants.dart";
23
import "../common.dart";
34

4-
check(cond) {
5-
if(!cond)
6-
throw AssertionError();
5+
checkObx(errorCode) {
6+
if(errorCode != OBXErrors.OBX_SUCCESS)
7+
throw ObjectBoxException(Common.lastErrorString(errorCode));
78
}
89

9-
checkObx(err) {
10-
if(err != OBXErrors.OBX_SUCCESS)
11-
throw ObjectBoxException(Common.lastErrorString(err));
12-
}
13-
14-
getSymbolName(Symbol sym) {
15-
return sym.toString().split('"')[1];
10+
checkObxPtr(Pointer ptr, String msg) {
11+
if(ptr == null || ptr.address == 0)
12+
throw ObjectBoxException(msg);
1613
}

objectbox/lib/src/box.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ class Box<T> {
4646
_entityBuilder = _store.getEntityBuilderFromClass<T>();
4747

4848
_objectboxBox = bindings.obx_box(_store.ptr, _entityDefinition["entity"]["id"]);
49-
check(_objectboxBox != null);
50-
check(_objectboxBox.address != 0);
49+
checkObxPtr(_objectboxBox, "failed to create box");
5150
}
5251

5352
_marshal(propVals) {
@@ -139,7 +138,7 @@ class Box<T> {
139138

140139
// get element with specified id from database
141140
Pointer<Void> txn = bindings.obx_txn_read(_store.ptr);
142-
check(txn != null && txn.address != 0);
141+
checkObxPtr(txn, "failed to created transaction");
143142
checkObx(bindings.obx_box_get(_objectboxBox, id, dataPtr, sizePtr));
144143
checkObx(bindings.obx_txn_close(txn));
145144
Pointer<Uint8> data = Pointer<Uint8>.fromAddress(dataPtr.load<Pointer<Void>>().address);

objectbox/lib/src/model.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Model {
2525

2626
Model(List<Map<String, dynamic>> modelDefinitions) {
2727
_objectboxModel = bindings.obx_model_create();
28-
check(_objectboxModel.address != 0);
28+
checkObxPtr(_objectboxModel, "failed to create model");
2929

3030
try {
3131
// transform classes into model descriptions and loop through them

objectbox/lib/src/store.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ class Store {
1414
var model = Model(defs.map((d) => d[1]["model"] as Map<String, dynamic>).toList());
1515

1616
var opt = bindings.obx_opt();
17-
check(opt.address != 0);
17+
checkObxPtr(opt, "failed to create store options");
1818
checkObx(bindings.obx_opt_model(opt, model.ptr));
19-
_objectboxStore = bindings.obx_store_open(opt);
20-
check(_objectboxStore != null);
21-
check(_objectboxStore.address != 0);
19+
_objectboxStore = null;//bindings.obx_store_open(opt);
20+
checkObxPtr(_objectboxStore, "failed to create store");
2221
}
2322

2423
close() {

0 commit comments

Comments
 (0)