Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[016-HelpMS-리포지토리-구조변경]JPAException을 인프라로 옮기기 위해 SpringJPA를 상속받는 JPAR… #36

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.checkinrequestMS.HelpAPI.domain.exceptions;
package com.example.checkinrequestMS.HelpAPI.domain.exceptions.help;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.checkinrequestMS.HelpAPI.domain.exceptions;
package com.example.checkinrequestMS.HelpAPI.domain.exceptions.help;

import com.example.checkinrequestMS.common.exception.types.DomainException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.example.checkinrequestMS.HelpAPI.domain.exceptions;
package com.example.checkinrequestMS.HelpAPI.domain.exceptions.progress;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum ProgressErrorCode {
;
NO_PROGRESS("진행 정보가 존재하지 않습니다.");

private final String detail;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.checkinrequestMS.HelpAPI.domain.exceptions.progress;

import com.example.checkinrequestMS.common.exception.types.DomainException;

public class ProgressException extends DomainException {

public ProgressException(ProgressErrorCode errorCode) {
super(errorCode.getDetail());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.example.checkinrequestMS.HelpAPI.domain.entities.help.child.LineUp;
import com.example.checkinrequestMS.HelpAPI.infra.db.help.LineUpJPARepository;
import com.example.checkinrequestMS.HelpAPI.infra.exceptions.JPAException;
import com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions.JPAException;
import com.example.checkinrequestMS.PlaceAPI.domain.Place;
import com.example.checkinrequestMS.PlaceAPI.domain.exceptions.place.PlaceException;
import com.example.checkinrequestMS.PlaceAPI.infra.PlaceRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.example.checkinrequestMS.HelpAPI.infra.exceptions.JPAErrorCode.ERROR_SAVING;
import static com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions.JPAErrorCode.ERROR_SAVE;
import static com.example.checkinrequestMS.PlaceAPI.domain.exceptions.place.PlaceErrorCode.NO_PLACE_INFO;

@Service
Expand All @@ -24,14 +24,11 @@ public class LineUpCRUDService {
public void registerLineUp(LineUp lineUp) {
Place place = placeRepository.findById(lineUp.getPlaceId())
.orElseThrow(() -> new PlaceException(NO_PLACE_INFO));
lineUp.setPlaceWithFullInfo(place);
//lineUp.setPlaceWithFullInfo(place);
lineUp.setLineUpTitle(place);

try {
lineUpJPARepository.save(lineUp);
} catch (Exception e) {
throw new JPAException(ERROR_SAVING);
}
lineUpJPARepository.save(lineUp);

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.example.checkinrequestMS.HelpAPI.domain.service.LineUp;

import com.example.checkinrequestMS.HelpAPI.domain.entities.help.child.LineUp;
import com.example.checkinrequestMS.HelpAPI.domain.exceptions.HelpException;
import com.example.checkinrequestMS.HelpAPI.domain.exceptions.help.HelpException;
import com.example.checkinrequestMS.HelpAPI.infra.db.help.LineUpJPARepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.example.checkinrequestMS.HelpAPI.domain.exceptions.HelpErrorCode.NO_LINE_UP_INFO;
import static com.example.checkinrequestMS.HelpAPI.domain.exceptions.help.HelpErrorCode.NO_LINE_UP_INFO;

@Service
@RequiredArgsConstructor
Expand All @@ -18,7 +18,6 @@ public class LineUpSelectService {
@Transactional
public LineUp selectLineUp(Long id) {
return lineUpJPARepository.findById(id).orElseThrow(
() -> new HelpException(NO_LINE_UP_INFO)
);
() -> new HelpException(NO_LINE_UP_INFO));
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.example.checkinrequestMS.HelpAPI.domain.service.checkIn;

import com.example.checkinrequestMS.HelpAPI.domain.entities.help.child.CheckIn;
import com.example.checkinrequestMS.HelpAPI.domain.entities.progress.Progress;
import com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions.JPAErrorCode;
import com.example.checkinrequestMS.HelpAPI.infra.db.help.CheckInJPARepository;
import com.example.checkinrequestMS.HelpAPI.infra.exceptions.JPAException;
import com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions.JPAException;
import com.example.checkinrequestMS.PlaceAPI.domain.Place;
import com.example.checkinrequestMS.PlaceAPI.domain.exceptions.place.PlaceException;
import com.example.checkinrequestMS.PlaceAPI.infra.PlaceRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.example.checkinrequestMS.HelpAPI.infra.exceptions.JPAErrorCode.ERROR_SAVING;

import static com.example.checkinrequestMS.PlaceAPI.domain.exceptions.place.PlaceErrorCode.NO_PLACE_INFO;

@Service
Expand All @@ -26,10 +26,6 @@ public void registerCheckIn(CheckIn checkIn) {
.orElseThrow(() -> new PlaceException(NO_PLACE_INFO));
checkIn.setCheckInTitle(place);

try {
checkInJPARepository.save(checkIn);
} catch (Exception e) {
throw new JPAException(ERROR_SAVING);
}
checkInJPARepository.save(checkIn);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.example.checkinrequestMS.HelpAPI.domain.service.checkIn;

import com.example.checkinrequestMS.HelpAPI.domain.entities.help.child.CheckIn;
import com.example.checkinrequestMS.HelpAPI.domain.exceptions.HelpException;
import com.example.checkinrequestMS.HelpAPI.domain.exceptions.help.HelpException;
import com.example.checkinrequestMS.HelpAPI.infra.db.help.CheckInJPARepository;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.example.checkinrequestMS.HelpAPI.domain.exceptions.HelpErrorCode.NO_CHECK_IN_INFO;
import static com.example.checkinrequestMS.HelpAPI.domain.exceptions.help.HelpErrorCode.NO_CHECK_IN_INFO;

@Service
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.example.checkinrequestMS.HelpAPI.domain.service.etc;

import com.example.checkinrequestMS.HelpAPI.domain.entities.help.child.Etc;
import com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions.JPAErrorCode;
import com.example.checkinrequestMS.HelpAPI.infra.db.help.EtcJPARepository;
import com.example.checkinrequestMS.HelpAPI.infra.exceptions.JPAException;
import com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions.JPAException;
import com.example.checkinrequestMS.PlaceAPI.domain.Place;
import com.example.checkinrequestMS.PlaceAPI.domain.exceptions.place.PlaceException;
import com.example.checkinrequestMS.PlaceAPI.infra.PlaceRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.example.checkinrequestMS.HelpAPI.infra.exceptions.JPAErrorCode.ERROR_SAVING;

import static com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions.JPAErrorCode.ERROR_SAVE;
import static com.example.checkinrequestMS.PlaceAPI.domain.exceptions.place.PlaceErrorCode.NO_PLACE_INFO;

@Service
Expand All @@ -21,14 +23,11 @@ public class EtcCRUDService {

@Transactional
public void registerEtc(Etc etc) {
Place place = placeRepository.findById(etc.getPlace().getId())
Place place = placeRepository.findById(etc.getPlaceId())
.orElseThrow(() -> new PlaceException(NO_PLACE_INFO));
etc.setPlaceWithFullInfo(place);
//etc.setPlaceWithFullInfo(place);

etcJPARepository.save(etc);

try {
etcJPARepository.save(etc);
} catch (Exception e) {
throw new JPAException(ERROR_SAVING);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.example.checkinrequestMS.HelpAPI.domain.service.etc;

import com.example.checkinrequestMS.HelpAPI.domain.entities.help.child.Etc;
import com.example.checkinrequestMS.HelpAPI.domain.exceptions.HelpErrorCode;
import com.example.checkinrequestMS.HelpAPI.domain.exceptions.HelpException;
import com.example.checkinrequestMS.HelpAPI.domain.exceptions.help.HelpException;
import com.example.checkinrequestMS.HelpAPI.infra.db.help.EtcJPARepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.example.checkinrequestMS.HelpAPI.domain.exceptions.HelpErrorCode.No_ETC_INFO;
import static com.example.checkinrequestMS.HelpAPI.domain.exceptions.help.HelpErrorCode.No_ETC_INFO;

@Service
@RequiredArgsConstructor
Expand All @@ -21,6 +20,5 @@ public Etc selectEtc(Long id) {
return etcJPARepository.findById(id).orElseThrow(
() -> new HelpException(No_ETC_INFO)
);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import com.example.checkinrequestMS.HelpAPI.domain.entities.help.Help;
import com.example.checkinrequestMS.HelpAPI.domain.entities.progress.Progress;
import com.example.checkinrequestMS.HelpAPI.domain.exceptions.HelpException;
import com.example.checkinrequestMS.HelpAPI.domain.exceptions.help.HelpException;

import com.example.checkinrequestMS.HelpAPI.domain.exceptions.progress.ProgressException;
import com.example.checkinrequestMS.HelpAPI.infra.db.help.HelpJPARepository;
import com.example.checkinrequestMS.HelpAPI.infra.db.progress.ProgressJPARepository;
import com.example.checkinrequestMS.HelpAPI.infra.exceptions.JPAException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.example.checkinrequestMS.HelpAPI.domain.exceptions.HelpErrorCode.NO_HELP_INFO;
import static com.example.checkinrequestMS.HelpAPI.infra.exceptions.JPAErrorCode.ERROR_SAVING;
import static com.example.checkinrequestMS.HelpAPI.domain.exceptions.help.HelpErrorCode.NO_HELP_INFO;
import static com.example.checkinrequestMS.HelpAPI.domain.exceptions.progress.ProgressErrorCode.NO_PROGRESS;

@Service
@RequiredArgsConstructor
Expand All @@ -23,15 +24,13 @@ public class ProgressCRUDService {
@Transactional
public Progress registerProgress(Progress progress) {
Help help = helpJPARepository.findById(progress.getHelp().getId()).orElseThrow(
() -> new HelpException(NO_HELP_INFO)
);
() -> new HelpException(NO_HELP_INFO));
progress.setHelp(help);
//todo: 내부 요청으로 MemberID
progressJPARepository.save(progress);

try{
return progressJPARepository.save(progress);
}catch (Exception e) {
throw new JPAException(ERROR_SAVING);
}
return progressJPARepository.findById(progress.getId()).orElseThrow(
() -> new ProgressException(NO_PROGRESS));
}

//사진파일저장, 인증 변화.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.checkinrequestMS.HelpAPI.infra.aop.aspect;


import com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions.JPAException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import static com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions.JPAErrorCode.*;

@Component
@Aspect
public class JPAExceptionAspect {

@Around("@annotation(JPARead)")
public Object handleJPAReadException(ProceedingJoinPoint joinPoint) throws Throwable {
try {
return joinPoint.proceed();
} catch (Exception e) {
throw new JPAException(ERROR_READ, e);

}
}

@Around("@annotation(JPASave)")
public Object handleJPASaveException(ProceedingJoinPoint joinPoint) throws Throwable {
try {
return joinPoint.proceed();
} catch (Exception e) {
throw new JPAException(ERROR_SAVE, e);
}
}
Comment on lines +26 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exception은 catch하지만 throwable은 그대로 throws 하고 있군요. catch에서 Throwable를 잡아야 모든 예외에 대해 JPAExcpetion으로 래핑되지 않을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다. 수정하도록 하겠습니다! (이슈)


@Around("@annotation(JPAUpdate)")
public Object handleJPAUpdateException(ProceedingJoinPoint joinPoint) throws Throwable {
try {
return joinPoint.proceed();
} catch (Exception e) {
throw new JPAException(ERROR_UPDATE, e);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.checkinrequestMS.HelpAPI.infra.aop.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JPARead {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.checkinrequestMS.HelpAPI.infra.aop.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JPASave {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.checkinrequestMS.HelpAPI.infra.aop.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JPAUpdate {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum JPAErrorCode {
ERROR_SAVE("JPA 저장 작업 중 에러"),
ERROR_UPDATE("JPA 업데이트 작업 중 에러"),
ERROR_READ("JPA 조회 작업 중 에러");
Comment on lines +8 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 jpaException을 만드신 이유는 어떤 작업을 하다가 발생했는지에 대한 메시지를 다르게 처리해주고 싶으셨던 것 같군요!
운영 환경에서는

  1. 보통 유저는 개발 레벨의 상세한 에러에 대한 관심이 없고 (서비스에 문제가 발생했습니다. 잠시후 시도해주세요 정도로도 보통 충분하니까요)
  2. 시스템 어디에서 문제가 발생했는지 이 정보를 악용하는 유저가 있을 수도 있어서
    상세한 메시지는 운영환경에서는 보여주지 않는게 좋을 것 같습니다.

하지만 개발 환경에서는 어디서 에러가 발생했는지 알게되면 도움이 될 수 있을 것 같기도 하네요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다. 개발자들에게만 메시지를 줄 수 있게 추후 HOTFIX로 업데이트 해 보겠습니다. (이슈)



private final String detail;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.checkinrequestMS.HelpAPI.infra.aop.exceptions;

import com.example.checkinrequestMS.common.exception.types.InfraException;

public class JPAException extends InfraException {

public JPAException(JPAErrorCode error) {
super(error.getDetail());
}

public JPAException(JPAErrorCode error, Exception e) {
super(error.getDetail(), e);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package com.example.checkinrequestMS.HelpAPI.infra.db.help;

import com.example.checkinrequestMS.HelpAPI.domain.entities.help.child.CheckIn;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.checkinrequestMS.HelpAPI.infra.aop.aspect.JPARead;
import com.example.checkinrequestMS.HelpAPI.infra.aop.aspect.JPASave;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class CheckInJPARepository {
private final CheckInSpringJPARepository checkInSpringJPARepository;

@JPASave
public void save(CheckIn checkIn) {
checkInSpringJPARepository.save(checkIn);
}
@JPARead
public Optional<CheckIn> findById(Long id){
return checkInSpringJPARepository.findById(id);
}

public interface CheckInJPARepository extends JpaRepository<CheckIn, Long> {
}
Loading