-
Notifications
You must be signed in to change notification settings - Fork 565
Migrate to java events #657
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
Open
mbfreder
wants to merge
8
commits into
aws:main
Choose a base branch
from
mbfreder:migrate-java-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
df70d56
Added java-events v4 dependency
mbfreder ebd5acd
migrate to APIGatewayV2HTTPEvent
mbfreder 6086216
Migrate to APIGatewayV2HTTPEvent in struts and spark
mbfreder abc09a8
migrated to APIGatewayProxyRequestEvent and ALB
mbfreder 905aa4b
Migrated to AwsProxyResponseEvent
mbfreder 5a0b1b3
updated samples
mbfreder a00d71f
remove helper class
mbfreder 239bc0a
Fix samples
mbfreder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...a-container-core/src/main/java/com/amazonaws/serverless/proxy/AwsAlbExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.amazonaws.serverless.proxy; | ||
|
||
import com.amazonaws.serverless.exceptions.InvalidRequestEventException; | ||
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler; | ||
import com.amazonaws.serverless.proxy.model.ErrorModel; | ||
import com.amazonaws.services.lambda.runtime.events.AwsProxyResponseEvent; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import jakarta.ws.rs.InternalServerErrorException; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import jakarta.ws.rs.core.MediaType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class AwsAlbExceptionHandler implements ExceptionHandler<AwsProxyResponseEvent>{ | ||
|
||
private Logger log = LoggerFactory.getLogger(AwsAlbExceptionHandler.class); | ||
|
||
//------------------------------------------------------------- | ||
// Constants | ||
//------------------------------------------------------------- | ||
|
||
static final String INTERNAL_SERVER_ERROR = "Internal Server Error"; | ||
static final String GATEWAY_TIMEOUT_ERROR = "Gateway timeout"; | ||
|
||
|
||
//------------------------------------------------------------- | ||
// Variables - Private - Static | ||
//------------------------------------------------------------- | ||
|
||
private static final Map<String, List<String>> headers = new HashMap<>(); | ||
|
||
//------------------------------------------------------------- | ||
// Constructors | ||
//------------------------------------------------------------- | ||
|
||
static { | ||
List<String> values = new ArrayList<>(); | ||
values.add(MediaType.APPLICATION_JSON); | ||
headers.put(HttpHeaders.CONTENT_TYPE, values); | ||
} | ||
@Override | ||
public AwsProxyResponseEvent handle(Throwable ex) { | ||
log.error("Called exception handler for:", ex); | ||
|
||
// adding a print stack trace in case we have no appender or we are running inside SAM local, where need the | ||
// output to go to the stderr. | ||
ex.printStackTrace(); | ||
AwsProxyResponseEvent responseEvent = new AwsProxyResponseEvent(); | ||
|
||
responseEvent.setMultiValueHeaders(headers); | ||
if (ex instanceof InvalidRequestEventException || ex instanceof InternalServerErrorException) { | ||
//return new APIGatewayProxyResponseEvent(500, headers, getErrorJson(INTERNAL_SERVER_ERROR)); | ||
responseEvent.setBody(getErrorJson(INTERNAL_SERVER_ERROR)); | ||
responseEvent.setStatusCode(500); | ||
return responseEvent; | ||
} else { | ||
responseEvent.setBody(getErrorJson(GATEWAY_TIMEOUT_ERROR)); | ||
responseEvent.setStatusCode(502); | ||
return responseEvent; | ||
} | ||
} | ||
|
||
@Override | ||
public void handle(Throwable ex, OutputStream stream) throws IOException { | ||
AwsProxyResponseEvent response = handle(ex); | ||
|
||
LambdaContainerHandler.getObjectMapper().writeValue(stream, response); | ||
} | ||
|
||
//------------------------------------------------------------- | ||
// Methods - Protected | ||
//------------------------------------------------------------- | ||
|
||
String getErrorJson(String message) { | ||
|
||
try { | ||
return LambdaContainerHandler.getObjectMapper().writeValueAsString(new ErrorModel(message)); | ||
} catch (JsonProcessingException e) { | ||
log.error("Could not produce error JSON", e); | ||
return "{ \"message\": \"" + message + "\" }"; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need that class? Can't we use
AwsProxyExceptionHandler
? Unittests ran fine without it.