34 lines
1.6 KiB
Java
34 lines
1.6 KiB
Java
package de.szut.casino.exceptionHandling;
|
|
|
|
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
|
|
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
|
import jakarta.persistence.EntityExistsException;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.context.request.WebRequest;
|
|
|
|
import java.util.Date;
|
|
|
|
@ControllerAdvice
|
|
public class GlobalExceptionHandler {
|
|
|
|
@ExceptionHandler(UserNotFoundException.class)
|
|
public ResponseEntity<?> handleUserNotFoundException(UserNotFoundException ex, WebRequest request) {
|
|
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
|
|
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
|
|
}
|
|
|
|
@ExceptionHandler(InsufficientFundsException.class)
|
|
public ResponseEntity<?> handleInsufficientFundsException(InsufficientFundsException ex, WebRequest request) {
|
|
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
|
|
return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
|
|
}
|
|
|
|
@ExceptionHandler(EntityExistsException.class)
|
|
public ResponseEntity<?> handleEntityExistsException(EntityExistsException ex, WebRequest request) {
|
|
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
|
|
return new ResponseEntity<>(errorDetails, HttpStatus.CONFLICT);
|
|
}
|
|
}
|