Merge pull request 'refactor: throw and handle user not found exceptions' (!143) from refactor-user-not-found into main
All checks were successful
Release / Release (push) Successful in 58s
Release / Build Frontend Image (push) Successful in 25s
Release / Build Backend Image (push) Successful in 59s

Reviewed-on: #143
Reviewed-by: Jan K9f <jan@kjan.email>
This commit is contained in:
Phan Huy Tran 2025-04-24 12:38:07 +00:00
commit b9e48b8ada
Signed by:
GPG key ID: 944223E4D46B7412
7 changed files with 27 additions and 31 deletions

View file

@ -1,5 +1,6 @@
package de.szut.casino.blackjack;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.shared.dto.BetDto;
import de.szut.casino.shared.service.BalanceService;
import de.szut.casino.user.UserEntity;
@ -31,7 +32,7 @@ public class BlackJackGameController {
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
if (optionalUser.isEmpty()) {
return ResponseEntity.notFound().build();
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -48,7 +49,7 @@ public class BlackJackGameController {
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
if (optionalUser.isEmpty()) {
return ResponseEntity.notFound().build();
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -65,7 +66,7 @@ public class BlackJackGameController {
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
if (optionalUser.isEmpty()) {
return ResponseEntity.notFound().build();
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -82,7 +83,7 @@ public class BlackJackGameController {
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
if (optionalUser.isEmpty()) {
return ResponseEntity.notFound().build();
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -99,7 +100,7 @@ public class BlackJackGameController {
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
if (optionalUser.isEmpty()) {
return ResponseEntity.notFound().build();
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -116,7 +117,7 @@ public class BlackJackGameController {
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
if (optionalUser.isEmpty()) {
return ResponseEntity.notFound().build();
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();

View file

@ -1,8 +1,6 @@
package de.szut.casino.exceptionHandling;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
@ -12,17 +10,11 @@ import org.springframework.web.context.request.WebRequest;
import java.util.Date;
@ControllerAdvice
@ApiResponses(value = {
@ApiResponse(responseCode = "500", description = "invalid JSON posted",
content = @Content)
})
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<?> handleHelloEntityNotFoundException(ResourceNotFoundException ex, WebRequest request) {
@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);
}
}

View file

@ -1,11 +0,0 @@
package de.szut.casino.exceptionHandling;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}

View file

@ -0,0 +1,11 @@
package de.szut.casino.exceptionHandling.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException() {
super("user not found");
}
}

View file

@ -1,5 +1,6 @@
package de.szut.casino.lootboxes;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.user.UserEntity;
import de.szut.casino.user.UserRepository;
import de.szut.casino.user.UserService;
@ -37,7 +38,7 @@ public class LootBoxController {
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
if (optionalUser.isEmpty()) {
return ResponseEntity.notFound().build();
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();

View file

@ -1,5 +1,6 @@
package de.szut.casino.slots;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.shared.dto.BetDto;
import de.szut.casino.shared.service.BalanceService;
import de.szut.casino.user.UserEntity;
@ -31,7 +32,7 @@ public class SlotController {
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
if (optionalUser.isEmpty()) {
return ResponseEntity.notFound().build();
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();

View file

@ -1,5 +1,6 @@
package de.szut.casino.user;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.user.dto.CreateUserDto;
import de.szut.casino.user.dto.GetUserDto;
import jakarta.validation.Valid;
@ -34,7 +35,7 @@ public class UserController {
GetUserDto userData = userService.getCurrentUserAsDto(token);
if (userData == null) {
return ResponseEntity.notFound().build();
throw new UserNotFoundException();
}
return ResponseEntity.ok(userData);