Skip to content

Commit 754c630

Browse files
authored
SentryAssetBundle returns Future by default (#1462)
1 parent 6aab859 commit 754c630

File tree

2 files changed

+157
-132
lines changed

2 files changed

+157
-132
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancements
6+
7+
- `SentryAssetBundle` returns Future by default ([#1462](https://github.com/getsentry/sentry-dart/pull/1462))
8+
59
### Features
610

711
- Support `http` >= 1.0.0 ([#1475](https://github.com/getsentry/sentry-dart/pull/1475))

flutter/lib/src/sentry_asset_bundle.dart

Lines changed: 153 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,31 @@ class SentryAssetBundle implements AssetBundle {
5353
final bool _enableStructuredDataTracing;
5454

5555
@override
56-
Future<ByteData> load(String key) async {
57-
final span = _hub.getSpan()?.startChild(
58-
'file.read',
59-
description: 'AssetBundle.load: ${_fileName(key)}',
60-
);
61-
62-
span?.setData('file.path', key);
63-
64-
ByteData? data;
65-
try {
66-
data = await _bundle.load(key);
67-
_setDataLength(data, span);
68-
span?.status = SpanStatus.ok();
69-
} catch (exception) {
70-
span?.throwable = exception;
71-
span?.status = SpanStatus.internalError();
72-
rethrow;
73-
} finally {
74-
await span?.finish();
56+
Future<ByteData> load(String key) {
57+
Future<ByteData> future() async {
58+
final span = _hub.getSpan()?.startChild(
59+
'file.read',
60+
description: 'AssetBundle.load: ${_fileName(key)}',
61+
);
62+
63+
span?.setData('file.path', key);
64+
65+
ByteData? data;
66+
try {
67+
data = await _bundle.load(key);
68+
_setDataLength(data, span);
69+
span?.status = SpanStatus.ok();
70+
} catch (exception) {
71+
span?.throwable = exception;
72+
span?.status = SpanStatus.internalError();
73+
rethrow;
74+
} finally {
75+
await span?.finish();
76+
}
77+
return data;
7578
}
76-
return data;
79+
80+
return future();
7781
}
7882

7983
@override
@@ -85,106 +89,119 @@ class SentryAssetBundle implements AssetBundle {
8589
}
8690

8791
Future<T> _loadStructuredDataWithTracing<T>(
88-
String key, _StringParser<T> parser) async {
89-
final span = _hub.getSpan()?.startChild(
90-
'file.read',
91-
description: 'AssetBundle.loadStructuredData<$T>: ${_fileName(key)}',
92+
String key, _StringParser<T> parser) {
93+
Future<T> future() async {
94+
final span = _hub.getSpan()?.startChild(
95+
'file.read',
96+
description:
97+
'AssetBundle.loadStructuredData<$T>: ${_fileName(key)}',
98+
);
99+
span?.setData('file.path', key);
100+
101+
final completer = Completer<T>();
102+
103+
// This future is intentionally not awaited. Otherwise we deadlock with
104+
// the completer.
105+
// ignore: unawaited_futures
106+
runZonedGuarded(() async {
107+
final data = await _bundle.loadStructuredData(
108+
key,
109+
(value) async => await _wrapParsing(parser, value, key, span),
92110
);
93-
span?.setData('file.path', key);
94-
95-
final completer = Completer<T>();
96-
97-
// This future is intentionally not awaited. Otherwise we deadlock with
98-
// the completer.
99-
// ignore: unawaited_futures
100-
runZonedGuarded(() async {
101-
final data = await _bundle.loadStructuredData(
102-
key,
103-
(value) async => await _wrapParsing(parser, value, key, span),
104-
);
105-
span?.status = SpanStatus.ok();
106-
completer.complete(data);
107-
}, (exception, stackTrace) {
108-
completer.completeError(exception, stackTrace);
109-
});
110-
111-
T data;
112-
try {
113-
data = await completer.future;
114-
_setDataLength(data, span);
115-
span?.status = const SpanStatus.ok();
116-
} catch (e) {
117-
span?.throwable = e;
118-
span?.status = const SpanStatus.internalError();
119-
rethrow;
120-
} finally {
121-
await span?.finish();
111+
span?.status = SpanStatus.ok();
112+
completer.complete(data);
113+
}, (exception, stackTrace) {
114+
completer.completeError(exception, stackTrace);
115+
});
116+
117+
T data;
118+
try {
119+
data = await completer.future;
120+
_setDataLength(data, span);
121+
span?.status = const SpanStatus.ok();
122+
} catch (e) {
123+
span?.throwable = e;
124+
span?.status = const SpanStatus.internalError();
125+
rethrow;
126+
} finally {
127+
await span?.finish();
128+
}
129+
return data;
122130
}
123-
return data;
131+
132+
return future();
124133
}
125134

126135
Future<T> _loadStructuredBinaryDataWithTracing<T>(
127-
String key, _ByteParser<T> parser) async {
128-
final span = _hub.getSpan()?.startChild(
129-
'file.read',
130-
description:
131-
'AssetBundle.loadStructuredBinaryData<$T>: ${_fileName(key)}',
136+
String key, _ByteParser<T> parser) {
137+
Future<T> future() async {
138+
final span = _hub.getSpan()?.startChild(
139+
'file.read',
140+
description:
141+
'AssetBundle.loadStructuredBinaryData<$T>: ${_fileName(key)}',
142+
);
143+
span?.setData('file.path', key);
144+
145+
final completer = Completer<T>();
146+
147+
// This future is intentionally not awaited. Otherwise we deadlock with
148+
// the completer.
149+
// ignore: unawaited_futures
150+
runZonedGuarded(() async {
151+
final data = await _loadStructuredBinaryDataWrapper(
152+
key,
153+
(value) async => await _wrapBinaryParsing(parser, value, key, span),
132154
);
133-
span?.setData('file.path', key);
134-
135-
final completer = Completer<T>();
136-
137-
// This future is intentionally not awaited. Otherwise we deadlock with
138-
// the completer.
139-
// ignore: unawaited_futures
140-
runZonedGuarded(() async {
141-
final data = await _loadStructuredBinaryDataWrapper(
142-
key,
143-
(value) async => await _wrapBinaryParsing(parser, value, key, span),
144-
);
145-
span?.status = SpanStatus.ok();
146-
completer.complete(data);
147-
}, (exception, stackTrace) {
148-
completer.completeError(exception, stackTrace);
149-
});
150-
151-
T data;
152-
try {
153-
data = await completer.future;
154-
_setDataLength(data, span);
155-
span?.status = const SpanStatus.ok();
156-
} catch (e) {
157-
span?.throwable = e;
158-
span?.status = const SpanStatus.internalError();
159-
rethrow;
160-
} finally {
161-
await span?.finish();
155+
span?.status = SpanStatus.ok();
156+
completer.complete(data);
157+
}, (exception, stackTrace) {
158+
completer.completeError(exception, stackTrace);
159+
});
160+
161+
T data;
162+
try {
163+
data = await completer.future;
164+
_setDataLength(data, span);
165+
span?.status = const SpanStatus.ok();
166+
} catch (e) {
167+
span?.throwable = e;
168+
span?.status = const SpanStatus.internalError();
169+
rethrow;
170+
} finally {
171+
await span?.finish();
172+
}
173+
return data;
162174
}
163-
return data;
175+
176+
return future();
164177
}
165178

166179
@override
167-
Future<String> loadString(String key, {bool cache = true}) async {
168-
final span = _hub.getSpan()?.startChild(
169-
'file.read',
170-
description: 'AssetBundle.loadString: ${_fileName(key)}',
171-
);
172-
173-
span?.setData('file.path', key);
174-
span?.setData('from-cache', cache);
175-
176-
String? data;
177-
try {
178-
data = await _bundle.loadString(key, cache: cache);
179-
span?.status = SpanStatus.ok();
180-
} catch (exception) {
181-
span?.throwable = exception;
182-
span?.status = SpanStatus.internalError();
183-
rethrow;
184-
} finally {
185-
await span?.finish();
180+
Future<String> loadString(String key, {bool cache = true}) {
181+
Future<String> future() async {
182+
final span = _hub.getSpan()?.startChild(
183+
'file.read',
184+
description: 'AssetBundle.loadString: ${_fileName(key)}',
185+
);
186+
187+
span?.setData('file.path', key);
188+
span?.setData('from-cache', cache);
189+
190+
String? data;
191+
try {
192+
data = await _bundle.loadString(key, cache: cache);
193+
span?.status = SpanStatus.ok();
194+
} catch (exception) {
195+
span?.throwable = exception;
196+
span?.status = SpanStatus.internalError();
197+
rethrow;
198+
} finally {
199+
await span?.finish();
200+
}
201+
return data;
186202
}
187-
return data;
203+
204+
return future();
188205
}
189206

190207
void _setDataLength(dynamic data, ISentrySpan? span) {
@@ -220,30 +237,34 @@ class SentryAssetBundle implements AssetBundle {
220237
@override
221238
// This is an override on Flutter greater than 3.1
222239
// ignore: override_on_non_overriding_member
223-
Future<ImmutableBuffer> loadBuffer(String key) async {
224-
final span = _hub.getSpan()?.startChild(
225-
'file.read',
226-
description: 'AssetBundle.loadBuffer: ${_fileName(key)}',
227-
);
228-
229-
span?.setData('file.path', key);
230-
231-
ImmutableBuffer data;
232-
try {
233-
data = await _loadBuffer(key);
234-
_setDataLength(data, span);
235-
span?.status = SpanStatus.ok();
236-
} catch (exception) {
237-
span?.throwable = exception;
238-
span?.status = SpanStatus.internalError();
239-
rethrow;
240-
} finally {
241-
await span?.finish();
240+
Future<ImmutableBuffer> loadBuffer(String key) {
241+
Future<ImmutableBuffer> future() async {
242+
final span = _hub.getSpan()?.startChild(
243+
'file.read',
244+
description: 'AssetBundle.loadBuffer: ${_fileName(key)}',
245+
);
246+
247+
span?.setData('file.path', key);
248+
249+
ImmutableBuffer data;
250+
try {
251+
data = await _loadBuffer(key);
252+
_setDataLength(data, span);
253+
span?.status = SpanStatus.ok();
254+
} catch (exception) {
255+
span?.throwable = exception;
256+
span?.status = SpanStatus.internalError();
257+
rethrow;
258+
} finally {
259+
await span?.finish();
260+
}
261+
return data;
242262
}
243-
return data;
263+
264+
return future();
244265
}
245266

246-
Future<ImmutableBuffer> _loadBuffer(String key) async {
267+
Future<ImmutableBuffer> _loadBuffer(String key) {
247268
try {
248269
// ignore: return_of_invalid_type
249270
return (_bundle as dynamic).loadBuffer(key);
@@ -323,7 +344,7 @@ class SentryAssetBundle implements AssetBundle {
323344
Future<T> loadStructuredBinaryData<T>(
324345
String key,
325346
FutureOr<T> Function(ByteData data) parser,
326-
) async {
347+
) {
327348
if (_enableStructuredDataTracing) {
328349
return _loadStructuredBinaryDataWithTracing<T>(key, parser);
329350
}
@@ -335,7 +356,7 @@ class SentryAssetBundle implements AssetBundle {
335356
Future<T> _loadStructuredBinaryDataWrapper<T>(
336357
String key,
337358
FutureOr<T> Function(ByteData data) parser,
338-
) async {
359+
) {
339360
// The loadStructuredBinaryData method exists as of Flutter greater than 3.8
340361
// Previous versions don't have it, but later versions do.
341362
// We can't use `extends` in order to provide this method because this is

0 commit comments

Comments
 (0)