-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #691 from awslabs/690-exception-handler
handle Spring's ErrorResponse in separate default ExceptionHandlers
- Loading branch information
Showing
8 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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
24 changes: 24 additions & 0 deletions
24
...g/src/main/java/com/amazonaws/serverless/proxy/spring/SpringAwsProxyExceptionHandler.java
This file contains 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,24 @@ | ||
package com.amazonaws.serverless.proxy.spring; | ||
|
||
import com.amazonaws.serverless.proxy.AwsProxyExceptionHandler; | ||
import com.amazonaws.serverless.proxy.ExceptionHandler; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyResponse; | ||
import org.springframework.web.ErrorResponse; | ||
|
||
/** | ||
* This ExceptionHandler implementation enhances the standard AwsProxyExceptionHandler | ||
* by mapping additional details from org.springframework.web.ErrorResponse | ||
*/ | ||
public class SpringAwsProxyExceptionHandler extends AwsProxyExceptionHandler | ||
implements ExceptionHandler<AwsProxyResponse> { | ||
@Override | ||
public AwsProxyResponse handle(Throwable ex) { | ||
if (ex instanceof ErrorResponse) { | ||
return new AwsProxyResponse(((ErrorResponse) ex).getStatusCode().value(), | ||
HEADERS, getErrorJson(ex.getMessage())); | ||
} else { | ||
return super.handle(ex); | ||
} | ||
} | ||
|
||
} |
This file contains 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
21 changes: 21 additions & 0 deletions
21
...c/test/java/com/amazonaws/serverless/proxy/spring/SpringAwsProxyExceptionHandlerTest.java
This file contains 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,21 @@ | ||
package com.amazonaws.serverless.proxy.spring; | ||
|
||
import com.amazonaws.serverless.proxy.model.AwsProxyResponse; | ||
import jakarta.ws.rs.core.Response; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.web.servlet.NoHandlerFoundException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class SpringAwsProxyExceptionHandlerTest { | ||
|
||
@Test | ||
void noHandlerFoundExceptionResultsIn404() { | ||
AwsProxyResponse response = new SpringAwsProxyExceptionHandler(). | ||
handle(new NoHandlerFoundException(HttpMethod.GET.name(), "https://atesturl", | ||
HttpHeaders.EMPTY)); | ||
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...c/main/java/com/amazonaws/serverless/proxy/spring/SpringBootAwsProxyExceptionHandler.java
This file contains 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,27 @@ | ||
package com.amazonaws.serverless.proxy.spring; | ||
|
||
import com.amazonaws.serverless.proxy.AwsProxyExceptionHandler; | ||
import com.amazonaws.serverless.proxy.ExceptionHandler; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyResponse; | ||
import org.springframework.web.ErrorResponse; | ||
|
||
/** | ||
* This ExceptionHandler implementation enhances the standard AwsProxyExceptionHandler | ||
* by mapping additional details from org.springframework.web.ErrorResponse | ||
* | ||
* As of now this class is identical with SpringAwsProxyExceptionHandler. We may consider | ||
* moving it to a common module to share it in the future. | ||
*/ | ||
public class SpringBootAwsProxyExceptionHandler extends AwsProxyExceptionHandler | ||
implements ExceptionHandler<AwsProxyResponse> { | ||
@Override | ||
public AwsProxyResponse handle(Throwable ex) { | ||
if (ex instanceof ErrorResponse) { | ||
return new AwsProxyResponse(((ErrorResponse) ex).getStatusCode().value(), | ||
HEADERS, getErrorJson(ex.getMessage())); | ||
} else { | ||
return super.handle(ex); | ||
} | ||
} | ||
|
||
} |
This file contains 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