Skip to content

Commit

Permalink
refactor: 에러 코드 재정의 (#227)
Browse files Browse the repository at this point in the history
* refactor: 에러 코드 재정의

Co-authored-by: 3juhwan <[email protected]>

* fix: 변수를 받는 예외 메세지 수정

Co-authored-by: 3juhwan <[email protected]>

---------

Co-authored-by: kunsanglee <[email protected]>
Co-authored-by: Arachne <[email protected]>
  • Loading branch information
3 people authored Aug 8, 2024
1 parent 79bd41c commit 63c93df
Show file tree
Hide file tree
Showing 29 changed files with 156 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ActionService {

public List<MemberBillReportAppResponse> getMemberBillReports(String token) {
Event event = eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_EVENT));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));
List<BillAction> billActions = billActionRepository.findByAction_Event(event);
List<MemberAction> memberActions = memberActionRepository.findAllByEvent(event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import server.haengdong.domain.TokenProvider;
import server.haengdong.exception.AuthenticationException;
import server.haengdong.exception.HaengdongErrorCode;

public class AuthService {

Expand All @@ -30,7 +31,7 @@ public String findEventIdByToken(String token) {

private void validateToken(String token) {
if (!tokenProvider.validateToken(token)) {
throw new AuthenticationException();
throw new AuthenticationException(HaengdongErrorCode.TOKEN_INVALID);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private Action createStartAction(Event event) {
@Transactional
public void updateBillAction(String token, Long actionId, BillActionUpdateAppRequest request) {
BillAction billAction = billActionRepository.findByAction_Id(actionId)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_BILL_ACTION));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.BILL_ACTION_NOT_FOUND));

validateToken(token, billAction);

Expand All @@ -56,19 +56,19 @@ public void updateBillAction(String token, Long actionId, BillActionUpdateAppReq
private void validateToken(String token, BillAction billAction) {
Event event = billAction.getEvent();
if (event.isTokenMismatch(token)) {
throw new HaengdongException(HaengdongErrorCode.NOT_FOUND_BILL_ACTION);
throw new HaengdongException(HaengdongErrorCode.BILL_ACTION_NOT_FOUND);
}
}

private Event getEvent(String eventToken) {
return eventRepository.findByToken(eventToken)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_EVENT));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));
}

@Transactional
public void deleteBillAction(String token, Long actionId) {
Event event = eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_EVENT));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));

billActionRepository.deleteByAction_EventAndActionId(event, actionId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@ public void updateMember(String token, String memberName, MemberUpdateAppRequest
private void validateMemberNameUnique(Event event, String updatedMemberName) {
boolean isMemberNameExist = memberActionRepository.existsByAction_EventAndMemberName(event, updatedMemberName);
if (isMemberNameExist) {
throw new HaengdongException(HaengdongErrorCode.DUPLICATED_MEMBER_NAME);
throw new HaengdongException(HaengdongErrorCode.MEMBER_NAME_DUPLICATE);
}
}

public void validatePassword(EventLoginAppRequest request) throws HaengdongException {
Event event = getEvent(request.token());
if (event.isSamePassword(request.password())) {
throw new AuthenticationException();
throw new AuthenticationException(HaengdongErrorCode.PASSWORD_INVALID);
}
}

private Event getEvent(String token) {
return eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_EVENT));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server.haengdong.application;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -48,7 +47,7 @@ private void validateMemberNames(MemberActionsSaveAppRequest request) {

long uniqueCount = memberNames.stream().distinct().count();
if (uniqueCount != memberNames.size()) {
throw new HaengdongException(HaengdongErrorCode.DUPLICATED_MEMBER_ACTION);
throw new HaengdongException(HaengdongErrorCode.MEMBER_NAME_DUPLICATE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,24 @@ public List<CurrentMemberAppResponse> getCurrentMembers(String token) {

private Event findEvent(String token) {
return eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_EVENT));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));
}

public void deleteMember(String token, String memberName) {
Event event = eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_EVENT));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));

memberActionRepository.deleteAllByEventAndMemberName(event, memberName);
}

@Transactional
public void deleteMemberAction(String token, Long actionId) {
Event event = eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_EVENT));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));
Action action = actionRepository.findByIdAndEvent(actionId, event)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_ACTION));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.ACTION_NOT_FOUND));
MemberAction memberAction = memberActionRepository.findByAction(action)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.NOT_FOUND_MEMBER_ACTION));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.MEMBER_ACTION_NOT_FOUND));

memberActionRepository.deleteAllByMemberNameAndMinSequence(memberAction.getMemberName(),
memberAction.getSequence());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.web.servlet.HandlerInterceptor;
import server.haengdong.application.AuthService;
import server.haengdong.exception.AuthenticationException;
import server.haengdong.exception.HaengdongErrorCode;
import server.haengdong.infrastructure.auth.AuthenticationExtractor;

@Slf4j
Expand Down Expand Up @@ -39,7 +40,7 @@ private void validateToken(HttpServletRequest request) {
String tokenEventId = authService.findEventIdByToken(token);
String eventId = request.getRequestURI().split("/")[3];
if (!tokenEventId.equals(eventId)) {
throw new AuthenticationException();
throw new AuthenticationException(HaengdongErrorCode.FORBIDDEN);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
@Entity
public class BillAction implements Comparable<BillAction> {

private static final int MIN_TITLE_LENGTH = 2;
private static final int MAX_TITLE_LENGTH = 30;
private static final long MIN_PRICE = 1L;
private static final long MAX_PRICE = 10_000_000L;
public static final int MIN_TITLE_LENGTH = 1;
public static final int MAX_TITLE_LENGTH = 30;
public static final long MIN_PRICE = 1L;
public static final long MAX_PRICE = 10_000_000L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -53,15 +53,13 @@ private BillAction(Long id, Action action, String title, Long price) {
private void validateTitle(String title) {
int titleLength = title.trim().length();
if (titleLength < MIN_TITLE_LENGTH || titleLength > MAX_TITLE_LENGTH) {
throw new HaengdongException(HaengdongErrorCode.BAD_REQUEST,
String.format("앞뒤 공백을 제거한 지출 내역 제목은 %d ~ %d자여야 합니다.", MIN_TITLE_LENGTH, MAX_TITLE_LENGTH));
throw new HaengdongException(HaengdongErrorCode.BILL_ACTION_TITLE_INVALID);
}
}

private void validatePrice(Long price) {
if (price < MIN_PRICE || price > MAX_PRICE) {
throw new HaengdongException(HaengdongErrorCode.BAD_REQUEST,
String.format("지출 금액은 %,d 이하의 자연수여야 합니다.", MAX_PRICE));
throw new HaengdongException(HaengdongErrorCode.BILL_ACTION_PRICE_INVALID);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public CurrentMembers addMemberAction(MemberAction memberAction) {

public void validate(String memberName, MemberActionStatus memberActionStatus) {
if (memberActionStatus == MemberActionStatus.IN && members.contains(memberName)) {
throw new HaengdongException(HaengdongErrorCode.INVALID_MEMBER_IN_ACTION);
throw new HaengdongException(HaengdongErrorCode.MEMBER_ALREADY_EXIST);
}
if (memberActionStatus == MemberActionStatus.OUT && !members.contains(memberName)) {
throw new HaengdongException(HaengdongErrorCode.INVALID_MEMBER_OUT_ACTION);
throw new HaengdongException(HaengdongErrorCode.MEMBER_NOT_EXIST);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import server.haengdong.exception.HaengdongErrorCode;
import server.haengdong.exception.HaengdongException;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class MemberAction implements Comparable<MemberAction> {

public static final int MIN_NAME_LENGTH = 1;
public static final int MAX_NAME_LENGTH = 4;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -33,13 +38,22 @@ public class MemberAction implements Comparable<MemberAction> {
private Long memberGroupId;

public MemberAction(Action action, String memberName, MemberActionStatus status, Long memberGroupId) {
validateMemberName(memberName);
this.action = action;
this.memberName = memberName;
this.status = status;
this.memberGroupId = memberGroupId;
}

private void validateMemberName(String memberName) {
int memberLength = memberName.length();
if (memberLength < MIN_NAME_LENGTH || memberLength > MAX_NAME_LENGTH) {
throw new HaengdongException(HaengdongErrorCode.MEMBER_NAME_LENGTH_INVALID);
}
}

public void updateMemberName(String memberName) {
validateMemberName(memberName);
this.memberName = memberName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static MemberActionStatus of(String status) {
return Arrays.stream(MemberActionStatus.values())
.filter(s -> s.name().equals(status))
.findFirst()
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.BAD_REQUEST,
"존재하지 않는 인원 변동 액션입니다."));
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.MEMBER_ACTION_STATUS_INVALID,
String.format(HaengdongErrorCode.MEMBER_ACTION_STATUS_INVALID.getMessage(), status)));
}
}
15 changes: 8 additions & 7 deletions server/src/main/java/server/haengdong/domain/event/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
@Entity
public class Event {

private static final int MIN_NAME_LENGTH = 2;
private static final int MAX_NAME_LENGTH = 20;
public static final int MIN_NAME_LENGTH = 1;
public static final int MAX_NAME_LENGTH = 20;
public static final int PASSWORD_LENGTH = 4;
private static final String SPACES = " ";
private static final Pattern PASSWORD_PATTERN = Pattern.compile("^\\d{4}$");
private static final Pattern PASSWORD_PATTERN = Pattern.compile(String.format("^\\d{%d}$", PASSWORD_LENGTH));

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -41,24 +42,24 @@ public Event(String name, String password, String token) {
}

private void validateName(String name) {
int nameLength = name.length();
int nameLength = name.trim().length();
if (nameLength < MIN_NAME_LENGTH || MAX_NAME_LENGTH < nameLength) {
throw new HaengdongException(HaengdongErrorCode.BAD_REQUEST,
throw new HaengdongException(HaengdongErrorCode.EVENT_NAME_LENGTH_INVALID,
String.format("행사 이름은 %d자 이상 %d자 이하만 입력 가능합니다. 입력한 이름 길이 : %d",
MIN_NAME_LENGTH,
MAX_NAME_LENGTH,
name.length()));
}
if (isBlankContinuous(name)) {
throw new HaengdongException(HaengdongErrorCode.BAD_REQUEST,
throw new HaengdongException(HaengdongErrorCode.EVENT_NAME_CONSECUTIVE_SPACES,
String.format("행사 이름에는 공백 문자가 연속될 수 없습니다. 입력한 이름 : %s", name));
}
}

private void validatePassword(String password) {
Matcher matcher = PASSWORD_PATTERN.matcher(password);
if (!matcher.matches()) {
throw new HaengdongException(HaengdongErrorCode.BAD_REQUEST, "비밀번호는 4자리 숫자만 가능합니다.");
throw new HaengdongException(HaengdongErrorCode.EVENT_PASSWORD_FORMAT_INVALID, "비밀번호는 4자리 숫자만 가능합니다.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package server.haengdong.exception;

import lombok.Getter;

@Getter
public class AuthenticationException extends RuntimeException {

private final HaengdongErrorCode errorCode;
private final String message;

public AuthenticationException(HaengdongErrorCode errorCode) {
this(errorCode, errorCode.getMessage());
}

public AuthenticationException(HaengdongErrorCode errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package server.haengdong.exception;

public record ErrorResponse(
String code,
HaengdongErrorCode errorCode,
String message
) {

public static ErrorResponse of(HaengdongErrorCode errorCode) {
return new ErrorResponse(errorCode.getCode(), errorCode.getMessage());
return new ErrorResponse(errorCode, errorCode.getMessage());
}

public static ErrorResponse of(HaengdongErrorCode errorCode, String message){
return new ErrorResponse(errorCode.getCode(), message);
public static ErrorResponse of(HaengdongErrorCode errorCode, String message) {
return new ErrorResponse(errorCode, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
public class GlobalExceptionHandler {

@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<ErrorResponse> authenticationException() {
public ResponseEntity<ErrorResponse> authenticationException(AuthenticationException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(ErrorResponse.of(HaengdongErrorCode.UNAUTHORIZED));
.body(ErrorResponse.of(e.getErrorCode()));
}

@ExceptionHandler({HttpRequestMethodNotSupportedException.class, NoResourceFoundException.class})
Expand All @@ -41,7 +41,7 @@ public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(Metho
.collect(Collectors.joining(", "));

return ResponseEntity.badRequest()
.body(ErrorResponse.of(HaengdongErrorCode.BAD_REQUEST, errorMessage));
.body(ErrorResponse.of(HaengdongErrorCode.REQUEST_EMPTY, errorMessage));
}

@ExceptionHandler(HaengdongException.class)
Expand Down
Loading

0 comments on commit 63c93df

Please sign in to comment.