forked from Olympic/NTO-2025-Backend-TeamTask
1
This commit is contained in:
35
src/main/java/com/example/nto/GlobalExceptionHandler.java
Normal file
35
src/main/java/com/example/nto/GlobalExceptionHandler.java
Normal file
@@ -0,0 +1,35 @@
|
||||
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("что-то пошло не так");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user