Skip to content

Commit 2146168

Browse files
committed
Print error instead of throwing
Move try/catch block outside loadFontIfNecessary
1 parent 28b021f commit 2146168

File tree

2 files changed

+59
-61
lines changed

2 files changed

+59
-61
lines changed

lib/src/google_fonts_base.dart

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ TextStyle googleFontsTextStyle({
9494
fontUrl: fonts[matchedVariant],
9595
);
9696

97-
loadFontIfNecessary(descriptor);
97+
try {
98+
loadFontIfNecessary(descriptor);
99+
} catch (e) {
100+
final fontName = descriptor.familyWithVariant.toApiFilenamePrefix();
101+
print('error: google_fonts was unable to load font $fontName\n$e');
102+
}
98103

99104
return textStyle.copyWith(
100105
fontFamily: familyWithVariant.toString(),
@@ -121,40 +126,34 @@ Future<void> loadFontIfNecessary(GoogleFontsDescriptor descriptor) async {
121126

122127
Future<ByteData> byteData;
123128

124-
try {
125-
// Check if this font can be loaded by the pre-bundled assets.
126-
final assetManifestJson = await _loadAssetManifestJson();
127-
final assetPath = getFamilyWithVariantManifestPath(
128-
descriptor.familyWithVariant,
129-
assetManifestJson,
130-
);
131-
if (assetPath != '') {
132-
byteData = rootBundle.load(assetPath);
133-
}
134-
if (await byteData != null) {
135-
return loadFontByteData(familyWithVariantString, byteData);
136-
}
137-
138-
// Check if this font can be loaded from the device file system.
139-
if (!kIsWeb) {
140-
byteData = _loadFontFromDeviceFileSystem(familyWithVariantString);
141-
}
142-
if (await byteData != null) {
143-
return loadFontByteData(familyWithVariantString, byteData);
144-
}
129+
// Check if this font can be loaded by the pre-bundled assets.
130+
final assetManifestJson = await _loadAssetManifestJson();
131+
final assetPath = getFamilyWithVariantManifestPath(
132+
descriptor.familyWithVariant,
133+
assetManifestJson,
134+
);
135+
if (assetPath != '') {
136+
byteData = rootBundle.load(assetPath);
137+
}
138+
if (await byteData != null) {
139+
return loadFontByteData(familyWithVariantString, byteData);
140+
}
145141

146-
// Attempt to load this font via http.
147-
byteData = _httpFetchFontAndSaveToDevice(
148-
familyWithVariantString,
149-
descriptor.fontUrl,
150-
);
151-
if (await byteData != null) {
152-
return loadFontByteData(familyWithVariantString, byteData);
153-
}
154-
} catch (e) {
155-
final fontName = descriptor.familyWithVariant.toApiFilenamePrefix();
156-
throw Exception('google_fonts was unable to load font $fontName\n$e');
142+
// Check if this font can be loaded from the device file system.
143+
if (!kIsWeb) {
144+
byteData = _loadFontFromDeviceFileSystem(familyWithVariantString);
145+
}
146+
if (await byteData != null) {
147+
return loadFontByteData(familyWithVariantString, byteData);
157148
}
149+
150+
// Attempt to load this font via http.
151+
byteData = _httpFetchFontAndSaveToDevice(
152+
familyWithVariantString,
153+
descriptor.fontUrl,
154+
);
155+
await byteData;
156+
return loadFontByteData(familyWithVariantString, byteData);
158157
}
159158

160159
/// Loads a font with [FontLoader], given its name and byte-representation.

test/load_font_if_necessary_test.dart

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,33 @@ main() {
108108
);
109109
});
110110

111+
testWidgets('loadFontIfNecessary method throws if font cannot be loaded',
112+
(tester) async {
113+
// Mock a bad response.
114+
when(httpClient.get(any)).thenAnswer((_) async {
115+
return http.Response('fake response body - failure', 300);
116+
});
117+
118+
final fooUrl = Uri.http('fonts.google.com', '/Foo');
119+
final descriptorInAssets = GoogleFontsDescriptor(
120+
familyWithVariant: GoogleFontsFamilyWithVariant(
121+
family: 'Foo',
122+
googleFontsVariant: GoogleFontsVariant(
123+
fontWeight: FontWeight.w900,
124+
fontStyle: FontStyle.italic,
125+
),
126+
),
127+
fontUrl: fooUrl.toString(),
128+
);
129+
130+
// Call loadFontIfNecessary and verify that it throws an exception.
131+
expect(
132+
loadFontIfNecessary(descriptorInAssets),
133+
throwsA(predicate((e) => e.message.toString().startsWith(
134+
'Failed to load font with url: http://fonts.google.com/Foo'))),
135+
);
136+
});
137+
111138
testWidgets(
112139
'loadFontIfNecessary method does nothing if the font is in the '
113140
'Asset Manifest', (tester) async {
@@ -154,32 +181,4 @@ main() {
154181
await loadFontIfNecessary(descriptorNotInAssets);
155182
verify(httpClient.get(barUrl)).called(1);
156183
});
157-
158-
testWidgets('loadFontIfNecessary method throws if font cannot be loaded',
159-
(tester) async {
160-
// Mock a bad response.
161-
when(httpClient.get(any)).thenAnswer((_) async {
162-
return http.Response('fake response body - failure', 300);
163-
});
164-
165-
final fooUrl = Uri.http('fonts.google.com', '/Foo');
166-
final descriptorInAssets = GoogleFontsDescriptor(
167-
familyWithVariant: GoogleFontsFamilyWithVariant(
168-
family: 'Foo',
169-
googleFontsVariant: GoogleFontsVariant(
170-
fontWeight: FontWeight.w900,
171-
fontStyle: FontStyle.italic,
172-
),
173-
),
174-
fontUrl: fooUrl.toString(),
175-
);
176-
177-
// Call loadFontIfNecessary and verify that it throws an exception.
178-
expect(
179-
loadFontIfNecessary(descriptorInAssets),
180-
throwsA(predicate((e) => e.message
181-
.toString()
182-
.startsWith('google_fonts was unable to load font Foo'))),
183-
);
184-
});
185184
}

0 commit comments

Comments
 (0)