Skip to content

Commit

Permalink
implemented custom exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
vladutmargineanu committed Jan 23, 2024
1 parent cc6f613 commit afd6370
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,11 +32,10 @@ public ResponseEntity<PlayerDto> 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();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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<Player> getPlayerDetails(String idPlayer) {
writeLog("PlayerService.getPlayerDetails() - get player by idPlayer: {}", idPlayer);
Optional<Player> 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<Player> optionalPlayer = playerRepository.findByIdPlayer(UUID.fromString(idPlayer));
if (optionalPlayer.isPresent()) {
Player player = optionalPlayer.get();
writeLog("PlayerService.getPlayerDetails() - player found: {}", player.getIdPlayer());
Expand All @@ -58,6 +72,7 @@ public Optional<Player> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}

0 comments on commit afd6370

Please sign in to comment.