refactor: throw and handle user not found exceptions
All checks were successful
All checks were successful
This commit is contained in:
parent
ef069d7d18
commit
1f4256124f
7 changed files with 27 additions and 31 deletions
|
@ -1,5 +1,6 @@
|
||||||
package de.szut.casino.blackjack;
|
package de.szut.casino.blackjack;
|
||||||
|
|
||||||
|
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
||||||
import de.szut.casino.shared.dto.BetDto;
|
import de.szut.casino.shared.dto.BetDto;
|
||||||
import de.szut.casino.shared.service.BalanceService;
|
import de.szut.casino.shared.service.BalanceService;
|
||||||
import de.szut.casino.user.UserEntity;
|
import de.szut.casino.user.UserEntity;
|
||||||
|
@ -31,7 +32,7 @@ public class BlackJackGameController {
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
|
|
||||||
if (optionalUser.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = optionalUser.get();
|
UserEntity user = optionalUser.get();
|
||||||
|
@ -48,7 +49,7 @@ public class BlackJackGameController {
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
|
|
||||||
if (optionalUser.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = optionalUser.get();
|
UserEntity user = optionalUser.get();
|
||||||
|
@ -65,7 +66,7 @@ public class BlackJackGameController {
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
|
|
||||||
if (optionalUser.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = optionalUser.get();
|
UserEntity user = optionalUser.get();
|
||||||
|
@ -82,7 +83,7 @@ public class BlackJackGameController {
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
|
|
||||||
if (optionalUser.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = optionalUser.get();
|
UserEntity user = optionalUser.get();
|
||||||
|
@ -99,7 +100,7 @@ public class BlackJackGameController {
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
|
|
||||||
if (optionalUser.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = optionalUser.get();
|
UserEntity user = optionalUser.get();
|
||||||
|
@ -116,7 +117,7 @@ public class BlackJackGameController {
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
|
|
||||||
if (optionalUser.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = optionalUser.get();
|
UserEntity user = optionalUser.get();
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package de.szut.casino.exceptionHandling;
|
package de.szut.casino.exceptionHandling;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
@ -12,17 +10,11 @@ import org.springframework.web.context.request.WebRequest;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
@ApiResponses(value = {
|
|
||||||
@ApiResponse(responseCode = "500", description = "invalid JSON posted",
|
|
||||||
content = @Content)
|
|
||||||
})
|
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
@ExceptionHandler(ResourceNotFoundException.class)
|
@ExceptionHandler(UserNotFoundException.class)
|
||||||
public ResponseEntity<?> handleHelloEntityNotFoundException(ResourceNotFoundException ex, WebRequest request) {
|
public ResponseEntity<?> handleUserNotFoundException(UserNotFoundException ex, WebRequest request) {
|
||||||
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
|
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
|
||||||
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package de.szut.casino.lootboxes;
|
package de.szut.casino.lootboxes;
|
||||||
|
|
||||||
|
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
||||||
import de.szut.casino.user.UserEntity;
|
import de.szut.casino.user.UserEntity;
|
||||||
import de.szut.casino.user.UserRepository;
|
import de.szut.casino.user.UserRepository;
|
||||||
import de.szut.casino.user.UserService;
|
import de.szut.casino.user.UserService;
|
||||||
|
@ -37,7 +38,7 @@ public class LootBoxController {
|
||||||
|
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
if (optionalUser.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = optionalUser.get();
|
UserEntity user = optionalUser.get();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package de.szut.casino.slots;
|
package de.szut.casino.slots;
|
||||||
|
|
||||||
|
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
||||||
import de.szut.casino.shared.dto.BetDto;
|
import de.szut.casino.shared.dto.BetDto;
|
||||||
import de.szut.casino.shared.service.BalanceService;
|
import de.szut.casino.shared.service.BalanceService;
|
||||||
import de.szut.casino.user.UserEntity;
|
import de.szut.casino.user.UserEntity;
|
||||||
|
@ -31,7 +32,7 @@ public class SlotController {
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
|
|
||||||
if (optionalUser.isEmpty()) {
|
if (optionalUser.isEmpty()) {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = optionalUser.get();
|
UserEntity user = optionalUser.get();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package de.szut.casino.user;
|
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.CreateUserDto;
|
||||||
import de.szut.casino.user.dto.GetUserDto;
|
import de.szut.casino.user.dto.GetUserDto;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
@ -34,7 +35,7 @@ public class UserController {
|
||||||
GetUserDto userData = userService.getCurrentUserAsDto(token);
|
GetUserDto userData = userService.getCurrentUserAsDto(token);
|
||||||
|
|
||||||
if (userData == null) {
|
if (userData == null) {
|
||||||
return ResponseEntity.notFound().build();
|
throw new UserNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResponseEntity.ok(userData);
|
return ResponseEntity.ok(userData);
|
||||||
|
|
Reference in a new issue