Search code examples
aws-lambdamicronautgraalvm-native-image

Micronaut Error Body Not Showing on AWS Lambda


I build a Micronaut application with graalvm following the instructions found here. I configured a custom error handler that returns an HttpResponse when a validation error of request body is encountered on the controller.

@Factory
public class ApiExceptionHandler {
    
    @Produces
    @Singleton
    @Requires(classes = {CustomException.class, ExceptionHandler.class})
    public ExceptionHandler<CustomException, HttpResponse<ApiResponse>> handleCustomException() {
        return (req, ex) -> {
            log.error("exception=[{}], status=[{}], message=[{}]",
                    ex.getClass().getSimpleName(), HttpStatus.BAD_REQUEST.getCode(), ex.getMessage());

            ApiResponse error = new ApiResponse.Builder(HttpStatus.BAD_REQUEST.getCode(), HttpStatus.BAD_REQUEST.getReason())
                    .error(new ErrorInfo(ex.getErrorCode(), ex.getErrorCode().getErrorMessage(), null))
                    .build();

            return HttpResponse.badRequest().body(error);
        };
    }
}

When testing it on postman, the error is showing properly

{
    "timestamp": "2021-06-28T12:54:24.292",
    "status": 400,
    "description": "Bad Request",
    "error": {
        "code": "custom code",
        "message": "Validation Error"
    }
}

But when testing it on lambda using apigateway-aws-proxy, the error body is not displaying

{
  "statusCode": 400,
  "multiValueHeaders": {
    "Content-Type": [
      "application/json"
    ]
  },
  "body": "{}",
  "isBase64Encoded": false
}

How can I display the error body on aws lambda function?


Solution

  • Adding @Introspected on response body solved the issue.