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-27 18:01:02 +07:00
|
|
|
import ru.soune.no_copy.exception.*;
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-10 04:51:07 +07:00
|
|
|
@ExceptionHandler(NotValidFieldException.class)
|
2025-12-10 02:37:31 +07:00
|
|
|
@ResponseStatus(HttpStatus.OK)
|
2025-12-10 04:51:07 +07:00
|
|
|
public ResponseEntity<?> handleUserContainsException(NotValidFieldException ex) {
|
2025-12-10 02:37:31 +07:00
|
|
|
|
|
|
|
|
return ResponseEntity
|
|
|
|
|
.ok()
|
2025-11-24 12:29:32 +07:00
|
|
|
.body(Map.of(
|
2025-12-10 02:37:31 +07:00
|
|
|
"msg_id", ex.getMessageId(),
|
|
|
|
|
"message_code", ex.getMessageCode(),
|
|
|
|
|
"message_desc", ex.getMessageDescription()));
|
2025-11-24 12:29:32 +07:00
|
|
|
}
|
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-27 18:01:02 +07:00
|
|
|
|
|
|
|
|
@ExceptionHandler(TokenNotFoundException.class)
|
|
|
|
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
|
|
|
|
public ResponseEntity<?> handleNotFoundTokenException(TokenNotFoundException ex) {
|
|
|
|
|
return ResponseEntity.
|
|
|
|
|
badRequest()
|
|
|
|
|
.body(Map.of(
|
|
|
|
|
"success", false,
|
|
|
|
|
"message" ,ex.getMessage()
|
|
|
|
|
));
|
|
|
|
|
}
|
2025-11-24 12:29:32 +07:00
|
|
|
}
|