| 1 |
package com.paymentlink.exception; |
| 2 |
|
| 3 |
import org.slf4j.Logger; |
| 4 |
import org.slf4j.LoggerFactory; |
| 5 |
import org.springframework.http.HttpStatus; |
| 6 |
import org.springframework.web.bind.annotation.ControllerAdvice; |
| 7 |
import org.springframework.web.bind.annotation.ExceptionHandler; |
| 8 |
import org.springframework.web.bind.annotation.ResponseStatus; |
| 9 |
import org.springframework.web.servlet.ModelAndView; |
| 10 |
|
| 11 |
@ControllerAdvice |
| 12 |
public class GlobalExceptionHandler { |
| 13 |
|
| 14 |
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); |
| 15 |
|
| 16 |
@ExceptionHandler(RegionBlockedException.class) |
| 17 |
@ResponseStatus(HttpStatus.FORBIDDEN) |
| 18 |
public ModelAndView handleRegionBlocked(RegionBlockedException ex) { |
| 19 |
logger.warn("Region blocked: {} ({})", ex.getCountryName(), ex.getCountryCode()); |
| 20 |
|
| 21 |
ModelAndView mav = new ModelAndView("region-blocked"); |
| 22 |
mav.addObject("countryCode", ex.getCountryCode()); |
| 23 |
mav.addObject("countryName", ex.getCountryName()); |
| 24 |
mav.addObject("message", "Service is not available in your region"); |
| 25 |
|
| 26 |
return mav; |
| 27 |
} |
| 28 |
|
| 29 |
@ExceptionHandler(Exception.class) |
| 30 |
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) |
| 31 |
public ModelAndView handleGenericException(Exception ex) { |
| 32 |
logger.error("Unexpected error", ex); |
| 33 |
|
| 34 |
ModelAndView mav = new ModelAndView("error"); |
| 35 |
mav.addObject("message", "An unexpected error occurred"); |
| 36 |
mav.addObject("error", ex.getMessage()); |
| 37 |
|
| 38 |
return mav; |
| 39 |
} |
| 40 |
} |
| 41 |
|