aws-lambda-dart-runtime
aws-lambda-dart-runtime copied to clipboard
Converting object to an encodable object failed for Instance of InvocationResult ApiGateway
I am trying to follow api gateway example in readme My code looks like
import 'package:aws_lambda_dart_runtime/aws_lambda_dart_runtime.dart';
void main() async {
print("hello from inside dart 1");
/// This demo's handling an API Gateway request.
final Handler<AwsApiGatewayEvent> helloApiGateway = (context, event) async {
print("hello from inside dart 2");
final response = {"message": "hello ${context.requestId}"};
print("hello from inside dart 3");
/// it returns an encoded response to the gateway
final resp = InvocationResult(
context.requestId, AwsApiGatewayResponse.fromJson(response));
print("hello from inside dart 4");
return resp;
};
/// The Runtime is a singleton. You can define the handlers as you wish.
Runtime()
..registerHandler<AwsApiGatewayEvent>("main.hello", helloApiGateway)
..invoke();
}
When endpoint invoked via curl, I get
{"message": "Internal server error"}
but I see all hello...
prints in the lambda cloudwatch logs and request end without any exception.
When enpoint is invoked using AWS console for apigateway test UI, I see
Received response. Status: 200, Integration latency: 24 ms
Thu May 27 23:15:24 UTC 2021 : Endpoint response headers: {Date=Thu, 27 May 2021 23:15:24 GMT, Content-Type=application/json, Content-Length=128, Connection=keep-alive, x-amzn-RequestId=d71971ef-c9f7-4999-a306-2cec2aee846d, X-Amz-Function-Error=Unhandled, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-60b0280c-20ab219fdb20cbc96a699b53;sampled=0}
Thu May 27 23:15:24 UTC 2021 : Endpoint response body before transformations: {"errorMessage":"Converting object to an encodable object failed: Instance of 'InvocationResult'","errorType":"InvocationError"}
Thu May 27 23:15:24 UTC 2021 : Lambda execution failed with status 200 due to customer function error: Converting object to an encodable object failed: Instance of 'InvocationResult'. Lambda request id: d71971ef-c9f7-4999-a306-2cec2aee846d
Thu May 27 23:15:24 UTC 2021 : Method completed with status: 502
I tried changing line
return resp;
to ut8.encode(json.encode(resp))
and a few other things. No luck.
I am not sure what to do. Any help.
I am deploying using serverless.
I had to set the runtime as provided.al2
since it would not do it with runtime as dart
@asequeira-os This repo is more up to date: https://github.com/katallaxie/aws-lambda-dart-runtime
I submitted a pull request that fixes this error
@asequeira-os
https://github.com/katallaxie/aws-lambda-dart-runtime/blob/c05e0013302676546e105c6967635d2f3a5fc9bf/example/lib/main.dart#L10
Handler
should return AwsApiGatewayResponse
instead of InvocationResult
.
import 'package:aws_lambda_dart_runtime/aws_lambda_dart_runtime.dart';
void main() async {
print("hello from inside dart 1");
/// This demo's handling an API Gateway request.
final Handler<AwsApiGatewayEvent> helloApiGateway = (context, event) async {
print("hello from inside dart 2");
final response = {"message": "hello ${context.requestId}"};
print("hello from inside dart 3");
/// it returns an encoded response to the gateway
final resp = AwsApiGatewayResponse.fromJson(response);
print("hello from inside dart 4");
return resp;
};
/// The Runtime is a singleton. You can define the handlers as you wish.
Runtime()
..registerHandler<AwsApiGatewayEvent>("main.hello", helloApiGateway)
..invoke();
}