Skip to content

Commit 80d07ed

Browse files
authored
[pigeon] Swift host error handling (#3084)
* temp * sets up use of wrapError * remove swift from skip list, update wrapError func * changelog * macos tests * Stacktrace label * nits * async wont try, do, throw, or catch * Async error handling * throwAsyncError all but kotlin + c++ * Revert "Async error handling" This reverts commit a07190c. * gen * nit
1 parent ad8e4e6 commit 80d07ed

File tree

26 files changed

+407
-208
lines changed

26 files changed

+407
-208
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 7.1.2
2+
3+
* [swift] Adds error handling to sync host api methods.
4+
15
## 7.1.1
26

37
* [c++] Fixes handling of the `cpp*` options in `@ConfigurePigeon` annotations.

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'ast.dart';
1111
/// The current version of pigeon.
1212
///
1313
/// This must match the version in pubspec.yaml.
14-
const String pigeonVersion = '7.1.1';
14+
const String pigeonVersion = '7.1.2';
1515

1616
/// Read all the content from [stdin] to a String.
1717
String readStdin() {

packages/pigeon/lib/swift_generator.dart

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,11 @@ import FlutterMacOS
404404
argSignature.add('completion: @escaping ($returnType) -> Void');
405405
indent.writeln('func ${components.name}(${argSignature.join(', ')})');
406406
} else if (method.returnType.isVoid) {
407-
indent.writeln('func ${components.name}(${argSignature.join(', ')})');
407+
indent.writeln(
408+
'func ${components.name}(${argSignature.join(', ')}) throws');
408409
} else {
409410
indent.writeln(
410-
'func ${components.name}(${argSignature.join(', ')}) -> $returnType');
411+
'func ${components.name}(${argSignature.join(', ')}) throws -> $returnType');
411412
}
412413
}
413414
});
@@ -465,8 +466,9 @@ import FlutterMacOS
465466
}
466467
});
467468
}
469+
final String tryStatement = method.isAsynchronous ? '' : 'try ';
468470
final String call =
469-
'api.${components.name}(${methodArgument.join(', ')})';
471+
'${tryStatement}api.${components.name}(${methodArgument.join(', ')})';
470472
if (method.isAsynchronous) {
471473
indent.write('$call ');
472474
if (method.returnType.isVoid) {
@@ -479,13 +481,19 @@ import FlutterMacOS
479481
});
480482
}
481483
} else {
482-
if (method.returnType.isVoid) {
483-
indent.writeln(call);
484-
indent.writeln('reply(wrapResult(nil))');
485-
} else {
486-
indent.writeln('let result = $call');
487-
indent.writeln('reply(wrapResult(result))');
488-
}
484+
indent.write('do ');
485+
indent.addScoped('{', '}', () {
486+
if (method.returnType.isVoid) {
487+
indent.writeln(call);
488+
indent.writeln('reply(wrapResult(nil))');
489+
} else {
490+
indent.writeln('let result = $call');
491+
indent.writeln('reply(wrapResult(result))');
492+
}
493+
}, addTrailingNewline: false);
494+
indent.addScoped(' catch {', '}', () {
495+
indent.writeln('reply(wrapError(error))');
496+
});
489497
}
490498
});
491499
}, addTrailingNewline: false);
@@ -595,13 +603,22 @@ import FlutterMacOS
595603

596604
void _writeWrapError(Indent indent) {
597605
indent.newln();
598-
indent.write('private func wrapError(_ error: FlutterError) -> [Any?] ');
606+
indent.write('private func wrapError(_ error: Any) -> [Any?] ');
599607
indent.addScoped('{', '}', () {
608+
indent.write('if let flutterError = error as? FlutterError ');
609+
indent.addScoped('{', '}', () {
610+
indent.write('return ');
611+
indent.addScoped('[', ']', () {
612+
indent.writeln('flutterError.code,');
613+
indent.writeln('flutterError.message,');
614+
indent.writeln('flutterError.details');
615+
});
616+
});
600617
indent.write('return ');
601618
indent.addScoped('[', ']', () {
602-
indent.writeln('error.code,');
603-
indent.writeln('error.message,');
604-
indent.writeln('error.details');
619+
indent.writeln(r'"\(error)",');
620+
indent.writeln(r'"\(type(of: error))",');
621+
indent.writeln(r'"Stacktrace: \(Thread.callStackSymbols)"');
605622
});
606623
});
607624
}

packages/pigeon/mock_handler_tester/test/message.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v7.0.5), do not edit directly.
5+
// Autogenerated from Pigeon (v7.1.2), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
88

packages/pigeon/mock_handler_tester/test/test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v7.0.5), do not edit directly.
5+
// Autogenerated from Pigeon (v7.1.2), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
88
// ignore_for_file: avoid_relative_lib_imports

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/main/java/com/example/alternate_language_test_plugin/CoreTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v7.1.1), do not edit directly.
5+
// Autogenerated from Pigeon (v7.1.2), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77

88
package com.example.alternate_language_test_plugin;

packages/pigeon/platform_tests/alternate_language_test_plugin/ios/Classes/CoreTests.gen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v7.1.1), do not edit directly.
5+
// Autogenerated from Pigeon (v7.1.2), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77

88
#import <Foundation/Foundation.h>

packages/pigeon/platform_tests/alternate_language_test_plugin/ios/Classes/CoreTests.gen.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v7.1.1), do not edit directly.
5+
// Autogenerated from Pigeon (v7.1.2), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77

88
#import "CoreTests.gen.h"

packages/pigeon/platform_tests/flutter_null_safe_unit_tests/lib/core_tests.gen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v7.0.5), do not edit directly.
5+
// Autogenerated from Pigeon (v7.1.2), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
88

packages/pigeon/platform_tests/flutter_null_safe_unit_tests/lib/flutter_unittests.gen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v7.0.5), do not edit directly.
5+
// Autogenerated from Pigeon (v7.1.2), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
88

0 commit comments

Comments
 (0)