Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jersey : AwsProxyHttpServletResponseWriter : NullPointerException for Status that is not present in Response.StatusType #497

Closed
anmolk6 opened this issue Dec 12, 2022 · 3 comments
Assignees
Labels

Comments

@anmolk6
Copy link
Contributor

anmolk6 commented Dec 12, 2022

To help us debug your issue fill in the basic information below using the options provided

Serverless Java Container version: 1.9

Implementations: Jersey

Framework version: jersey-media-json-jackson : 2.37

Frontend service: ALB

Deployment method: Console

Scenario

I am trying to return 422 statusCode in specific scenario using javax.ws.rs.core Response.status(422).entity(response).build();

Expected behavior

It should through exception with status 422

Actual behavior

502Bad Gateway
{
"message": "Gateway timeout"
}

Steps to reproduce

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createPet(final Pet newPet) {
    if (newPet.getName() == null || newPet.getBreed() == null) {
        return Response.status(422).entity(new Error("Invalid name or breed")).build();
    }
    Pet dbPet = newPet;
    dbPet.setId(UUID.randomUUID().toString());
    return Response.status(200).entity(dbPet).build();
}

Full log output

"java.lang.NullPointerException: null\n\tat com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletResponseWriter.writeResponse(AwsProxyHttpServletResponseWriter.java:77)\n\tat com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletResponseWriter.writeResponse(AwsProxyHttpServletResponseWriter.java:36)\n\tat com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:222)\n\tat com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:257)\n\tat com.telstra.digitalservices.lambda.Handler.handleRequest(Handler.java:42)\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)\n\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)\n\tat java.base/java.lang.reflect.Method.invoke(Unknown Source)\n\tat lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:375)\n\tat lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:899)\n\tat lambdainternal.AWSLambda.startRuntime(AWSLambda.java:268)\n\tat lambdainternal.AWSLambda.startRuntime(AWSLambda.java:206)\n\tat lambdainternal.AWSLambda.main(AWSLambda.java:200)\n"

Analysis

As per analysis it's failing while getting reason phrase on
https://github.com/awslabs/aws-serverless-java-container/blob/main/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletResponseWriter.java#L77

422 is not present by default in provided enum , so fromStatusCode(int statusCode) is returning null as per its implementation

public static Status fromStatusCode(int statusCode) {
for (Status s : values()) {
if (s.code == statusCode) {
return s;
}
}
return null;
}

And null.getReasonPhrase is causing this nullPointerException.

Could you please suggest ways to handle this?

@deki
Copy link
Collaborator

deki commented Dec 12, 2022

Thanks for the issue report. Let's fix that by adding a null check and omitting the reason phrase in this case.

anmolk6 added a commit to anmolk6/aws-serverless-java-container that referenced this issue Dec 12, 2022
Fix provided for: aws#497

This is to resolve NullPointerException when HttpStatusCode is not present in default enum Response.StatusType
@anmolk6
Copy link
Contributor Author

anmolk6 commented Dec 12, 2022

Fix in this PR is tested with ALB : #498

@deki deki self-assigned this Dec 13, 2022
@deki deki added the bug label Dec 13, 2022
deki pushed a commit to anmolk6/aws-serverless-java-container that referenced this issue Dec 14, 2022
Fix provided for: aws#497

This is to resolve NullPointerException when HttpStatusCode is not present in default enum Response.StatusType
deki pushed a commit that referenced this issue Dec 14, 2022
pdating AwsProxyHttpServletResponseWriter.java

Fix provided for #497 

This is to resolve NullPointerException when HttpStatusCode is not present in default enum Response.StatusType
@deki
Copy link
Collaborator

deki commented Dec 14, 2022

Released as part of version 1.9.1.

@deki deki closed this as completed Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants