|
| 1 | +package it.eng.dome.billing.proxy.exception; |
| 2 | + |
| 3 | +import java.net.ConnectException; |
| 4 | +import java.net.SocketTimeoutException; |
| 5 | +import java.net.UnknownHostException; |
| 6 | + |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.core.Ordered; |
| 10 | +import org.springframework.core.annotation.Order; |
| 11 | +import org.springframework.http.HttpStatus; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.web.bind.annotation.ControllerAdvice; |
| 14 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 15 | +import org.springframework.web.client.HttpClientErrorException; |
| 16 | +import org.springframework.web.client.HttpServerErrorException; |
| 17 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
| 18 | +import it.eng.dome.brokerage.exception.ErrorResponse; |
| 19 | +import jakarta.servlet.http.HttpServletRequest; |
| 20 | + |
| 21 | +@Order(Ordered.HIGHEST_PRECEDENCE) |
| 22 | +@ControllerAdvice |
| 23 | +public class ControllerExceptionHandler extends ResponseEntityExceptionHandler { |
| 24 | + |
| 25 | + private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class); |
| 26 | + |
| 27 | + @ExceptionHandler(IllegalArgumentException.class) |
| 28 | + protected ResponseEntity<Object> handleIllegalArgumentException(HttpServletRequest request, IllegalArgumentException ex) { |
| 29 | + return buildResponseEntity(new ErrorResponse(request, HttpStatus.BAD_REQUEST, ex)); |
| 30 | + } |
| 31 | + |
| 32 | + @ExceptionHandler(IllegalStateException.class) |
| 33 | + protected ResponseEntity<Object> handleIllegalStateException(HttpServletRequest request, IllegalStateException ex) { |
| 34 | + return buildResponseEntity(new ErrorResponse(request,HttpStatus.BAD_REQUEST, ex)); |
| 35 | + } |
| 36 | + |
| 37 | + @ExceptionHandler(ConnectException.class) |
| 38 | + protected ResponseEntity<Object> handleConnectionException(HttpServletRequest request, ConnectException ex) { |
| 39 | + return buildResponseEntity(new ErrorResponse(request,HttpStatus.SERVICE_UNAVAILABLE, ex)); |
| 40 | + } |
| 41 | + |
| 42 | + @ExceptionHandler(UnknownHostException.class) |
| 43 | + protected ResponseEntity<Object> handleConnectionException(HttpServletRequest request, UnknownHostException ex) { |
| 44 | + return buildResponseEntity(new ErrorResponse(request,HttpStatus.SERVICE_UNAVAILABLE, ex)); |
| 45 | + } |
| 46 | + |
| 47 | + @ExceptionHandler(HttpClientErrorException.class) |
| 48 | + protected ResponseEntity<Object> handleBadRequest(HttpServletRequest request, HttpClientErrorException ex) { |
| 49 | + return buildResponseEntity(new ErrorResponse(request, HttpStatus.BAD_REQUEST, ex)); |
| 50 | + } |
| 51 | + |
| 52 | + @ExceptionHandler(HttpServerErrorException.class) |
| 53 | + protected ResponseEntity<Object> handleInternalServerError(HttpServletRequest request, HttpServerErrorException ex) { |
| 54 | + return buildResponseEntity(new ErrorResponse(request, HttpStatus.INTERNAL_SERVER_ERROR, ex)); |
| 55 | + } |
| 56 | + |
| 57 | + @ExceptionHandler(SocketTimeoutException.class) |
| 58 | + protected ResponseEntity<Object> handleSocketTimeoutError(HttpServletRequest request, SocketTimeoutException ex) { |
| 59 | + return buildResponseEntity(new ErrorResponse(request, HttpStatus.REQUEST_TIMEOUT, ex)); |
| 60 | + } |
| 61 | + |
| 62 | + @ExceptionHandler(it.eng.dome.tmforum.tmf620.v4.ApiException.class) |
| 63 | + protected ResponseEntity<Object> handleApiException(HttpServletRequest request, it.eng.dome.tmforum.tmf620.v4.ApiException ex) { |
| 64 | + return buildResponseEntity(new ErrorResponse(request, HttpStatus.SERVICE_UNAVAILABLE, ex)); |
| 65 | + } |
| 66 | + |
| 67 | + private ResponseEntity<Object> buildResponseEntity(ErrorResponse errorResponse) { |
| 68 | + logger.error("{} - {}", errorResponse.getStatus(), errorResponse.getMessage()); |
| 69 | + return new ResponseEntity<>(errorResponse, errorResponse.getStatus()); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments