I'm using @ControllerAdvice in Spring Boot project to handle Stripe's Card Exception but every time when this exception is thrown the code to handle Exception works instead of the code to handle specific Card Exception.
this is what I am doing :
"This is the Api that throws he exception - CardException on entering wrong Card details"
@PostMapping("/charge")
public String chargePayment(HttpServletRequest request) throws CardException, AuthenticationException, InvalidRequestException, StripeException {
.......
}
"This is my Exeption handling class code"
@ControllerAdvice
public class StripeExceptionHandler {
// I want this to work when the above method/Api throws CardException.
@ExceptionHandler(CardException.class)
public String handleCardException(CardException cardException, Model model) {
model.addAttribute("exception","CardException : "+ cardException.toString());
model.addAttribute("redirectUrl", "contribute");
return "exceptionpage";
}
// But every time CardException is thrown this code works instead of the above specific code.
@ExceptionHandler(Exception.class)
public String handleGlobalExceptions(Exception exception, WebRequest webRequest, Model model) {
model.addAttribute("exception", exception.getMessage());
model.addAttribute("redirectUrl", webRequest.getDescription(false));
return "exceptionpage";
}
}