Skip to content

Commit 08d5393

Browse files
authored
Merge pull request #28 from davidmartos96/custom_error_codes
Custom error codes on the plugin side
2 parents 5508a59 + c58fadc commit 08d5393

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

plugin_tutorial/go-plugin-example/complex/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func (p *Example) InitPlugin(messenger plugin.BinaryMessenger) error {
2121

2222
p.channel = plugin.NewMethodChannel(messenger, "instance.id/go/data", plugin.StandardMethodCodec{})
2323
p.channel.HandleFunc("getData", getRemotesFunc)
24+
p.channel.HandleFunc("getError", getErrorFunc)
2425
p.channel.CatchAllHandleFunc(p.catchAllTest)
2526

2627
return nil
@@ -64,3 +65,7 @@ func getRemotesFunc(arguments interface{}) (reply interface{}, err error) {
6465

6566
return sectionList, nil
6667
}
68+
69+
func getErrorFunc(arguments interface{}) (reply interface{}, err error) {
70+
return nil, plugin.NewError("customErrorCode", errors.New("Some error"))
71+
}

plugin_tutorial/lib/main_desktop.dart

+29-17
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,38 @@ void main() async {
2121
print(batteryLevel);
2222
});
2323

24-
// A more complicated plugin
25-
//
26-
test('Test StandardMethodCodec array of map', () async {
24+
group('Complex plugin', () {
2725
const platform_complex_structure =
2826
const MethodChannel('instance.id/go/data');
2927

30-
final List<dynamic> result = await platform_complex_structure.invokeMethod(
31-
'getData', "HelloFromDart"); // passing "HelloFromDart" as an argument
32-
expect(result, [
33-
{"instanceid": 1023, "pcbackup": "test", "brbackup": "test2"},
34-
{"instanceid": 1024, "pcbackup": "test", "brbackup": "test2"},
35-
{"instanceid": 1056, "pcbackup": "coucou", "brbackup": "coucou2"},
36-
{"instanceid": 3322, "pcbackup": "finaly", "brbackup": "finaly2"}
37-
]);
38-
39-
// golang can return the random methodName
40-
final String methodName = 'test/' + new Random().nextInt(100000).toString();
41-
final String resultPathPrefix =
42-
await platform_complex_structure.invokeMethod(methodName);
43-
expect(resultPathPrefix, methodName);
28+
// A more complicated plugin
29+
test('Test StandardMethodCodec array of map', () async {
30+
final List<dynamic> result =
31+
await platform_complex_structure.invokeMethod('getData',
32+
"HelloFromDart"); // passing "HelloFromDart" as an argument
33+
expect(result, [
34+
{"instanceid": 1023, "pcbackup": "test", "brbackup": "test2"},
35+
{"instanceid": 1024, "pcbackup": "test", "brbackup": "test2"},
36+
{"instanceid": 1056, "pcbackup": "coucou", "brbackup": "coucou2"},
37+
{"instanceid": 3322, "pcbackup": "finaly", "brbackup": "finaly2"}
38+
]);
39+
40+
// golang can return the random methodName
41+
final String methodName =
42+
'test/' + new Random().nextInt(100000).toString();
43+
final String resultPathPrefix =
44+
await platform_complex_structure.invokeMethod(methodName);
45+
expect(resultPathPrefix, methodName);
46+
});
47+
48+
test('Custom errors', () async {
49+
var matcher = predicate(
50+
(e) => e is PlatformException && e.code == "customErrorCode");
51+
expect(
52+
platform_complex_structure.invokeMethod('getError'),
53+
throwsA(matcher),
54+
);
55+
});
4456
});
4557

4658
tearDownAll(() async {

0 commit comments

Comments
 (0)