Skip to content

Add "error" method to the Result interface in Pigeon #83244

Description

@PoisonousJohn

Use case

I'm using Pigeon to generate type-safe interop with native platforms.
In the case of @async functions, it generates the following interface in Java

public interface Result<T> {
    void success(T var1);
}

It has no way to report an error.

My use case is to call some method on the Host, and in case of any error, I need to show an error popup on the Flutter side.

Proposal

I suggest adding error method to the interface

    public interface Result<T> {
        void success(T var1);
        void error();
    }

Activity

  1. added
    in triagePresently being triaged by the triage team
    p: pigeonrelated to pigeon messaging codegen tool
    c: proposalA detailed proposal for a change to Flutter
    c: new featureNothing broken; request for a new capability
    and removed
    in triagePresently being triaged by the triage team
    on May 24, 2021
  2. stuartmorgan-g commented on May 27, 2021

    @stuartmorgan-g
    Contributor
  3. added
    P2Important issues not at the top of the work list
    on May 27, 2021
  4. gaaclarke commented on May 28, 2021

    @gaaclarke
    Member

    Yea, here is the caller of the callback:

          {
            BasicMessageChannel<Object> channel =
                new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.Api2Host.calculate", new StandardMessageCodec());
            if (api != null) {
              channel.setMessageHandler((message, reply) -> {
                Map<String, Object> wrapped = new HashMap<>();
                try {
                  @SuppressWarnings("ConstantConditions")
                  Value input = Value.fromMap((Map<String, Object>)message);
                  api.calculate(input, result -> { wrapped.put("result", result.toMap()); reply.reply(wrapped); });
                }
                catch (Error | RuntimeException exception) {
                  wrapped.put("error", wrapError(exception));
                  reply.reply(wrapped);
                }
              });
            } else {
              channel.setMessageHandler(null);
            }
          }

    We should have a way to fill in wrapped.put("error", wrapError(exception)) in response to the callback.

    As a workaround for now you can include an error in your pigeon class:

    class QueryResult {
      String? error;
      String? result;
    }
    
    @HostApi()
    abstract class Api {
      @async QueryResult query();
    }
  5. ajmalk commented on Jul 17, 2021

    @ajmalk
    Contributor

    +1.

    We just ran into needing this as well. Would be nice to have.

  6. AhmedNourJamalElDin commented on Jul 17, 2021

    @AhmedNourJamalElDin

    +1

    yes, I think this is needed.

    Trying to have an object that represents an error doesn't work for me:

    class ErrorClass {
         final int code;
         final String message;
         
         ErrorClass({required this.code, required this.message});
    }
    
    class ResultType {
        final string someValue;
        
        ResultType({required this.someValue});
    }
    
    class Reply {
           final ErrorClass? error;
           final ResultType? result;
           
           Reply({this.error, this.result});
    }
    

    and returning something like error = null and result != null or vice versa from Java / Kotlin, throws a null reference exception stating that error is null but it should not be null when calling error.toMap() for example.

    Attempt to invoke virtual method 'java.util.Map x.y.z.ErrorClass.toMap()' on a null object reference
    
  7. cenumi commented on Sep 6, 2021

    @cenumi
    Contributor

    any news?

  8. gaaclarke commented on Sep 7, 2021

    @gaaclarke
    Member

    Fixed in flutter/packages#460, It took a while because I didn't want to implement it until the testing framework for the java generator was improved.

  9. github-actions commented on Sep 22, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  10. locked as resolved and limited conversation to collaborators on Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterp: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions