Add rag-service
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package com.balex.rag.advice;
|
||||
|
||||
import com.balex.rag.model.constants.ApiConstants;
|
||||
import com.balex.rag.model.exception.InvalidDataException;
|
||||
import com.balex.rag.model.exception.InvalidPasswordException;
|
||||
import com.balex.rag.model.exception.NotFoundException;
|
||||
import com.balex.rag.service.model.exception.DataExistException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import java.nio.file.AccessDeniedException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
public class CommonControllerAdvice {
|
||||
|
||||
@ExceptionHandler
|
||||
@ResponseBody
|
||||
protected ResponseEntity<String> handleNotFoundException(NotFoundException ex) {
|
||||
logStackTrace(ex);
|
||||
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.NOT_FOUND)
|
||||
.body(ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(DataExistException.class)
|
||||
@ResponseBody
|
||||
protected ResponseEntity<String> handleDataExistException(DataExistException ex) {
|
||||
logStackTrace(ex);
|
||||
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.CONFLICT)
|
||||
.body(ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<Map<String, String>> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
|
||||
logStackTrace(ex);
|
||||
|
||||
Map<String, String> errors = new HashMap<>();
|
||||
for (ObjectError error : ex.getBindingResult().getAllErrors()) {
|
||||
String errorMessage = error.getDefaultMessage();
|
||||
errors.put("error", errorMessage);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(AuthenticationException.class)
|
||||
@ResponseBody
|
||||
protected ResponseEntity<String> handleAuthenticationException(AuthenticationException ex) {
|
||||
logStackTrace(ex);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.UNAUTHORIZED)
|
||||
.body(ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(InvalidDataException.class)
|
||||
@ResponseBody
|
||||
protected ResponseEntity<String> handleInvalidDataException(InvalidDataException ex) {
|
||||
logStackTrace(ex);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body(ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(InvalidPasswordException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ResponseBody
|
||||
public String handleInvalidPasswordException(InvalidPasswordException ex) {
|
||||
return ex.getMessage();
|
||||
}
|
||||
|
||||
@ExceptionHandler(AccessDeniedException.class)
|
||||
@ResponseBody
|
||||
protected ResponseEntity<String> handleAccessDeniedException(AccessDeniedException ex) {
|
||||
logStackTrace(ex);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.FORBIDDEN)
|
||||
.body(ex.getMessage());
|
||||
}
|
||||
|
||||
private void logStackTrace(Exception ex) {
|
||||
StringBuilder stackTrace = new StringBuilder();
|
||||
|
||||
stackTrace.append(ApiConstants.ANSI_RED);
|
||||
|
||||
stackTrace.append(ex.getMessage()).append(ApiConstants.BREAK_LINE);
|
||||
|
||||
if (Objects.nonNull(ex.getCause())) {
|
||||
stackTrace.append(ex.getCause().getMessage()).append(ApiConstants.BREAK_LINE);
|
||||
}
|
||||
|
||||
Arrays.stream(ex.getStackTrace())
|
||||
.filter(st -> st.getClassName().startsWith(ApiConstants.TIME_ZONE_PACKAGE_NAME))
|
||||
.forEach(st -> stackTrace
|
||||
.append(st.getClassName())
|
||||
.append(".")
|
||||
.append(st.getMethodName())
|
||||
.append(" (")
|
||||
.append(st.getLineNumber())
|
||||
.append(") ")
|
||||
);
|
||||
|
||||
log.error(stackTrace.append(ApiConstants.ANSI_WHITE).toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user