Skip to content

Commit 0f1d0e3

Browse files
authored
🎨 Improve exceptions thrown by asset bundle (#114313)
1 parent c2edb20 commit 0f1d0e3

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

packages/flutter/lib/src/services/asset_bundle.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ abstract class AssetBundle {
8181
/// isolate to avoid jank on the main thread.
8282
Future<String> loadString(String key, { bool cache = true }) async {
8383
final ByteData data = await load(key);
84-
if (data == null) {
85-
throw FlutterError('Unable to load asset: $key');
86-
}
8784
// 50 KB of data should take 2-3 ms to parse on a Moto G4, and about 400 μs
8885
// on a Pixel 4.
8986
if (data.lengthInBytes < 50 * 1024) {
@@ -139,7 +136,7 @@ class NetworkAssetBundle extends AssetBundle {
139136
final HttpClientResponse response = await request.close();
140137
if (response.statusCode != HttpStatus.ok) {
141138
throw FlutterError.fromParts(<DiagnosticsNode>[
142-
ErrorSummary('Unable to load asset: $key'),
139+
_errorSummaryWithKey(key),
143140
IntProperty('HTTP status code', response.statusCode),
144141
]);
145142
}
@@ -255,7 +252,10 @@ class PlatformAssetBundle extends CachingAssetBundle {
255252
final ByteData? asset =
256253
await ServicesBinding.instance.defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData());
257254
if (asset == null) {
258-
throw FlutterError('Unable to load asset: $key');
255+
throw FlutterError.fromParts(<DiagnosticsNode>[
256+
_errorSummaryWithKey(key),
257+
ErrorDescription('The asset does not exist or has empty data.'),
258+
]);
259259
}
260260
return asset;
261261
}
@@ -284,8 +284,11 @@ class PlatformAssetBundle extends CachingAssetBundle {
284284
}
285285
try {
286286
return await ui.ImmutableBuffer.fromAsset(key);
287-
} on Exception {
288-
throw FlutterError('Unable to load asset: $key.');
287+
} on Exception catch (e) {
288+
throw FlutterError.fromParts(<DiagnosticsNode>[
289+
_errorSummaryWithKey(key),
290+
ErrorDescription(e.toString()),
291+
]);
289292
}
290293
}
291294
}
@@ -294,6 +297,10 @@ AssetBundle _initRootBundle() {
294297
return PlatformAssetBundle();
295298
}
296299

300+
ErrorSummary _errorSummaryWithKey(String key) {
301+
return ErrorSummary('Unable to load asset: "$key".');
302+
}
303+
297304
/// The [AssetBundle] from which this application was loaded.
298305
///
299306
/// The [rootBundle] contains the resources that were packaged with the

packages/flutter/test/services/asset_bundle_test.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class TestAssetBundle extends CachingAssetBundle {
2727
}
2828

2929
void main() {
30+
TestWidgetsFlutterBinding.ensureInitialized();
31+
3032
test('Caching asset bundle test', () async {
3133
final TestAssetBundle bundle = TestAssetBundle();
3234

@@ -72,8 +74,8 @@ void main() {
7274
expect(
7375
error.toStringDeep(),
7476
'FlutterError\n'
75-
' Unable to load asset: key\n'
76-
' HTTP status code: 404\n',
77+
' Unable to load asset: "key".\n'
78+
' HTTP status code: 400\n',
7779
);
7880
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/39998
7981

@@ -83,4 +85,20 @@ void main() {
8385

8486
expect(bundle.toString(), 'NetworkAssetBundle#${shortHash(bundle)}($uri)');
8587
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/39998
88+
89+
test('Throws expected exceptions when loading not exists asset', () async {
90+
late final FlutterError error;
91+
try {
92+
await rootBundle.load('not-exists');
93+
} on FlutterError catch (e) {
94+
error = e;
95+
}
96+
expect(
97+
error.message,
98+
equals(
99+
'Unable to load asset: "not-exists".\n'
100+
'The asset does not exist or has empty data.',
101+
),
102+
);
103+
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/56314
86104
}

packages/flutter/test/widgets/image_test.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,13 @@ void main() {
19971997
find.byKey(key),
19981998
matchesGoldenFile('image_test.missing.1.png'),
19991999
);
2000-
expect(tester.takeException().toString(), startsWith('Unable to load asset: '));
2000+
expect(
2001+
tester.takeException().toString(),
2002+
equals(
2003+
'Unable to load asset: "missing-asset".\n'
2004+
'The asset does not exist or has empty data.',
2005+
),
2006+
);
20012007
await tester.pump();
20022008
await expectLater(
20032009
find.byKey(key),

0 commit comments

Comments
 (0)