Skip to content

Custom error codes on the plugin side #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugin_tutorial/go-plugin-example/complex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (p *Example) InitPlugin(messenger plugin.BinaryMessenger) error {

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

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

return sectionList, nil
}

func getErrorFunc(arguments interface{}) (reply interface{}, err error) {
return nil, plugin.NewError("customErrorCode", errors.New("Some error"))
}
46 changes: 29 additions & 17 deletions plugin_tutorial/lib/main_desktop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,38 @@ void main() async {
print(batteryLevel);
});

// A more complicated plugin
//
test('Test StandardMethodCodec array of map', () async {
group('Complex plugin', () {
const platform_complex_structure =
const MethodChannel('instance.id/go/data');

final List<dynamic> result = await platform_complex_structure.invokeMethod(
'getData', "HelloFromDart"); // passing "HelloFromDart" as an argument
expect(result, [
{"instanceid": 1023, "pcbackup": "test", "brbackup": "test2"},
{"instanceid": 1024, "pcbackup": "test", "brbackup": "test2"},
{"instanceid": 1056, "pcbackup": "coucou", "brbackup": "coucou2"},
{"instanceid": 3322, "pcbackup": "finaly", "brbackup": "finaly2"}
]);

// golang can return the random methodName
final String methodName = 'test/' + new Random().nextInt(100000).toString();
final String resultPathPrefix =
await platform_complex_structure.invokeMethod(methodName);
expect(resultPathPrefix, methodName);
// A more complicated plugin
test('Test StandardMethodCodec array of map', () async {
final List<dynamic> result =
await platform_complex_structure.invokeMethod('getData',
"HelloFromDart"); // passing "HelloFromDart" as an argument
expect(result, [
{"instanceid": 1023, "pcbackup": "test", "brbackup": "test2"},
{"instanceid": 1024, "pcbackup": "test", "brbackup": "test2"},
{"instanceid": 1056, "pcbackup": "coucou", "brbackup": "coucou2"},
{"instanceid": 3322, "pcbackup": "finaly", "brbackup": "finaly2"}
]);

// golang can return the random methodName
final String methodName =
'test/' + new Random().nextInt(100000).toString();
final String resultPathPrefix =
await platform_complex_structure.invokeMethod(methodName);
expect(resultPathPrefix, methodName);
});

test('Custom errors', () async {
var matcher = predicate(
(e) => e is PlatformException && e.code == "customErrorCode");
expect(
platform_complex_structure.invokeMethod('getError'),
throwsA(matcher),
);
});
});

tearDownAll(() async {
Expand Down