diff --git a/matcher/src/main/java/com/profile/matcher/arhitecture/BaseException.java b/matcher/src/main/java/com/profile/matcher/arhitecture/BaseException.java index 330e3ec..d068e0c 100644 --- a/matcher/src/main/java/com/profile/matcher/arhitecture/BaseException.java +++ b/matcher/src/main/java/com/profile/matcher/arhitecture/BaseException.java @@ -1,11 +1,35 @@ package com.profile.matcher.arhitecture; +import org.springframework.http.HttpStatus; + +import static com.profile.matcher.utils.Constants.ErrorCode.DEFAULT_ERROR_CODE; + public class BaseException extends RuntimeException { + + private final String code; + private final HttpStatus status; + public BaseException(String message) { super(message); + this.code = DEFAULT_ERROR_CODE; + this.status = HttpStatus.INTERNAL_SERVER_ERROR; } public BaseException(String message, Throwable cause) { super(message, cause); + this.code = DEFAULT_ERROR_CODE; + this.status = HttpStatus.INTERNAL_SERVER_ERROR; + } + + public BaseException(String message, Throwable cause, String code, HttpStatus status) { + super(message, cause); + this.code = code; + this.status = status; + } + + public BaseException(String message, String code, HttpStatus status) { + super(message); + this.status = status; + this.code = code; } } diff --git a/matcher/src/main/java/com/profile/matcher/controller/PlayerController.java b/matcher/src/main/java/com/profile/matcher/controller/PlayerController.java index fb7fb20..5532e6c 100644 --- a/matcher/src/main/java/com/profile/matcher/controller/PlayerController.java +++ b/matcher/src/main/java/com/profile/matcher/controller/PlayerController.java @@ -6,6 +6,7 @@ import com.profile.matcher.dto.player.PlayerDto; import com.profile.matcher.entity.player.Player; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -31,11 +32,10 @@ public ResponseEntity getClientConfig(@PathVariable("player_id") Stri if (playerOptional.isPresent()) { PlayerDto playerDto = playerAssembler.toResource(playerOptional.get()); writeLog("PlayerController.getClientConfig() - player found: {}", playerDto); - return ResponseEntity.ok(playerDto); } else { writeLog("PlayerController.getClientConfig() - player not found"); - return new ResponseEntity<>(HttpStatus.NO_CONTENT); + return ResponseEntity.status(HttpStatus.NO_CONTENT).headers(HttpHeaders.EMPTY).build(); } } diff --git a/matcher/src/main/java/com/profile/matcher/exception/FindCurrentCampaignException.java b/matcher/src/main/java/com/profile/matcher/exception/FindCurrentCampaignException.java new file mode 100644 index 0000000..19fab19 --- /dev/null +++ b/matcher/src/main/java/com/profile/matcher/exception/FindCurrentCampaignException.java @@ -0,0 +1,15 @@ +package com.profile.matcher.exception; + +import com.profile.matcher.arhitecture.BaseException; +import org.springframework.http.HttpStatus; + +public class FindCurrentCampaignException extends BaseException { + + public FindCurrentCampaignException(String message, Throwable cause) { + super(message, cause); + } + + public FindCurrentCampaignException(String message, String code, HttpStatus status) { + super(message, code, status); + } +} diff --git a/matcher/src/main/java/com/profile/matcher/exception/FindPlayerDetailsException.java b/matcher/src/main/java/com/profile/matcher/exception/FindPlayerDetailsException.java new file mode 100644 index 0000000..d32c19c --- /dev/null +++ b/matcher/src/main/java/com/profile/matcher/exception/FindPlayerDetailsException.java @@ -0,0 +1,15 @@ +package com.profile.matcher.exception; + +import com.profile.matcher.arhitecture.BaseException; +import org.springframework.http.HttpStatus; + +public class FindPlayerDetailsException extends BaseException { + + public FindPlayerDetailsException(String message, Throwable cause) { + super(message, cause); + } + + public FindPlayerDetailsException(String message, String code, HttpStatus status) { + super(message, code, status); + } +} diff --git a/matcher/src/main/java/com/profile/matcher/exception/NotAllowedException.java b/matcher/src/main/java/com/profile/matcher/exception/NotAllowedException.java deleted file mode 100644 index 9a69ba3..0000000 --- a/matcher/src/main/java/com/profile/matcher/exception/NotAllowedException.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.profile.matcher.exception; - -import com.profile.matcher.arhitecture.BaseException; - -public class NotAllowedException extends BaseException { - - public NotAllowedException(String message) { - super(message); - } - - public NotAllowedException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/matcher/src/main/java/com/profile/matcher/exception/UpdatePlayerDetailsException.java b/matcher/src/main/java/com/profile/matcher/exception/UpdatePlayerDetailsException.java new file mode 100644 index 0000000..a464f2d --- /dev/null +++ b/matcher/src/main/java/com/profile/matcher/exception/UpdatePlayerDetailsException.java @@ -0,0 +1,14 @@ +package com.profile.matcher.exception; + +import com.profile.matcher.arhitecture.BaseException; + +public class UpdatePlayerDetailsException extends BaseException { + + public UpdatePlayerDetailsException(String message) { + super(message); + } + + public UpdatePlayerDetailsException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/matcher/src/main/java/com/profile/matcher/service/PlayerService.java b/matcher/src/main/java/com/profile/matcher/service/PlayerService.java index 77532c8..019b279 100644 --- a/matcher/src/main/java/com/profile/matcher/service/PlayerService.java +++ b/matcher/src/main/java/com/profile/matcher/service/PlayerService.java @@ -4,8 +4,12 @@ import com.profile.matcher.dto.campaign.CampaignDto; import com.profile.matcher.entity.player.Item; import com.profile.matcher.entity.player.Player; +import com.profile.matcher.exception.FindCurrentCampaignException; +import com.profile.matcher.exception.FindPlayerDetailsException; +import com.profile.matcher.exception.UpdatePlayerDetailsException; import com.profile.matcher.repository.PlayerRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @@ -16,6 +20,9 @@ import java.util.UUID; import java.util.function.BiPredicate; +import static com.profile.matcher.utils.Constants.ErrorCode.ERROR_CURRENT_CAMPAIGN; +import static com.profile.matcher.utils.Constants.ErrorCode.ERROR_PLAYER_DETAIL; + @Service public class PlayerService extends BaseService { @@ -28,11 +35,18 @@ public class PlayerService extends BaseService { * @param idPlayer * @return */ - @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED) + @Transactional(rollbackFor = {Exception.class, UpdatePlayerDetailsException.class}, propagation = Propagation.REQUIRED) public Optional getPlayerDetails(String idPlayer) { - writeLog("PlayerService.getPlayerDetails() - get player by idPlayer: {}", idPlayer); + Optional optionalPlayer; + + try { + writeLog("PlayerService.getPlayerDetails() - get player by idPlayer: {}", idPlayer); + optionalPlayer = playerRepository.findByIdPlayer(UUID.fromString(idPlayer)); + } catch (Exception e) { + writeLog("PlayerService.getPlayerDetails() - failed to retrieve player: {}", e.getMessage()); + throw new FindPlayerDetailsException(e.getMessage(), ERROR_PLAYER_DETAIL, HttpStatus.INTERNAL_SERVER_ERROR); + } - Optional optionalPlayer = playerRepository.findByIdPlayer(UUID.fromString(idPlayer)); if (optionalPlayer.isPresent()) { Player player = optionalPlayer.get(); writeLog("PlayerService.getPlayerDetails() - player found: {}", player.getIdPlayer()); @@ -58,6 +72,7 @@ public Optional getPlayerDetails(String idPlayer) { } catch (Exception e) { writeLog("PlayerService.getPlayerDetails() - failed to retrieve current campaign: {}", e.getMessage()); + throw new FindCurrentCampaignException(e.getMessage(), ERROR_CURRENT_CAMPAIGN, HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/matcher/src/main/java/com/profile/matcher/utils/Constants.java b/matcher/src/main/java/com/profile/matcher/utils/Constants.java index 3f90616..e061a3d 100644 --- a/matcher/src/main/java/com/profile/matcher/utils/Constants.java +++ b/matcher/src/main/java/com/profile/matcher/utils/Constants.java @@ -8,4 +8,10 @@ private Constants() { public static final class DateTimePatterns { public static final String DATE_WITH_TIME = "yyyy-MM-dd HH:mm:ss'Z'"; } + + public static final class ErrorCode { + public static final String DEFAULT_ERROR_CODE = "generic.error"; + public static final String ERROR_PLAYER_DETAIL = "error.player.detail"; + public static final String ERROR_CURRENT_CAMPAIGN = "error.current.campaign"; + } }