Skip to content

Commit 0a7e1b3

Browse files
committed
Improved error handling
1 parent e7c0cd8 commit 0a7e1b3

5 files changed

Lines changed: 76 additions & 26 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
## 0.0.1
22

3-
* TODO: Describe initial release.
3+
### Added
4+
5+
- `getLanguages` method
6+
- `getThemes` method
7+
- `getHighlights` method

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MyApp extends StatefulWidget {
1313
}
1414

1515
class _MyAppState extends State<MyApp> {
16-
final _highlightsPlugin = HighlightsPlugin();
16+
final _highlightsPlugin = HighlightsPlugin(debug: true);
1717

1818
String? _code;
1919
String? _language;

lib/highlights_plugin.dart

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,63 @@
1+
import 'package:flutter/cupertino.dart';
12
import 'package:highlights_plugin/model/code_highlight.dart';
23
import 'package:highlights_plugin/model/phrase_location.dart';
3-
4+
import 'package:highlights_plugin/method_index.dart' as methods;
45
import 'highlights_interface.dart';
56
import 'highlights_plugin_platform_interface.dart';
67

78
class HighlightsPlugin implements HighlightsInterface {
9+
HighlightsPlugin({this.debug = false}) {
10+
// Setup before first `instance` getter call
11+
HighlightsPluginPlatform.debug = debug;
12+
}
13+
14+
final bool debug;
15+
816
@override
917
Future<List<CodeHighlight>> getHighlights(
1018
String code,
1119
String language,
1220
String theme,
1321
List<PhraseLocation> emphasisLocations,
14-
) {
15-
return HighlightsPluginPlatform.instance.getHighlights(
16-
code,
17-
language,
18-
theme,
19-
emphasisLocations,
20-
);
22+
) async {
23+
try {
24+
return await HighlightsPluginPlatform.instance.getHighlights(
25+
code,
26+
language,
27+
theme,
28+
emphasisLocations,
29+
);
30+
} catch (e, st) {
31+
_printDebugInfo(methods.getHighlights, e, st);
32+
return [];
33+
}
2134
}
2235

2336
@override
24-
Future<List<String>> getLanguages() {
25-
return HighlightsPluginPlatform.instance.getLanguages();
37+
Future<List<String>> getLanguages() async {
38+
try {
39+
return await HighlightsPluginPlatform.instance.getLanguages();
40+
} catch (e, st) {
41+
_printDebugInfo(methods.getLanguages, e, st);
42+
return [];
43+
}
2644
}
2745

46+
// TODO Create release 0.0.1 and 0.0.2
2847
@override
29-
Future<List<String>> getThemes() {
30-
return HighlightsPluginPlatform.instance.getThemes();
48+
Future<List<String>> getThemes() async {
49+
try {
50+
return await HighlightsPluginPlatform.instance.getThemes();
51+
} catch (e, st) {
52+
_printDebugInfo(methods.getThemes, e, st);
53+
return [];
54+
}
55+
}
56+
57+
void _printDebugInfo(String method, Object error, StackTrace stackTrace) {
58+
if (debug) {
59+
print('HighlightsPlugin $method error: $error');
60+
debugPrintStack(stackTrace: stackTrace);
61+
}
3162
}
3263
}

lib/highlights_plugin_method_channel.dart

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ import 'highlights_plugin_platform_interface.dart';
1313
/// An implementation of [HighlightsPluginPlatform] that uses method channels.
1414
class MethodChannelHighlightsPlugin extends HighlightsPluginPlatform
1515
implements HighlightsInterface {
16+
MethodChannelHighlightsPlugin({required this.debug});
17+
1618
/// The method channel used to interact with the native platform.
1719
@visibleForTesting
1820
final methodChannel = const MethodChannel('highlights_plugin');
1921

20-
List<String>? _languages;
21-
List<String>? _themes;
22+
final bool debug;
23+
24+
List<String>? _cachedLanguages;
25+
List<String>? _cachedThemes;
2226

2327
@override
2428
Future<List<CodeHighlight>> getHighlights(
@@ -44,7 +48,7 @@ class MethodChannelHighlightsPlugin extends HighlightsPluginPlatform
4448
if (data is List) {
4549
return data.map((e) => CodeHighlight.fromJson(e)).toList();
4650
} else {
47-
print(
51+
_debugPrint(
4852
'${index.getHighlights}: Expected List but got ${data.runtimeType}',
4953
);
5054
return [];
@@ -53,16 +57,16 @@ class MethodChannelHighlightsPlugin extends HighlightsPluginPlatform
5357

5458
@override
5559
Future<List<String>> getLanguages() async {
56-
if (_languages != null) return _languages!;
60+
if (_cachedLanguages != null) return _cachedLanguages!;
5761

5862
final result = await methodChannel.invokeMethod(index.getLanguages);
5963

6064
if (result is List) {
6165
final output = result.map((data) => data.toString()).toList();
62-
_languages = output;
66+
_cachedLanguages = output;
6367
return output;
6468
} else {
65-
print(
69+
_debugPrint(
6670
'${index.getLanguages}: Expected List but got ${result.runtimeType}',
6771
);
6872
return [];
@@ -71,31 +75,39 @@ class MethodChannelHighlightsPlugin extends HighlightsPluginPlatform
7175

7276
@override
7377
Future<List<String>> getThemes() async {
74-
if (_themes != null) return _themes!;
78+
if (_cachedThemes != null) return _cachedThemes!;
7579

7680
final result = await methodChannel.invokeMethod(index.getThemes);
7781

7882
if (result is List) {
7983
final output = result.map((e) => e.toString()).toList();
80-
_themes = output;
84+
_cachedThemes = output;
8185
return output;
8286
} else {
83-
print('${index.getThemes}: Expected List but got ${result.runtimeType}');
87+
_debugPrint(
88+
'${index.getThemes}: Expected List but got ${result.runtimeType}',
89+
);
8490
return [];
8591
}
8692
}
8793

8894
Future<String> _getLanguage(String expected) async {
89-
final data = _languages ?? (await getLanguages());
95+
final data = _cachedLanguages ?? (await getLanguages());
9096
final result = data.getMatching(expected);
9197
return result ?? data.first;
9298
}
9399

94100
Future<String> _getTheme(String expected) async {
95-
final data = _themes ?? (await getThemes());
101+
final data = _cachedThemes ?? (await getThemes());
96102
final result = data.getMatching(expected);
97103
return result ?? data.first;
98104
}
105+
106+
void _debugPrint(String message) {
107+
if (debug) {
108+
print(message);
109+
}
110+
}
99111
}
100112

101113
extension on List<String> {

lib/highlights_plugin_platform_interface.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ abstract class HighlightsPluginPlatform extends PlatformInterface
1212

1313
static final Object _token = Object();
1414

15-
static HighlightsPluginPlatform _instance = MethodChannelHighlightsPlugin();
15+
static bool debug = false;
16+
17+
static HighlightsPluginPlatform _instance =
18+
MethodChannelHighlightsPlugin(debug: debug);
1619

1720
/// The default instance of [HighlightsPluginPlatform] to use.
1821
///

0 commit comments

Comments
 (0)