package com.cardpaymentsystem.cardplatform.config; import com.cardpaymentsystem.cardplatform.exceptions.CardPlatformException; import com.cardpaymentsystem.cardplatform.exceptions.CheckoutException; import com.cardpaymentsystem.cardplatform.exceptions.FraudDetectedException; import com.cardpaymentsystem.cardplatform.exceptions.JwtExpiryException; import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.JwtException; import org.hibernate.annotations.Check; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; @ControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class); @ExceptionHandler(CardPlatformException.class) public ResponseEntity<String> handleCardPlatformException(CardPlatformException e) { LOGGER.error("error occurred {}", e); return new ResponseEntity<>("Error: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } // Checked exception where Transaction rollback does not occur @ExceptionHandler(FraudDetectedException.class) public ResponseEntity<String> handleCardPlatformException(FraudDetectedException e) { LOGGER.error("error occurred {}", e); return new ResponseEntity<>("Error: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(JwtExpiryException.class) public ResponseEntity<String> handleJwt(JwtExpiryException e) { LOGGER.error("error occurred {}", e); return new ResponseEntity<>("Error: " + e.getMessage(), HttpStatus.UNAUTHORIZED); } // Checked exception where Transaction rollback does not occur so we can persist failed transactions @ExceptionHandler(CheckoutException.class) public ResponseEntity<String> handleCheckoutException(CheckoutException e){ LOGGER.error("error occurred {}", e); return new ResponseEntity<>("Error: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } }