Skip to content

Commit 6e9c606

Browse files
Add StorageException, includes errorCode as property (#31)
1 parent f83fbb9 commit 6e9c606

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

objectbox/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Allow `analyzer` with major version 5. #487
1111
* Generator not longer warns that it can not find the package source root if the output directory is
1212
the package root directory.
13+
* Add `StorageException` which is a `ObjectBoxException` with an `errorCode` (a `OBX_ERROR` code).
1314

1415
## 1.6.2 (2022-08-24)
1516

objectbox/lib/src/common.dart

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ class ObjectBoxException implements Exception {
2828
String toString() => 'ObjectBoxException: $message';
2929
}
3030

31+
/// ObjectBox database exception with an OBX_ERROR code.
32+
class StorageException extends ObjectBoxException {
33+
/// OBX_ERROR code as defined in [objectbox.h of the C library](https://github.com/objectbox/objectbox-c/blob/main/include/objectbox.h).
34+
final int errorCode;
35+
36+
/// Create with a message and OBX_ERROR code.
37+
StorageException(String message, this.errorCode) : super(message);
38+
39+
@override
40+
String toString() => 'StorageException: $message (OBX_ERROR code $errorCode)';
41+
}
42+
3143
/// A unique constraint would have been violated by this database operation.
3244
class UniqueViolationException extends ObjectBoxException {
3345
/// Create a new exception.

objectbox/lib/src/native/bindings/helpers.dart

+15-7
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,33 @@ class ObjectBoxNativeError {
5757

5858
ObjectBoxNativeError(this.code, this.message, this.context);
5959

60-
String get fullMessage =>
61-
context == null ? '$code $message' : '$context: $code $message';
60+
String get messageWithContext =>
61+
context == null ? message : '$context: $message';
62+
63+
String get messageWithErrorCode => code == 0
64+
? messageWithContext
65+
: '$messageWithContext (OBX_ERROR code $code)';
6266

6367
Never throwMapped() {
6468
switch (code) {
6569
case OBX_ERROR_ILLEGAL_STATE:
66-
throw StateError(fullMessage);
70+
throw StateError(messageWithErrorCode);
6771
case OBX_ERROR_ILLEGAL_ARGUMENT:
6872
case OBX_ERROR_STD_ILLEGAL_ARGUMENT:
69-
throw ArgumentError(fullMessage);
73+
throw ArgumentError(messageWithErrorCode);
7074
case OBX_ERROR_NUMERIC_OVERFLOW:
7175
case OBX_ERROR_STD_OUT_OF_RANGE:
7276
case OBX_ERROR_STD_RANGE:
7377
case OBX_ERROR_STD_OVERFLOW:
74-
throw RangeError(fullMessage);
78+
throw RangeError(messageWithErrorCode);
7579
case OBX_ERROR_UNIQUE_VIOLATED:
76-
throw UniqueViolationException(fullMessage);
80+
throw UniqueViolationException(messageWithContext);
7781
default:
78-
throw ObjectBoxException(fullMessage);
82+
if (code == 0) {
83+
throw ObjectBoxException(messageWithContext);
84+
} else {
85+
throw StorageException(messageWithContext, code);
86+
}
7987
}
8088
}
8189
}

objectbox/test/box_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ void main() {
660660
TxMode.write, () => box.putMany(param));
661661
}, simpleItems()),
662662
throwsA(predicate((StateError e) => e.toString().contains(
663-
'Bad state: failed to create transaction: 10001 Cannot start a write transaction inside a read only transaction'))));
663+
'Bad state: failed to create transaction: Cannot start a write transaction inside a read only transaction (OBX_ERROR code 10001)'))));
664664
}, skip: notAtLeastDart2_15_0());
665665

666666
test('failing in recursive txn', () {

objectbox/test/store_test.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:io';
33
import 'dart:isolate';
44

55
import 'package:async/async.dart';
6+
import 'package:objectbox/src/native/bindings/bindings.dart';
67
import 'package:objectbox/src/store.dart';
78
import 'package:test/test.dart';
89

@@ -247,8 +248,9 @@ void main() {
247248
expect(
248249
() => box.put(testEntity2),
249250
throwsA(predicate((e) =>
250-
e is ObjectBoxException &&
251-
e.message == 'object put failed: 10101 Could not put')));
251+
e is StorageException &&
252+
e.errorCode == OBX_ERROR_DB_FULL &&
253+
e.message == 'object put failed: Could not put')));
252254

253255
// Re-open with larger size.
254256
store.close();

0 commit comments

Comments
 (0)