Skip to content

"Not compatible with a supported function shape" when using RequestContext parameterΒ #464

@jimmyff

Description

@jimmyff

Hey, I've recently updated this package (0.4.3+1) and I'm having difficulty getting my async pub sub endpoints working again.

The issue seems to be with using async modifier, giving me Not compatible with a supported function shape error, this is my function decleration:

@CloudFunction()
Future<void> function(PubSub pubSub, RequestContext context) async {

Where as this works, but then I can't use all the async conveniences.

@CloudFunction()
FutureOr<void> function(PubSub pubSub, RequestContext context) {

I'm sure I am missing something simple here? πŸ˜… (apologies in advance...)

Error

[SEVERE] functions_framework_builder:function_framework_builder on lib/functions.dart:

Not compatible with a supported function shape:
  HandlerWithLogger [FutureOr<Response> Function(Request, RequestLogger)] from package:functions_framework/functions_framework.dart
  Handler [FutureOr<Response> Function(Request)] from package:shelf/shelf.dart
  CloudEventWithContextHandler [FutureOr<void> Function(CloudEvent, RequestContext)] from package:functions_framework/functions_framework.dart
  CloudEventHandler [FutureOr<void> Function(CloudEvent)] from package:functions_framework/functions_framework.dart
  JsonHandler [FutureOr<ResponseType> Function(RequestType request, RequestContext context)] from package:functions_framework/functions_framework.dart
  JsonHandler [FutureOr<ResponseType> Function(RequestType request)] from package:functions_framework/functions_framework.dart

package:test_pubsub/functions.dart:10:14
   |
   |  Future<void> function(PubSub pubSub, RequestContext context) async {
   |             ^^^^^^^^
   |
[INFO] Running build completed, took 21.1s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 165ms

[SEVERE] Failed after 21.3s

bin/server.dart
import 'package:functions_framework/serve.dart';
import 'package:test_pubsub/functions.dart' as function_library;

Future<void> main(List<String> args) async {
  await serve(args, _nameToFunctionTarget);
}

FunctionTarget? _nameToFunctionTarget(String name) => switch (name) {
      'function' => JsonWithContextFunctionTarget.voidResult(
          function_library.function,
          (json) {
            if (json is Map<String, dynamic>) {
              try {
                return function_library.PubSub.fromJson(json);
              } catch (e, stack) {
                throw BadRequestException(
                  400,
                  'There was an error parsing the provided JSON data.',
                  innerError: e,
                  innerStack: stack,
                );
              }
            }
            throw BadRequestException(
              400,
              'The provided JSON is not the expected type '
              '`Map<String, dynamic>`.',
            );
          },
        ),
      _ => null
    };
lib/functions.dart
@CloudFunction()
Future<void> function(PubSub pubSub, RequestContext context) async {
  await Future.delayed(Duration(milliseconds: 1));
  context.logger.info('[Pub Sub] subscription: ${pubSub.subscription}');
  context.logger.info('[Pub Sub] message: ${pubSub.message.dataDecoded()}');
  context.responseHeaders['subscription'] = pubSub.subscription;
}

Thanks

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions