Skip to content

Commit ea04b59

Browse files
committed
Added Exception handling
Signed-off-by: muhamadto <muhamadto@gmail.com>
1 parent a7285cf commit ea04b59

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ $ ./mvnw -ntp clean verify -U --settings ./settings-spring.xml
4949
```
5050
3. Make a call
5151
```shell
52-
$ curl --location --request POST 'http://localhost:8080/exampleFunction' \
52+
$ curl --location --request POST 'https://ffwtdrvhmg.execute-api.ap-southeast-2.amazonaws.com/dev' \
5353
--header 'Content-Type: application/json' \
5454
--data-raw '{
5555
"name": "CoffeeBeans"
@@ -250,6 +250,7 @@ and the following trust relationship
250250
"lambda:RemovePermission",
251251
"lambda:PutFunctionEventInvokeConfig",
252252
"lambda:DeleteFunctionEventInvokeConfig",
253+
"lambda:UpdateFunctionEventInvokeConfig",
253254
"lambda:UpdateFunctionCode",
254255
"lambda:ListTags",
255256
"lambda:UpdateFunctionConfiguration"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.coffeebeans.springnativeawslambda.function;
2+
3+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
4+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import java.util.Map;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.bind.annotation.RestControllerAdvice;
10+
11+
@Slf4j
12+
@RestControllerAdvice
13+
public class LambdaExceptionHandler {
14+
15+
@ExceptionHandler(ClassCastException.class)
16+
public APIGatewayProxyResponseEvent handleException(
17+
final ClassCastException e,
18+
final APIGatewayProxyRequestEvent request) {
19+
log.error("Error processing request: {}", e.getMessage());
20+
return new APIGatewayProxyResponseEvent()
21+
.withStatusCode(500)
22+
.withHeaders(Map.of("X-Requested-Id", request.getRequestContext().getRequestId()))
23+
.withBody("{\n"
24+
+ " \"message\": \"Internal Server Error\",\n"
25+
+ " \"errorCode\": \"SERVER_ERROR\n"
26+
+ " }");
27+
}
28+
29+
@ExceptionHandler(JsonProcessingException.class)
30+
public APIGatewayProxyResponseEvent handleException(
31+
final JsonProcessingException e,
32+
final APIGatewayProxyRequestEvent request) {
33+
log.error("Error processing request: {}", e.getMessage());
34+
return new APIGatewayProxyResponseEvent()
35+
.withStatusCode(500)
36+
.withHeaders(Map.of("X-Requested-Id", request.getRequestContext().getRequestId()))
37+
.withBody("{\n"
38+
+ " \"message\": \"Internal Server Error\",\n"
39+
+ " \"errorCode\": \"SERVER_ERROR\n"
40+
+ " }");
41+
}
42+
}

spring-native-aws-lambda-function/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
spring:
22
main:
33
banner-mode: off
4-
web-application-type: servlet # change to 'servlet' for development
4+
web-application-type: none # change to 'servlet' for development
55
cloud:
66
function:
77
web:

0 commit comments

Comments
 (0)