Skip to content

Commit 617d2bd

Browse files
[pigeon] Make Kotlin FlutterError a runtime error (#11469)
Change the Kotlin generator's `FlutterError` to inherit from `RuntimeException`, rather than just `Runnable`, so that it behaves as an unchecked error when used from Java (as is the case for the Java version). Fixes flutter/flutter#184200 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent f52df41 commit 617d2bd

9 files changed

Lines changed: 51 additions & 8 deletions

File tree

packages/pigeon/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 26.3.4
2+
3+
* [kotlin] Updates generated error class to inherit from `RuntimeException`
4+
instead of `Throwable`, for better Java interoperability.
5+
16
## 26.3.3
27

38
* Updates `analyzer` dependency to support versions 10 through 12.

packages/pigeon/example/app/android/app/src/main/kotlin/dev/flutter/pigeon_example_app/Messages.g.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class FlutterError(
191191
val code: String,
192192
override val message: String? = null,
193193
val details: Any? = null
194-
) : Throwable()
194+
) : RuntimeException()
195195

196196
enum class Code(val raw: Int) {
197197
ONE(0),

packages/pigeon/lib/src/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'generator.dart';
1515
/// The current version of pigeon.
1616
///
1717
/// This must match the version in pubspec.yaml.
18-
const String pigeonVersion = '26.3.3';
18+
const String pigeonVersion = '26.3.4';
1919

2020
/// Default plugin package name.
2121
const String defaultPluginPackageName = 'dev.flutter.pigeon';

packages/pigeon/lib/src/kotlin/kotlin_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ if (wrapped == null) {
13501350
indent.writeln('override val message: String? = null,');
13511351
indent.writeln('val details: Any? = null');
13521352
}, addTrailingNewline: false);
1353-
indent.addln(' : Throwable()');
1353+
indent.addln(' : RuntimeException()');
13541354
}
13551355

13561356
void _writeCreateConnectionError(

packages/pigeon/platform_tests/test_plugin/android/src/main/kotlin/com/example/test_plugin/CoreTests.gen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class FlutterError(
194194
val code: String,
195195
override val message: String? = null,
196196
val details: Any? = null
197-
) : Throwable()
197+
) : RuntimeException()
198198

199199
enum class AnEnum(val raw: Int) {
200200
ONE(0),

packages/pigeon/platform_tests/test_plugin/android/src/main/kotlin/com/example/test_plugin/EventChannelTests.gen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class EventChannelTestsError(
172172
val code: String,
173173
override val message: String? = null,
174174
val details: Any? = null
175-
) : Throwable()
175+
) : RuntimeException()
176176

177177
enum class EventEnum(val raw: Int) {
178178
ONE(0),

packages/pigeon/platform_tests/test_plugin/android/src/main/kotlin/com/example/test_plugin/ProxyApiTests.gen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ProxyApiTestsError(
5050
val code: String,
5151
override val message: String? = null,
5252
val details: Any? = null
53-
) : Throwable()
53+
) : RuntimeException()
5454
/**
5555
* Maintains instances used to communicate with the corresponding objects in Dart.
5656
*

packages/pigeon/pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pigeon
22
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
33
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
5-
version: 26.3.3 # This must match the version in lib/src/generator_tools.dart
5+
version: 26.3.4 # This must match the version in lib/src/generator_tools.dart
66

77
environment:
88
sdk: ^3.9.0
@@ -27,4 +27,3 @@ topics:
2727
- interop
2828
- platform-channels
2929
- plugin-development
30-

packages/pigeon/test/kotlin_generator_test.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,45 @@ void main() {
19831983
expect(code, isNot(contains('FlutterError')));
19841984
});
19851985

1986+
test('error class inherits from RuntimeException', () {
1987+
final root = Root(
1988+
apis: <Api>[
1989+
AstHostApi(
1990+
name: 'Api',
1991+
methods: <Method>[
1992+
Method(
1993+
name: 'method',
1994+
location: ApiLocation.host,
1995+
returnType: const TypeDeclaration.voidDeclaration(),
1996+
parameters: <Parameter>[
1997+
Parameter(
1998+
name: 'field',
1999+
type: const TypeDeclaration(
2000+
baseName: 'int',
2001+
isNullable: true,
2002+
),
2003+
),
2004+
],
2005+
),
2006+
],
2007+
),
2008+
],
2009+
classes: <Class>[],
2010+
enums: <Enum>[],
2011+
);
2012+
final sink = StringBuffer();
2013+
const kotlinOptions = InternalKotlinOptions(kotlinOut: '');
2014+
const generator = KotlinGenerator();
2015+
generator.generate(
2016+
kotlinOptions,
2017+
root,
2018+
sink,
2019+
dartPackageName: DEFAULT_PACKAGE_NAME,
2020+
);
2021+
final code = sink.toString();
2022+
expect(code, contains(': RuntimeException()'));
2023+
});
2024+
19862025
test('do not generate duplicated entries in writeValue', () {
19872026
final root = Root(
19882027
apis: <Api>[

0 commit comments

Comments
 (0)