Untitled
unknown
plain_text
2 months ago
5.9 kB
1
Indexable
Never
package com.kotak.collection.audit.exception.handler; import com.kotak.collection.audit.dto.CommonErrorResponse; import com.kotak.collection.audit.dto.CommonErrorResponseDto; import com.kotak.collection.audit.exception.IllegalValueException; import com.kotak.collection.audit.exception.RecordsNotFoundException; import lombok.extern.log4j.Log4j2; import org.springframework.data.elasticsearch.NoSuchIndexException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; 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.NoHandlerFoundException; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; /** * GlobalExceptionHandler to handle all exception here. */ @ControllerAdvice @Log4j2 public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { /* handleNoHandlerFoundException :- handleNoHandlerFoundException is used for API not found */ @SuppressWarnings("NullableProblems") @Override protected ResponseEntity<Object> handleNoHandlerFoundException( final NoHandlerFoundException ex, final HttpHeaders headers, final HttpStatusCode status, final WebRequest request) { return this.handleNoHandlerFoundException(ex); } private ResponseEntity<Object> handleNoHandlerFoundException(final NoHandlerFoundException ex) { log.error( "inside handleNoHandlerFoundException method:{} message:{} ", ex.getClass(), ex.getMessage(), ex); var commonErrorResponseDto = CommonErrorResponseDto.builder() .errorCode(String.valueOf(HttpStatus.NOT_FOUND.value())) .message(HttpStatus.NOT_FOUND.getReasonPhrase()) .build(); var commonErrorResponse = new CommonErrorResponse<>(commonErrorResponseDto); return new ResponseEntity<>(commonErrorResponse, HttpStatus.NOT_FOUND); } /** * RecordsNotFoundException handler. * * @param ex RecordsNotFoundException exception * @param request request no use case * @return Error Response */ @ExceptionHandler({RecordsNotFoundException.class}) public ResponseEntity<CommonErrorResponse<CommonErrorResponseDto>> handleRecordsNotFoundException( final RecordsNotFoundException ex, final WebRequest request) { log.error( "inside handleRecordsNotFoundException method:{} message:{} ", ex.getClass(), ex.getMessage(), ex); var commonErrorResponseDto = CommonErrorResponseDto.builder() .errorCode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value())) .message(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()) .build(); var commonErrorResponse = new CommonErrorResponse<>(commonErrorResponseDto); return new ResponseEntity<>(commonErrorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } /** * IllegalValueException handler. * * @param ex IllegalValueException exception * @param request request no use case * @return Error Response */ @ExceptionHandler({IllegalValueException.class}) public ResponseEntity<CommonErrorResponse<CommonErrorResponseDto>> handleIllegalValueException( final IllegalValueException ex, final WebRequest request) { log.error( "inside handleIllegalValueException method:{} message:{} ", ex.getClass(), ex.getMessage(), ex); var commonErrorResponseDto = CommonErrorResponseDto.builder() .errorCode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value())) .message(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()) .build(); var commonErrorResponse = new CommonErrorResponse<>(commonErrorResponseDto); return new ResponseEntity<>(commonErrorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } /** * NoSuchIndexException handler. * * @param ex NoSuchIndexException exception * @param request request no use case * @return Error Response */ @ExceptionHandler({NoSuchIndexException.class}) public ResponseEntity<CommonErrorResponse<CommonErrorResponseDto>> handleNoSuchIndexException( final NoSuchIndexException ex, final WebRequest request) { log.error( "inside GlobalExceptionHandler method:{} message:{} ", ex.getClass(), ex.getMessage(), ex); var commonErrorResponseDto = CommonErrorResponseDto.builder() .errorCode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value())) .message(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()) .build(); var commonErrorResponse = new CommonErrorResponse<>(commonErrorResponseDto); return new ResponseEntity<>(commonErrorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } /** * This is generic exception handler. * * @param ex Generic Exception to catch all Exception * @param request request no use case * @return Error Response */ @ExceptionHandler({Throwable.class}) public ResponseEntity<CommonErrorResponse<CommonErrorResponseDto>> handleAll( final Throwable ex, final WebRequest request) { log.error( "inside GlobalExceptionHandler method:{} message:{} ", ex.getClass(), ex.getMessage(), ex); var commonErrorResponseDto = CommonErrorResponseDto.builder() .errorCode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value())) .message(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()) .build(); var commonErrorResponse = new CommonErrorResponse<>(commonErrorResponseDto); return new ResponseEntity<>(commonErrorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } }