Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c510bd6
[pigeon] Do not use FlutterError when generating Swift code
hpcnt-daniel-l Apr 25, 2024
d8968d7
[pigeon] Add option to whether emit PigeonError class in Swift code
hpcnt-daniel-l Apr 25, 2024
c2beea1
[pigeon] Simplify localizedDescription of PigeonError in Swift
hpcnt-daniel-l Apr 25, 2024
6a8a94c
[pigeon] Always make sure CoreTests.gen.swift has PigeonError class
hpcnt-daniel-l Apr 25, 2024
b874f09
[pigeon] Make PigeonError class final and add FlutterError initializer
hpcnt-daniel-l Apr 25, 2024
df102e3
[pigeon] Rename swiftEmitErrorClass to emitPigeonErrorClass
hpcnt-daniel-l Apr 25, 2024
b30a0cc
[pigeon] Make wrapError in swift also handle PigeonError
hpcnt-daniel-l Apr 26, 2024
30ac050
[pigeon] Emit Error class for Swift with different names per file
hpcnt-daniel-l Apr 26, 2024
92c97af
[pigeon] Restore docregion for swift-class
hpcnt-daniel-l Apr 26, 2024
86d9168
[pigeon] Add CHANGELOG.md entry
hpcnt-daniel-l Apr 26, 2024
20ac87a
[pigeon] Apply format fix
hpcnt-daniel-l Apr 26, 2024
800ba60
[pigeon] Generate Swift error on CoreTests.gen.swift
hpcnt-daniel-l Apr 26, 2024
8fa6b75
[pigeon] Fix unittests for swift generator
hpcnt-daniel-l Apr 26, 2024
4bd3dbc
[pigeon] Run update-excerpts
hpcnt-daniel-l Apr 26, 2024
fae49ea
[pigeon] Bump version correctly
hpcnt-daniel-l Apr 26, 2024
b386a96
Merge commit 'd670b2c38c8db0a773aa703e7d3f15682e05ad7f' into feature/…
hpcnt-daniel-l May 8, 2024
78c3bb6
Remove conversion from FlutterError to PigeonError in Swift
hpcnt-daniel-l May 9, 2024
f13a06a
Update the comment for the PigeonError class in Swift
hpcnt-daniel-l May 9, 2024
63df915
Update the documentation for using PigeonError in Swift
hpcnt-daniel-l May 9, 2024
428736d
Replace several usage of Indent.nest with Indent.writeScoped
hpcnt-daniel-l May 10, 2024
88cd27d
Update Changelog and README of pigeon package
hpcnt-daniel-l May 10, 2024
a78cae7
Addressing review comments
hpcnt-daniel-l May 10, 2024
4ed7852
Relocate PigeonError class in Swift files
hpcnt-daniel-l May 14, 2024
a8ab4f8
Merge branch 'main' into feature/pigeon-swift-error
tarrinneal May 14, 2024
877015d
Merge branch 'main' into feature/pigeon-swift-error
bc-lee May 16, 2024
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
Prev Previous commit
Next Next commit
Update the documentation for using PigeonError in Swift
And replace existing usage of FlutterError.
  • Loading branch information
hpcnt-daniel-l committed May 9, 2024
commit 63df9154bf047a7ea3a7d26ffd5b8a567433583e
2 changes: 1 addition & 1 deletion packages/pigeon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ should be returned via the provided callback.
To pass custom details into `PlatformException` for error handling,
use `FlutterError` in your Host API. [Example](./example/README.md#HostApi_Example).

For swift, use `PigeonError` instead of `FlutterError`.
For swift, use `PigeonError` instead of `FlutterError` when throwing an error. See [Example#Swift](./example/README.md#Swift) for more details.

#### Objective-C and C++

Expand Down
7 changes: 4 additions & 3 deletions packages/pigeon/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ Future<bool> sendMessage(String messageText) {
### Swift

This is the code that will use the generated Swift code to receive calls from Flutter.
packages/pigeon/example/app/ios/Runner/AppDelegate.swift
Unlike other languages, when throwing an error (both synchronous and asynchronous), use `PigeonError` instead of `FlutterError`, as `FlutterError` is not conforming to `Swift.Error`.
Previously, a workaround was to declare an extension to `FlutterError` to conform to `Swift.Error`, but, as of Pigeon 19.0.0, `PigeonError` is provided for this purpose. Older code still using `FlutterError` will continue to work, but it is recommended to switch to `PigeonError` and remove the extension.
Copy link
Contributor

@tarrinneal tarrinneal May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike other languages, when throwing an error, use `PigeonError` instead of `FlutterError`, as `FlutterError` does not conform to `Swift.Error`.

I don't think the second line is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

<?code-excerpt "ios/Runner/AppDelegate.swift (swift-class)"?>
```swift
private class PigeonApiImplementation: ExampleHostApi {
Expand All @@ -125,14 +126,14 @@ private class PigeonApiImplementation: ExampleHostApi {

func add(_ a: Int64, to b: Int64) throws -> Int64 {
if a < 0 || b < 0 {
throw FlutterError(code: "code", message: "message", details: "details")
throw PigeonError(code: "code", message: "message", details: "details")
}
return a + b
}

func sendMessage(message: MessageData, completion: @escaping (Result<Bool, Error>) -> Void) {
if message.code == Code.one {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
completion(.failure(PigeonError(code: "code", message: "message", details: "details")))
return
}
completion(.success(true))
Expand Down
4 changes: 2 additions & 2 deletions packages/pigeon/example/app/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ private class PigeonApiImplementation: ExampleHostApi {

func add(_ a: Int64, to b: Int64) throws -> Int64 {
if a < 0 || b < 0 {
throw FlutterError(code: "code", message: "message", details: "details")
throw PigeonError(code: "code", message: "message", details: "details")
}
return a + b
}

func sendMessage(message: MessageData, completion: @escaping (Result<Bool, Error>) -> Void) {
if message.code == Code.one {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
completion(.failure(PigeonError(code: "code", message: "message", details: "details")))
return
}
completion(.success(true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import Flutter
import UIKit

// If someone want to throw FlutterError from Swift, they need to conform to Error protocol.
// However this is not recommended as this extension will be public visibility, as
// both FlutterError and Error are public.
extension FlutterError: Error {}

/// This plugin handles the native side of the integration tests in
/// example/integration_test/.
public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
Expand Down Expand Up @@ -53,15 +48,15 @@ public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
}

func throwError() throws -> Any? {
throw FlutterError(code: "code", message: "message", details: "details")
throw PigeonError(code: "code", message: "message", details: "details")
}

func throwErrorFromVoid() throws {
throw FlutterError(code: "code", message: "message", details: "details")
throw PigeonError(code: "code", message: "message", details: "details")
}

func throwFlutterError() throws -> Any? {
throw FlutterError(code: "code", message: "message", details: "details")
throw PigeonError(code: "code", message: "message", details: "details")
}

func echo(_ anInt: Int64) -> Int64 {
Expand Down Expand Up @@ -189,15 +184,15 @@ public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
}

func throwAsyncError(completion: @escaping (Result<Any?, Error>) -> Void) {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
completion(.failure(PigeonError(code: "code", message: "message", details: "details")))
}

func throwAsyncErrorFromVoid(completion: @escaping (Result<Void, Error>) -> Void) {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
completion(.failure(PigeonError(code: "code", message: "message", details: "details")))
}

func throwAsyncFlutterError(completion: @escaping (Result<Any?, Error>) -> Void) {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
completion(.failure(PigeonError(code: "code", message: "message", details: "details")))
}

func echoAsync(_ everything: AllTypes, completion: @escaping (Result<AllTypes, Error>) -> Void) {
Expand Down Expand Up @@ -626,7 +621,7 @@ public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
} else {
completion(
.failure(
FlutterError(
PigeonError(
code: "",
message: "Multi-instance responses were not matching: \(resOne), \(resTwo)",
details: nil)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import Cocoa
import FlutterMacOS

// If someone want to throw FlutterError from Swift, they need to conform to Error protocol.
// However this is not recommended as this extension will be public visibility, as
// both FlutterError and Error are public.
extension FlutterError: Error {}

/// This plugin handles the native side of the integration tests in
/// example/integration_test/.
public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
Expand Down Expand Up @@ -52,15 +47,15 @@ public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
}

func throwError() throws -> Any? {
throw FlutterError(code: "code", message: "message", details: "details")
throw PigeonError(code: "code", message: "message", details: "details")
}

func throwErrorFromVoid() throws {
throw FlutterError(code: "code", message: "message", details: "details")
throw PigeonError(code: "code", message: "message", details: "details")
}

func throwFlutterError() throws -> Any? {
throw FlutterError(code: "code", message: "message", details: "details")
throw PigeonError(code: "code", message: "message", details: "details")
}

func echo(_ anInt: Int64) -> Int64 {
Expand Down Expand Up @@ -188,15 +183,15 @@ public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
}

func throwAsyncError(completion: @escaping (Result<Any?, Error>) -> Void) {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
completion(.failure(PigeonError(code: "code", message: "message", details: "details")))
}

func throwAsyncErrorFromVoid(completion: @escaping (Result<Void, Error>) -> Void) {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
completion(.failure(PigeonError(code: "code", message: "message", details: "details")))
}

func throwAsyncFlutterError(completion: @escaping (Result<Any?, Error>) -> Void) {
completion(.failure(FlutterError(code: "code", message: "message", details: "details")))
completion(.failure(PigeonError(code: "code", message: "message", details: "details")))
}

func echoAsync(_ everything: AllTypes, completion: @escaping (Result<AllTypes, Error>) -> Void) {
Expand Down Expand Up @@ -625,7 +620,7 @@ public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi {
} else {
completion(
.failure(
FlutterError(
PigeonError(
code: "",
message: "Multi-instance responses were not matching: \(resOne), \(resTwo)",
details: nil)))
Expand Down