Skip to content

Fixed issue where a 200 response is returned when server throws exception #272

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

Closed
wants to merge 2 commits into from

Conversation

mikaelq
Copy link

@mikaelq mikaelq commented Jul 30, 2019

Added handling of all exceptions in LambdaContainerHandler.proxyStream() to return an error rather than a 200.

Issue #, if available: #273

Description of changes:
If an exception is thrown that is not explicitly handled by the LambdaContainerHandler.proxyStream() method an empty 200 will be returned. I've only seen this behavior using the Jersey handler but I believe this will also occur in the other handlers. The Jersey handler returns empty 200s if HK2 is unable to resolve dependencies. If the service framework is unable to start I'd expect a 500 to be returned.

@sapessi
Copy link
Contributor

sapessi commented Jul 31, 2019

Thanks for the PR, @mikaelq. This does seem to be a bug. However, we should not just marshal all exceptions and return them. The correct way to implement this is to rely on the ExceptionHandler interface. Can you modify this pull request to follow a similar logic to the proxy() method. Also, we'll need unit tests to verify the behavior. I'll open an issue to track this.

update: I've updated the PR description to link to the new issue I created.

Copy link
Contributor

@sapessi sapessi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add an integration test to verify the behavior.

@@ -215,6 +215,9 @@ public void proxyStream(InputStream input, OutputStream output, Context context)
} catch (JsonMappingException e) {
log.error("Error while mapping object to RequestType class", e);
getObjectMapper().writeValue(output, exceptionHandler.handle(e));
} catch (Exception e) {
log.error("Error while proxying request.", e);
getObjectMapper().writeValue(output, exceptionHandler.handle(e));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment on the PR. We should rely on the ExceptionHandler object instead like we do in the proxy() method.

…ervletResponseWriter.failure() to throw if an error occured rather than return ultimately returning a 200
@mikaelq
Copy link
Author

mikaelq commented Jul 31, 2019

@sapessi thanks for the quick reply. Oops, I didn't see proxy() method, it handles all exceptions. My initial fix did not work but I found what the issue is.

The issue is the JerseyServletResponseWriter.failure() eats failures ultimately returning a 200. I modified it to throw an InternalServerError exception. I verified this fixes the issue via unit tests. I did not add integrations tests. I'll try to do this soon.

Copy link
Contributor

@sapessi sapessi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you!

throw new InternalServerErrorException(e);
}
log.error("failure", throwable);
throw new InternalServerErrorException("Jersey failed to process request", throwable);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering what will happen to the latch in this case. The behavior seems correct as far as the outcome (what we return to the client). I'll add a TODO list item to verify that we don't leak anything in this case. I'll take care of it before the next release

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tested that latch.await() is not called during unit tests but this might not be the case every time. Looking closer at the code it could cause a leak. I took a look at Jersey's ApplicationHandler.java they use a future and call .completeExceptionally(). Maybe the code should be refactored to use futures instead of a latch. Also I wonder how framework failures are handled by the other consumers of LambdaContainerHandler.java.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created issue #276 to track this for the 2.0 release of the framework.

@sapessi sapessi added this to the Release 1.4 milestone Aug 1, 2019
@sapessi
Copy link
Contributor

sapessi commented Aug 1, 2019

I'll merge this manually in the core branch where I'm working on other changes.

@sapessi
Copy link
Contributor

sapessi commented Aug 13, 2019

I've merged this manually in the core branch. Thanks for the fix @mikaelq!

@sapessi sapessi closed this Aug 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants