|
| 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 | +} |
0 commit comments