Skip to content

[pigeon] Document how to return FlutterError in Swift #123851

@theodelrieu

Description

@theodelrieu

Hello,

I'm writing the Swift part of our application, and I need to return additional error details to the Dart part.
To that end, I wanted to use FlutterError, which has a details field.

However, the following code fails:

  completion(Result.failure(FlutterError(code: "someCode", message: "msg", details: 42)))

and I get this error:

Swift Compiler Error (Xcode): 'FlutterError' is not convertible to 'any Error'

I dug up a bit, and came across the wrapError function in the generated Swift code:

private func wrapError(_ error: Any) -> [Any?] {
  if let flutterError = error as? FlutterError {
    return [
      flutterError.code,
      flutterError.message,
      flutterError.details
    ]
  }
  return [
    "\(error)",
    "\(type(of: error))",
    "Stacktrace: \(Thread.callStackSymbols)"
  ]
}

So it seems that the only way to get additional info passed to Dart is to return a FlutterError, which doesn't seem to implement the Error protocol.

I can workaround that for the time being by returning an ad-hoc structure. From what I understand, the Objective-C generator supports FlutterError since a few versions, was there an oversight on the Swift one?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listd: api docsIssues with https://api.flutter.dev/p: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions