Skip to content

Commit e5c2b9b

Browse files
committed
test throwing in base_box, cleanup
1 parent 22bed30 commit e5c2b9b

File tree

8 files changed

+472
-38
lines changed

8 files changed

+472
-38
lines changed

flutter/example/lib/main.dart

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import 'dart:async';
44
import 'dart:convert';
5-
import 'dart:io';
65

76
import 'package:flutter/foundation.dart';
87
import 'package:flutter/material.dart';
@@ -439,7 +438,7 @@ class MainScaffold extends StatelessWidget {
439438
bindToScope: true,
440439
);
441440

442-
var appDir = await getApplicationDocumentsDirectory();
441+
final appDir = await getApplicationDocumentsDirectory();
443442
SentryHive.init(appDir.path);
444443

445444
final catsBox = await SentryHive.openBox<Map>('cats');

hive/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Integration for the [`hive`](https://pub.dev/packages/hive) package.
2828
import 'package:sentry/sentry.dart';
2929
import 'package:hive/hive.dart';
3030
import 'package:sentry_hive/sentry_hive.dart';
31+
import 'package:path_provider/path_provider.dart';
3132
3233
Future<void> main() async {
3334
await SentryFlutter.init(
@@ -41,8 +42,9 @@ Future<void> main() async {
4142
4243
Future<void> insertUser() async {
4344
// Use [SentryHive] where you would use [Hive]
45+
final appDir = await getApplicationDocumentsDirectory();
4446
SentryHive
45-
..init(Directory.current.path)
47+
..init(appDir.path)
4648
..registerAdapter(PersonAdapter());
4749
4850
var box = await SentryHive.openBox('testBox');

hive/lib/src/sentry_box_base.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ class SentryBoxBase<E> implements BoxBase<E> {
1515
@override
1616
Future<int> add(E value) async {
1717
return _asyncWrapInSpan('add', () async {
18-
return await _boxBase.add(value);
18+
try {
19+
return await _boxBase.add(value);
20+
} catch (error) {
21+
rethrow;
22+
}
1923
});
2024
}
2125

hive/lib/src/sentry_hive_impl.dart

+43-28
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,24 @@ class SentryHiveImpl implements SentryHiveInterface {
118118
Uint8List? bytes,
119119
String? collection,
120120
List<int>? encryptionKey}) {
121-
return _asyncWrapInSpan('openBox', () async {
122-
final Box<E> box = await _hive.openBox(
123-
name,
124-
encryptionCipher: encryptionCipher,
125-
keyComparator: keyComparator,
126-
compactionStrategy: compactionStrategy,
127-
crashRecovery: crashRecovery,
128-
path: path,
129-
bytes: bytes,
130-
collection: collection,
131-
encryptionKey: encryptionKey,
132-
);
133-
return SentryBox(box, _hub);
134-
});
121+
return _asyncWrapInSpan(
122+
'openBox',
123+
() async {
124+
final Box<E> box = await _hive.openBox(
125+
name,
126+
encryptionCipher: encryptionCipher,
127+
keyComparator: keyComparator,
128+
compactionStrategy: compactionStrategy,
129+
crashRecovery: crashRecovery,
130+
path: path,
131+
bytes: bytes,
132+
collection: collection,
133+
encryptionKey: encryptionKey,
134+
);
135+
return SentryBox(box, _hub);
136+
},
137+
dbName: name,
138+
);
135139
}
136140

137141
@override
@@ -143,19 +147,23 @@ class SentryHiveImpl implements SentryHiveInterface {
143147
String? path,
144148
String? collection,
145149
List<int>? encryptionKey}) {
146-
return _asyncWrapInSpan('openLazyBox', () async {
147-
final LazyBox<E> lazyBox = await _hive.openLazyBox(
148-
name,
149-
encryptionCipher: encryptionCipher,
150-
keyComparator: keyComparator,
151-
compactionStrategy: compactionStrategy,
152-
crashRecovery: crashRecovery,
153-
path: path,
154-
collection: collection,
155-
encryptionKey: encryptionKey,
156-
);
157-
return SentryLazyBox(lazyBox, _hub);
158-
});
150+
return _asyncWrapInSpan(
151+
'openLazyBox',
152+
() async {
153+
final LazyBox<E> lazyBox = await _hive.openLazyBox(
154+
name,
155+
encryptionCipher: encryptionCipher,
156+
keyComparator: keyComparator,
157+
compactionStrategy: compactionStrategy,
158+
crashRecovery: crashRecovery,
159+
path: path,
160+
collection: collection,
161+
encryptionKey: encryptionKey,
162+
);
163+
return SentryLazyBox(lazyBox, _hub);
164+
},
165+
dbName: name,
166+
);
159167
}
160168

161169
@override
@@ -175,7 +183,10 @@ class SentryHiveImpl implements SentryHiveInterface {
175183
// Helper
176184

177185
Future<T> _asyncWrapInSpan<T>(
178-
String description, Future<T> Function() execute) async {
186+
String description,
187+
Future<T> Function() execute, {
188+
String? dbName,
189+
}) async {
179190
final currentSpan = _hub.getSpan();
180191
final span = currentSpan?.startChild(
181192
SentryHiveImpl.dbOp,
@@ -187,6 +198,10 @@ class SentryHiveImpl implements SentryHiveInterface {
187198

188199
span?.setData(SentryHiveImpl.dbSystemKey, SentryHiveImpl.dbSystem);
189200

201+
if (dbName != null) {
202+
span?.setData(SentryHiveImpl.dbNameKey, dbName);
203+
}
204+
190205
try {
191206
final result = await execute();
192207
span?.status = SpanStatus.ok();

hive/test/mocks/mocks.dart

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import 'package:hive/hive.dart';
12
import 'package:mockito/annotations.dart';
23
import 'package:sentry/sentry.dart';
34

45
@GenerateMocks([
56
Hub,
7+
Box,
68
])
79
void main() {}

0 commit comments

Comments
 (0)