forked from Olympic/NTO-2025-Backend-TeamTask
35 lines
1.5 KiB
Java
35 lines
1.5 KiB
Java
package com.example.nto;
|
|
|
|
import com.example.nto.exception.AlreadyBookedException;
|
|
import com.example.nto.exception.CodeNotFoundException;
|
|
import com.example.nto.exception.InvalidRequestException;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
@ControllerAdvice
|
|
public class GlobalExceptionHandler {
|
|
|
|
@ExceptionHandler(InvalidRequestException.class)
|
|
public ResponseEntity<String> handleInvalidRequest(InvalidRequestException ex) {
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
|
|
}
|
|
|
|
@ExceptionHandler(CodeNotFoundException.class)
|
|
public ResponseEntity<String> handleCodeNotFound(CodeNotFoundException ex) {
|
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ex.getMessage());
|
|
}
|
|
|
|
@ExceptionHandler(AlreadyBookedException.class)
|
|
public ResponseEntity<String> handleAlreadyBooked(AlreadyBookedException ex) {
|
|
return ResponseEntity.status(HttpStatus.CONFLICT).body(ex.getMessage());
|
|
}
|
|
|
|
// Обработка ошибок парсинга JSON
|
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
|
public ResponseEntity<String> handleJsonParseError(HttpMessageNotReadableException ex) {
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("что-то пошло не так");
|
|
}
|
|
} |