2025-11-24 12:29:32 +07:00
|
|
|
package ru.soune.no_copy.handler;
|
|
|
|
|
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
2025-11-26 13:03:20 +07:00
|
|
|
import ru.soune.no_copy.exception.ContentNotFoundException;
|
2025-11-25 11:59:37 +07:00
|
|
|
import ru.soune.no_copy.exception.NotValidationPasswordException;
|
2025-11-24 12:29:32 +07:00
|
|
|
import ru.soune.no_copy.exception.UserAlreadyExistsException;
|
2025-11-25 11:59:37 +07:00
|
|
|
import ru.soune.no_copy.exception.UserNotFoundException;
|
2025-11-24 12:29:32 +07:00
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@RestControllerAdvice
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
|
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
|
|
public ResponseEntity<?> handleValidationException(MethodArgumentNotValidException ex) {
|
|
|
|
|
String message = ex.getBindingResult().getFieldErrors().stream()
|
|
|
|
|
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
|
|
|
|
.findFirst()
|
|
|
|
|
.orElse("");
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.badRequest()
|
|
|
|
|
.body(Map.of(
|
|
|
|
|
"success", false,
|
|
|
|
|
"message" ,message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ExceptionHandler(UserAlreadyExistsException.class)
|
|
|
|
|
@ResponseStatus(HttpStatus.CONFLICT)
|
|
|
|
|
public ResponseEntity<?> handleUserContainsException(UserAlreadyExistsException ex) {
|
|
|
|
|
return ResponseEntity.
|
|
|
|
|
badRequest()
|
|
|
|
|
.body(Map.of(
|
|
|
|
|
"success", false,
|
|
|
|
|
"message" ,ex.getMessage()
|
|
|
|
|
));
|
|
|
|
|
}
|
2025-11-25 11:59:37 +07:00
|
|
|
|
|
|
|
|
@ExceptionHandler(UserNotFoundException.class)
|
|
|
|
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
|
|
|
|
public ResponseEntity<?> handleUserNotFoundException(UserNotFoundException ex) {
|
|
|
|
|
return ResponseEntity.
|
|
|
|
|
badRequest()
|
|
|
|
|
.body(Map.of(
|
|
|
|
|
"success", false,
|
|
|
|
|
"message" ,ex.getMessage()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 13:03:20 +07:00
|
|
|
@ExceptionHandler(ContentNotFoundException.class)
|
|
|
|
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
|
|
|
|
public ResponseEntity<?> handleUserNotFoundException(ContentNotFoundException ex) {
|
|
|
|
|
return ResponseEntity.
|
|
|
|
|
badRequest()
|
|
|
|
|
.body(Map.of(
|
|
|
|
|
"success", false,
|
|
|
|
|
"message" ,ex.getMessage()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 11:59:37 +07:00
|
|
|
@ExceptionHandler(NotValidationPasswordException.class)
|
|
|
|
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
|
|
|
|
public ResponseEntity<?> handleNotValidationPasswordException(NotValidationPasswordException ex) {
|
|
|
|
|
return ResponseEntity.
|
|
|
|
|
badRequest()
|
|
|
|
|
.body(Map.of(
|
|
|
|
|
"success", false,
|
|
|
|
|
"message" ,ex.getMessage()
|
|
|
|
|
));
|
|
|
|
|
}
|
2025-11-24 12:29:32 +07:00
|
|
|
}
|