Skip to content

Commit

Permalink
Merge pull request #84 from Tune-Fun/fix/common_application_exception
Browse files Browse the repository at this point in the history
Fix/common application exception
  • Loading branch information
habinkim authored Jul 3, 2024
2 parents 4d17140 + 46770e6 commit e5b2826
Show file tree
Hide file tree
Showing 35 changed files with 114 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class LogoutController {
public ResponseEntity<Response<?>> logout(final HttpServletRequest request,
@Valid @RequestBody final AccountCommands.Device device, @CurrentUser final User user) {
String authorizationValue = Optional.ofNullable(request.getHeader(AUTHORIZATION))
.orElseThrow(() -> new CommonApplicationException(EXCEPTION_AUTHENTICATION_TOKEN_NOT_FOUND));
.orElseThrow(CommonApplicationException.EXCEPTION_AUTHENTICATION_TOKEN_NOT_FOUND);
String accessToken = StringUtil.removeBearerPrefix(authorizationValue);

logoutUseCase.logout(accessToken, device, user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private String getAccessTokenFromRequest(final HttpServletRequest request) {
String accessToken = Optional.ofNullable(request.getHeader(AUTHORIZATION))
.orElseThrow(() -> {
log.info("Servlet Path is {}", request.getServletPath());
return new CommonApplicationException(MessageCode.EXCEPTION_AUTHENTICATION_TOKEN_NOT_FOUND);
return CommonApplicationException.EXCEPTION_AUTHENTICATION_TOKEN_NOT_FOUND;
});

return StringUtil.removeBearerPrefix(accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.tune_fun.v1.account.application.port.output.jwt.*;
import com.tune_fun.v1.account.domain.behavior.SaveJwtToken;
import com.tune_fun.v1.common.exception.CommonApplicationException;
import com.tune_fun.v1.common.stereotype.PersistenceAdapter;
import com.tune_fun.v1.common.property.JwtProperty;
import com.tune_fun.v1.common.response.MessageCode;
import com.tune_fun.v1.common.stereotype.PersistenceAdapter;
import com.tune_fun.v1.external.aws.kms.KmsProvider;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
Expand Down Expand Up @@ -82,7 +82,7 @@ public Boolean validate(String token) {
return true;
} catch (IllegalArgumentException e) {
log.error("JWT claims string is empty: {}", e.getMessage());
throw new CommonApplicationException(MessageCode.EXCEPTION_AUTHENTICATION_INVALID_TOKEN);
throw CommonApplicationException.EXCEPTION_AUTHENTICATION_INVALID_TOKEN;
}
}

Expand Down Expand Up @@ -162,10 +162,10 @@ public String reissueAccessToken(String refreshTokenValue) {
RefreshTokenRedisEntity refreshToken = redisTemplateRefresh.opsForValue().get(refreshTokenKey);

if (refreshToken == null && refreshToken.getToken().equals(refreshTokenValue))
throw new CommonApplicationException(MessageCode.EXCEPTION_AUTHENTICATION_INVALID_TOKEN);
throw CommonApplicationException.EXCEPTION_AUTHENTICATION_INVALID_TOKEN;

if (isRefreshTokenExpired(refreshToken.getToken()))
throw new CommonApplicationException(MessageCode.EXCEPTION_EXPIRED_REFRESH_TOKEN);
throw CommonApplicationException.EXCEPTION_EXPIRED_REFRESH_TOKEN;

SaveJwtToken behavior = new SaveJwtToken(username, getPayload(refreshTokenValue).get("role").toString());
return createAccessToken(behavior);
Expand Down Expand Up @@ -212,7 +212,7 @@ private Claims getPayload(final String token) {
try {
return getJwtParser().parseSignedClaims(token).getPayload();
} catch (IllegalArgumentException e) {
throw new CommonApplicationException(MessageCode.EXCEPTION_AUTHENTICATION_INVALID_TOKEN);
throw CommonApplicationException.EXCEPTION_AUTHENTICATION_INVALID_TOKEN;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.USER_POLICY_USERNAME_REGISTERED;


@Service
@UseCase
Expand All @@ -23,7 +21,7 @@ public class CheckUsernameDuplicateService implements CheckUsernameDuplicateUseC
public void checkUsernameDuplicate(final String username) {
loadAccountPort.registeredAccountInfoByUsername(username)
.ifPresent(account -> {
throw new CommonApplicationException(USER_POLICY_USERNAME_REGISTERED);
throw CommonApplicationException.USER_POLICY_USERNAME_REGISTERED;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.ACCOUNT_NOT_FOUND;


@Service
@UseCase
Expand All @@ -24,6 +22,6 @@ public class CustomUserDetailsService implements UserDetailsService {
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return loadAccountPort.loadCustomUserByUsername(username)
.orElseThrow(() -> new CommonApplicationException(ACCOUNT_NOT_FOUND));
.orElseThrow(CommonApplicationException.ACCOUNT_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import java.util.Optional;

import static com.tune_fun.v1.common.response.MessageCode.ACCOUNT_NOT_FOUND;


@Service
@UseCase
Expand All @@ -36,6 +34,6 @@ public void findUsername(final AccountQueries.Username query) throws Exception {
return;
}

throw new CommonApplicationException(ACCOUNT_NOT_FOUND);
throw CommonApplicationException.ACCOUNT_NOT_FOUND;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.ACCOUNT_NOT_FOUND;


@Service
@UseCase
Expand Down Expand Up @@ -50,10 +48,10 @@ private static LoginResult getLoginResult(RegisteredAccount registeredAccount, S
@Transactional
public LoginResult login(final AccountCommands.Login command) {
RegisteredAccount registeredAccount = loadAccountPort.registeredAccountInfoByUsername(command.username())
.orElseThrow(() -> new CommonApplicationException(ACCOUNT_NOT_FOUND));
.orElseThrow(CommonApplicationException.ACCOUNT_NOT_FOUND);

if (!passwordEncoder.matches(command.password(), registeredAccount.password()))
throw new CommonApplicationException(ACCOUNT_NOT_FOUND);
throw CommonApplicationException.ACCOUNT_NOT_FOUND;

String authorities = String.join(Constants.COMMA, registeredAccount.roles());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.USER_POLICY_ACCOUNT_REGISTERED;


@Service
@UseCase
Expand Down Expand Up @@ -71,7 +69,7 @@ public RegisterResult register(final String registerType, final AccountCommands.
@Transactional(readOnly = true)
public void checkRegisteredAccount(AccountCommands.Register command) {
loadAccountPort.currentAccountInfo(command.username()).ifPresent(accountInfo -> {
throw new CommonApplicationException(USER_POLICY_ACCOUNT_REGISTERED);
throw CommonApplicationException.USER_POLICY_ACCOUNT_REGISTERED;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.ACCOUNT_NOT_FOUND;
import static com.tune_fun.v1.otp.adapter.output.persistence.OtpType.FORGOT_PASSWORD;


Expand Down Expand Up @@ -49,6 +48,6 @@ public void sendOtp(final AccountCommands.SendForgotPasswordOtp command) throws
@Transactional(readOnly = true)
public CurrentAccount getCurrentAccount(final AccountCommands.SendForgotPasswordOtp command) {
return loadAccountPort.currentAccountInfo(command.username())
.orElseThrow(() -> new CommonApplicationException(ACCOUNT_NOT_FOUND));
.orElseThrow(CommonApplicationException.ACCOUNT_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.USER_POLICY_NICKNAME_REGISTERED;

@Service
@UseCase
@RequiredArgsConstructor
Expand All @@ -25,7 +23,7 @@ public class UpdateNicknameService implements UpdateNicknameUseCase {
@Transactional
public void updateNickname(final AccountCommands.UpdateNickname command, final User user) {
if (loadAccountPort.registeredAccountInfoByNickname(command.newNickname()).isPresent())
throw new CommonApplicationException(USER_POLICY_NICKNAME_REGISTERED);
throw CommonApplicationException.USER_POLICY_NICKNAME_REGISTERED;

updateNicknamePort.updateNickname(user.getUsername(), command.newNickname());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.USER_POLICY_EMAIL_REGISTERED;


@Service
@UseCase
Expand All @@ -23,7 +21,7 @@ public class CheckEmailDuplicateService implements CheckEmailDuplicateUseCase {
public void checkEmailDuplicate(final String email) {
loadAccountPort.registeredAccountInfoByEmail(email)
.ifPresent(account -> {
throw new CommonApplicationException(USER_POLICY_EMAIL_REGISTERED);
throw CommonApplicationException.USER_POLICY_EMAIL_REGISTERED;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.ACCOUNT_NOT_FOUND;
import static com.tune_fun.v1.common.response.MessageCode.EXCEPTION_EMAIL_NOT_VERIFIED;


@Service
@UseCase
Expand All @@ -25,9 +22,9 @@ public class CheckEmailVerifiedService implements CheckEmailVerifiedUseCase {
@Transactional
public void checkEmailVerified(final User user) {
CurrentAccount currentAccount = loadAccountPort.currentAccountInfo(user.getUsername())
.orElseThrow(() -> new CommonApplicationException(ACCOUNT_NOT_FOUND));
.orElseThrow(CommonApplicationException.ACCOUNT_NOT_FOUND);

if (currentAccount.emailVerifiedAt() == null)
throw new CommonApplicationException(EXCEPTION_EMAIL_NOT_VERIFIED);
throw CommonApplicationException.EXCEPTION_EMAIL_NOT_VERIFIED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.ACCOUNT_NOT_FOUND;
import static com.tune_fun.v1.common.response.MessageCode.USER_POLICY_CANNOT_REGISTER_EMAIL_TWICE;

@Service
@UseCase
@RequiredArgsConstructor
Expand All @@ -28,10 +25,10 @@ public class RegisterEmailService implements RegisterEmailUseCase {
@Override
public void registerEmail(AccountCommands.SaveEmail command, User user) throws Exception {
CurrentAccount currentAccount = loadAccountPort.currentAccountInfo(user.getUsername())
.orElseThrow(() -> new CommonApplicationException(ACCOUNT_NOT_FOUND));
.orElseThrow(CommonApplicationException.ACCOUNT_NOT_FOUND);

if (currentAccount.email() != null)
throw new CommonApplicationException(USER_POLICY_CANNOT_REGISTER_EMAIL_TWICE);
throw CommonApplicationException.USER_POLICY_CANNOT_REGISTER_EMAIL_TWICE;

saveEmailPort.saveEmail(command.email(), user.getUsername());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.tune_fun.v1.common.response.MessageCode.ACCOUNT_NOT_FOUND;
import static com.tune_fun.v1.otp.adapter.output.persistence.OtpType.VERIFY_EMAIL;

@Service
Expand Down Expand Up @@ -49,6 +48,6 @@ private static SaveOtp getSaveOtp(String username) {
@Transactional(readOnly = true)
public CurrentAccount getCurrentAccount(final String username) {
return loadAccountPort.currentAccountInfo(username)
.orElseThrow(() -> new CommonApplicationException(ACCOUNT_NOT_FOUND));
.orElseThrow(CommonApplicationException.ACCOUNT_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import static com.tune_fun.v1.account.adapter.output.persistence.oauth2.OAuth2AuthorizationRequestPersistenceAdapter.*;
import static com.tune_fun.v1.account.domain.value.oauth2.OAuth2AuthorizationRequestMode.fromQueryParameter;
import static com.tune_fun.v1.account.domain.value.oauth2.OAuth2Provider.APPLE;
import static com.tune_fun.v1.common.response.MessageCode.*;
import static com.tune_fun.v1.common.util.CookieUtil.getCookie;
import static com.tune_fun.v1.common.util.StringUtil.getFlattenAuthorities;
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
Expand Down Expand Up @@ -126,7 +125,7 @@ public String link(final OAuth2UserPrincipal principal, final String targetUrl,
if (usernameOptional.isEmpty()) return AUTH_FAILED_URL_FUNCTION.apply(targetUrl);

if (loadRegisteredOAuth2Account(principal).isPresent())
throw new CommonApplicationException(USER_POLICY_ALREADY_LINKED_PROVIDER);
throw CommonApplicationException.USER_POLICY_ALREADY_LINKED_PROVIDER;

String username = usernameOptional.get();
RegisteredAccount registeredAccount = loadRegisteredAccount(username);
Expand Down Expand Up @@ -162,7 +161,7 @@ public String unlink(final OAuth2UserPrincipal principal, final String targetUrl
String username = usernameOptional.get();
RegisteredAccount registeredAccount = loadRegisteredAccount(username);
if (registeredAccount.isUniqueOAuth2Account())
throw new CommonApplicationException(USER_POLICY_CANNOT_UNLINK_UNIQUE_PROVIDER);
throw CommonApplicationException.USER_POLICY_CANNOT_UNLINK_UNIQUE_PROVIDER;

unlinkHttpRequest(provider, accessToken);
disableOAuth2AccountPort.disableOAuth2Account(principal.userInfo().getEmail());
Expand All @@ -184,7 +183,7 @@ protected void clearAuthenticationAttributes(HttpServletRequest request, HttpSer
@Transactional
public RegisteredAccount loadRegisteredAccount(final String username) {
return loadAccountPort.registeredAccountInfoByUsername(username)
.orElseThrow(() -> new CommonApplicationException(ACCOUNT_NOT_FOUND));
.orElseThrow(CommonApplicationException.ACCOUNT_NOT_FOUND);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.tune_fun.v1.common.lock.DistributedTransactionMediator;
import com.tune_fun.v1.common.lock.DistributionLock;
import com.tune_fun.v1.common.lock.DistributionLockKeyGenerator;
import com.tune_fun.v1.common.response.MessageCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
Expand All @@ -15,9 +14,6 @@
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import static com.tune_fun.v1.common.response.MessageCode.LOCK_ACQUISITION_FAILED_ERROR;
import static com.tune_fun.v1.common.response.MessageCode.LOCK_INTERRUPTED_ERROR;

@Slf4j
@Aspect
@Component
Expand All @@ -34,13 +30,13 @@ public Object lock(ProceedingJoinPoint joinPoint, DistributionLock distributionL

try {
if (!lockInfo.reentrantLock().tryLock(distributionLock.waitTime(), distributionLock.leaseTime(), distributionLock.timeUnit()))
throw new CommonApplicationException(LOCK_ACQUISITION_FAILED_ERROR);
throw CommonApplicationException.LOCK_ACQUISITION_FAILED_ERROR;

log.info("reentrantLock - {}", lockInfo.key());
return distributedTransactionMediator.proceed(joinPoint);
} catch (InterruptedException e) {
log.error(e.getMessage());
throw new CommonApplicationException(LOCK_INTERRUPTED_ERROR);
throw CommonApplicationException.LOCK_INTERRUPTED_ERROR;
} finally {
log.info("unlock - {}", lockInfo.key());
lockInfo.reentrantLock().unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.tune_fun.v1.common.exception.CommonApplicationException;
import com.tune_fun.v1.common.rate.TokenBucketResolver;
import com.tune_fun.v1.common.response.MessageCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
Expand Down Expand Up @@ -31,7 +30,7 @@ public Object rateLimit(ProceedingJoinPoint joinPoint) throws Throwable {

if (tokenBucketResolver.checkBucketCounter(key)) return joinPoint.proceed();

throw new CommonApplicationException(MessageCode.TOO_MANY_REQUESTS);
throw CommonApplicationException.TOO_MANY_REQUESTS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

import java.io.Serializable;

import static com.tune_fun.v1.common.response.MessageCode.*;

@Slf4j
@Component
@RequiredArgsConstructor
Expand All @@ -40,16 +38,16 @@ public boolean hasPermission(Authentication authentication, Serializable targetI

public boolean hasPermissionForVotePaper(User principal, Serializable targetId) {
RegisteredVotePaper registeredVotePaper = loadVotePaperPort.loadRegisteredVotePaper(principal.getUsername())
.orElseThrow(() -> new CommonApplicationException(VOTE_POLICY_ONLY_AUTHOR_CAN_UPDATE_DELIVERY_DATE));
.orElseThrow(CommonApplicationException.VOTE_POLICY_ONLY_AUTHOR_CAN_UPDATE_DELIVERY_DATE);

if (registeredVotePaper.id().equals(targetId)) return true;

throw new CommonApplicationException(VOTE_PAPER_NOT_FOUND);
throw CommonApplicationException.VOTE_PAPER_NOT_FOUND;
}

private boolean hasPermissionForVote(User principal, Serializable targetId) {
if (loadVotePort.loadVoteByVoterAndVotePaperId(principal.getUsername(), (Long) targetId).isPresent())
throw new CommonApplicationException(VOTE_POLICY_ONE_VOTE_PER_USER);
throw CommonApplicationException.VOTE_POLICY_ONE_VOTE_PER_USER;

return true;
}
Expand Down
Loading

0 comments on commit e5b2826

Please sign in to comment.