Skip to content

Commit

Permalink
Merge pull request #56 from Team-UMC/feature/#24/ErrorCodeSeparation
Browse files Browse the repository at this point in the history
[Refactor] ErrorCode 분리
  • Loading branch information
tnals2384 authored Feb 6, 2024
2 parents 18c22bc + c9dcf93 commit 07038b7
Show file tree
Hide file tree
Showing 59 changed files with 658 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.umc.networkingService.global.common.enums.Semester;
import com.umc.networkingService.global.common.exception.ErrorCode;
import com.umc.networkingService.global.common.exception.RestApiException;
import com.umc.networkingService.global.common.exception.code.BranchErrorCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -41,6 +42,6 @@ public static BranchInfo getBranchInfo(String name) {
return Arrays.stream(BranchInfo.values())
.filter(branchInfo -> branchInfo.getName().equals(name))
.findFirst()
.orElseThrow(() -> new RestApiException(ErrorCode.EMPTY_BRANCH));
.orElseThrow(() -> new RestApiException(BranchErrorCode.EMPTY_BRANCH));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.umc.networkingService.global.common.enums.Semester;
import com.umc.networkingService.global.common.exception.ErrorCode;
import com.umc.networkingService.global.common.exception.RestApiException;
import com.umc.networkingService.global.common.exception.code.UniversityErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
Expand Down Expand Up @@ -110,7 +111,7 @@ private void updateBranchUniversities(List<BranchUniversity> branchUniversities)

private University findUniversityByName(String name) {
return universityRepository.findByName(name)
.orElseThrow(() -> new RestApiException(ErrorCode.EMPTY_UNIVERSITY));
.orElseThrow(() -> new RestApiException(UniversityErrorCode.EMPTY_UNIVERSITY));
}

// 새로운 프로젝트를 추가하는 함수
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.member.repository.MemberRepository;
import com.umc.networkingService.global.common.exception.ErrorCode;
import com.umc.networkingService.global.common.exception.code.MemberErrorCode;
import com.umc.networkingService.global.common.exception.RestApiException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetailsService;
Expand All @@ -20,7 +20,7 @@ public class PrincipalDetailsService implements UserDetailsService {
@Override
public PrincipalDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Member memberEntity = memberRepository.findById(UUID.fromString(username))
.orElseThrow(() -> new RestApiException(ErrorCode.EMPTY_MEMBER));
.orElseThrow(() -> new RestApiException(MemberErrorCode.EMPTY_MEMBER));
return new PrincipalDetails(memberEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.umc.networkingService.config.security.auth.PrincipalDetails;
import com.umc.networkingService.config.security.auth.PrincipalDetailsService;
import com.umc.networkingService.global.common.exception.ErrorCode;
import com.umc.networkingService.global.common.exception.code.AuthErrorCode;
import com.umc.networkingService.global.common.exception.RestApiException;
import io.jsonwebtoken.*;
import jakarta.annotation.PostConstruct;
Expand Down Expand Up @@ -86,7 +86,7 @@ public Authentication getAuthentication(String token) {
return new UsernamePasswordAuthenticationToken(principalDetails,
"", principalDetails.getAuthorities());
} catch (UsernameNotFoundException exception) {
throw new RestApiException(ErrorCode.UNSUPPORTED_JWT);
throw new RestApiException(AuthErrorCode.UNSUPPORTED_JWT);
}
}

Expand All @@ -97,7 +97,7 @@ public Authentication getRefreshAuthentication(String token) {
return new UsernamePasswordAuthenticationToken(principalDetails,
"", principalDetails.getAuthorities());
} catch (UsernameNotFoundException exception) {
throw new RestApiException(ErrorCode.UNSUPPORTED_JWT);
throw new RestApiException(AuthErrorCode.UNSUPPORTED_JWT);
}
}

Expand All @@ -122,27 +122,27 @@ public boolean validateToken(String token) {
Jwts.parser().setSigningKey(jwtSecretKey).parseClaimsJws(token);
return true;
} catch (SecurityException | MalformedJwtException e) {
throw new RestApiException(ErrorCode.INVALID_ACCESS_TOKEN);
throw new RestApiException(AuthErrorCode.INVALID_ACCESS_TOKEN);
} catch (ExpiredJwtException e) {
throw new RestApiException(ErrorCode.EXPIRED_MEMBER_JWT);
throw new RestApiException(AuthErrorCode.EXPIRED_MEMBER_JWT);
} catch (UnsupportedJwtException | SignatureException e) {
throw new RestApiException(ErrorCode.UNSUPPORTED_JWT);
throw new RestApiException(AuthErrorCode.UNSUPPORTED_JWT);
} catch (IllegalArgumentException e) {
throw new RestApiException(ErrorCode.EMPTY_JWT);
throw new RestApiException(AuthErrorCode.EMPTY_JWT);
}
}
public boolean validateRefreshToken(String token) {
try {
Jwts.parser().setSigningKey(refreshSecretKey).parseClaimsJws(token);
return true;
} catch (SecurityException | MalformedJwtException e) {
throw new RestApiException(ErrorCode.INVALID_REFRESH_TOKEN);
throw new RestApiException(AuthErrorCode.INVALID_REFRESH_TOKEN);
} catch (ExpiredJwtException e) {
throw new RestApiException(ErrorCode.EXPIRED_MEMBER_JWT);
throw new RestApiException(AuthErrorCode.EXPIRED_MEMBER_JWT);
} catch (UnsupportedJwtException | SignatureException e) {
throw new RestApiException(ErrorCode.UNSUPPORTED_JWT);
throw new RestApiException(AuthErrorCode.UNSUPPORTED_JWT);
} catch (IllegalArgumentException e) {
throw new RestApiException(ErrorCode.EMPTY_JWT);
throw new RestApiException(AuthErrorCode.EMPTY_JWT);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class BoardCommentController {
@Operation(summary = "댓글 작성 API", description = "댓글을 작성하는 API입니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "BOARD002", description = "게시글을 찾을 수 없을 경우 발생")
})
@PostMapping
public BaseResponse<BoardCommentIdResponse> addBoardComment(@CurrentMember Member member,
Expand Down Expand Up @@ -109,6 +110,7 @@ public BaseResponse<MyBoardPagingResponse> showBoardsByMemberCommentsForApp(@Cur
"board: NOTICE, FREE, WORKBOOK, OB, QUESTION 중 하나의 값을 대문자로 주세요.")
@ApiResponses(value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "COMMON405", description = "host, board type이 적절하지 않은 값일 경우 발생")
})
@Parameters(value = {
@Parameter(name = "keyword", description = "keyword를 주지 않으면 모든 내가 댓글 쓴 글이 조회됩니다. keyword를 주면 검색이 가능합니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class BoardController {
" NOTICE는 해당 hostType 이하 운영진만 작성 가능합니다. WORKBOOK 게시판은 host : CAMPUS에서 교내 운영진만 작성 가능합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "COMMON402", description = "request 요소들의 validation 검증에 실패할 경우 발생"),
@ApiResponse(responseCode = "BOARD001", description = "WORKBOOK 게시판과 CENTER, BRANCH를 선택했을 경우 금지된 요청"),
@ApiResponse(responseCode = "BOARD003", description = "게시글을 작성할 권한이 없을 경우 발생"),
@ApiResponse(responseCode = "FILE001", description = "파일 S3 업로드 실패할 경우 발생")
Expand All @@ -63,6 +64,7 @@ public BaseResponse<BoardIdResponse> createBoard(@CurrentMember Member member,
"NOTICE는 해당 hostType 이하 운영진만 수정 가능합니다. WORKBOOK 게시판은 host : CAMPUS에서 교내 운영진만 수정 가능합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "COMMON402", description = "request 요소들의 validation 검증에 실패할 경우 발생"),
@ApiResponse(responseCode = "BOARD001", description = "WORKBOOK 게시판과 CENTER, BRANCH를 선택했을 경우 금지된 요청"),
@ApiResponse(responseCode = "BOARD002", description = "게시글을 찾을 수 없을 경우 발생"),
@ApiResponse(responseCode = "BOARD003", description = "게시글을 수정할 권한이 없을 경우 발생"),
Expand Down Expand Up @@ -95,7 +97,7 @@ public BaseResponse<BoardIdResponse> deleteBoard(@CurrentMember Member member,
"board: NOTICE, FREE, WORKBOOK, OB, QUESTION 중 하나의 값을 대문자로 주세요.")
@ApiResponses(value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "COMMON403", description = "hostType, boardType이 적절하지 않은 값일 경우 발생")
@ApiResponse(responseCode = "COMMON405", description = "host, board type이 적절하지 않은 값일 경우 발생")
})
@Parameters(value = {
@Parameter(name = "page", description = "page 시작은 0번부터, 내림차순으로 조회됩니다."),
Expand Down Expand Up @@ -161,7 +163,8 @@ public BaseResponse<MyBoardPagingResponse> showBoardsByMemberForApp(@CurrentMemb
"host: CENTER, BRANCH, CAMPUS 중 하나의 값을 대문자로 주세요. " +
"board: NOTICE, FREE, WORKBOOK, OB, QUESTION 중 하나의 값을 대문자로 주세요.")
@ApiResponses(value = {
@ApiResponse(responseCode = "COMMON200", description = "성공")
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "COMMON405", description = "host, board type이 적절하지 않은 값일 경우 발생")
})
@Parameters(value = {
@Parameter(name = "keyword", description = "keyword를 주지 않으면 모든 내가 쓴 게시글이 조회됩니다. keyword를 주면 검색이 가능합니다."),
Expand Down Expand Up @@ -199,7 +202,8 @@ public BaseResponse<MyBoardPagingResponse> showMemberBoardHeartForApp(@CurrentMe
"host: CENTER, BRANCH, CAMPUS 중 하나의 값을 대문자로 주세요. " +
"board: NOTICE, FREE, WORKBOOK, OB, QUESTION 중 하나의 값을 대문자로 주세요.")
@ApiResponses(value = {
@ApiResponse(responseCode = "COMMON200", description = "성공")
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "COMMON405", description = "host, board type이 적절하지 않은 값일 경우 발생")
})
@Parameters(value = {
@Parameter(name = "keyword", description = "keyword를 주지 않으면 내가 좋아요한 모든 글이 조회됩니다. keyword를 주면 검색이 가능합니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public class StaffBoardController {
@Operation(summary = "공지사항 목록 조회/검색 API", description = "공지사항 목록을 조회합니다. keyword를 주면 검색됩니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "BOARD003", description = "해당 hostType의 공지사항을 볼 권한이 없을 경우 발생"),
@ApiResponse(responseCode = "COMMON405", description = "host, board type이 적절하지 않은 값일 경우 발생"),
@ApiResponse(responseCode = "BOARD003", description = "해당 host type의 공지사항을 볼 권한이 없을 경우 발생"),
})
@Parameters(value = {
@Parameter(name = "hostType", description = "ALL, CENTER, BRANCH, CAMPUS 중 하나의 값을 대문자로 주세요."),
@Parameter(name = "host", description = "ALL, CENTER, BRANCH, CAMPUS 중 하나의 값을 대문자로 주세요."),
@Parameter(name = "keyword", description = "keyword를 주지 않으면 모든 교내 공지사항 글이 조회됩니다. keyword를 주면 검색이 가능합니다."),
@Parameter(name = "page", description = "page 시작은 0번부터, 내림차순으로 조회됩니다."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class BoardCreateRequest {
@NotNull(message = "CENTER, BRANCH, CAMPUS 중 하나를 선택해주세요.")
@ValidEnum(message = "옳지 않은 값입니다. CENTER, BRANCH, CAMPUS 중 하나를 선택해주세요.", enumClass = HostType.class)
@JsonProperty("host")
private HostType hostType;
private String hostType;

@NotNull(message = "게시판 구분을 선택해주세요.")
@ValidEnum(message = "옳지 않은 값입니다. NOTICE, FREE ,WORKBOOK, QUESTION, STAFF, OB 중 하나를 선택해주세요.",
enumClass = BoardType.class)
@JsonProperty("board")
private BoardType boardType;
private String boardType;

@NotBlank(message = "title은 필수 입력값입니다.")
private String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class BoardUpdateRequest {
@NotNull(message = "CENTER, BRANCH, CAMPUS 중 하나를 선택해주세요.")
@ValidEnum(message = "옳지 않은 값입니다. CENTER, BRANCH, CAMPUS 중 하나를 선택해주세요.", enumClass = HostType.class)
@JsonProperty("host")
private HostType hostType;
private String hostType;

@NotNull(message = "게시판 구분을 선택해주세요.")
@ValidEnum(message = "옳지 않은 값입니다. NOTICE, FREE ,WORKBOOK, QUESTION, STAFF, OB 중 하나를 선택해주세요.",
enumClass = BoardType.class)
@JsonProperty("board")
private BoardType boardType;
private String boardType;

@NotBlank(message = "title은 필수 입력값입니다.")
private String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public class Board extends BaseEntity {
private boolean isFixed;

public void update(BoardUpdateRequest request, List<Semester> semesters) {
this.hostType = request.getHostType();
this.boardType = request.getBoardType();
this.hostType = HostType.valueOf(request.getHostType());
this.boardType = BoardType.valueOf(request.getBoardType());
this.title = request.getTitle();
this.content = request.getContent();
this.semesterPermission = semesters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.umc.networkingService.domain.board.dto.response.notice.BoardNoticePageResponse;
import com.umc.networkingService.domain.board.dto.response.notice.BoardNoticePagingResponse;
import com.umc.networkingService.domain.board.entity.Board;
import com.umc.networkingService.domain.board.entity.BoardType;
import com.umc.networkingService.domain.board.entity.HostType;
import com.umc.networkingService.domain.board.service.BoardFileService;
import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.global.common.enums.Semester;
Expand All @@ -27,8 +29,8 @@ public Board toEntity(Member member, BoardCreateRequest request,
.writer(member)
.title(request.getTitle())
.content(request.getContent())
.hostType(request.getHostType())
.boardType(request.getBoardType())
.hostType(HostType.valueOf(request.getHostType()))
.boardType(BoardType.valueOf(request.getBoardType()))
.semesterPermission(semesterPermission)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import com.umc.networkingService.domain.board.mapper.BoardMapper;
import com.umc.networkingService.domain.board.repository.BoardCommentRepository;
import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.global.common.exception.ErrorCode;
import com.umc.networkingService.global.common.exception.RestApiException;
import com.umc.networkingService.global.common.exception.code.BoardCommentErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -53,7 +53,7 @@ public BoardCommentIdResponse updateBoardComment(Member member, UUID commentId,

//현재 로그인한 member와 writer가 같지 않으면 수정 권한 없음
if(!comment.getWriter().equals(member))
throw new RestApiException(ErrorCode.NO_AUTHORIZATION_BOARD_COMMENT);
throw new RestApiException(BoardCommentErrorCode.NO_AUTHORIZATION_BOARD_COMMENT);

comment.update(request);

Expand All @@ -69,7 +69,7 @@ public BoardCommentIdResponse deleteBoardComment(Member member, UUID commentId)

//현재 로그인한 member와 writer가 같지 않으면 삭제 권한 없음
if(!comment.getWriter().equals(member))
throw new RestApiException(ErrorCode.NO_AUTHORIZATION_BOARD_COMMENT);
throw new RestApiException(BoardCommentErrorCode.NO_AUTHORIZATION_BOARD_COMMENT);

board.decreaseCommentCount();
comment.delete();
Expand Down Expand Up @@ -99,6 +99,6 @@ public MyBoardCommentPagingWebResponse showBoardsByMemberCommentForWeb(Member me
@Override
public BoardComment loadEntity(UUID commentId) {
return boardCommentRepository.findById(commentId).orElseThrow(
() -> new RestApiException(ErrorCode.EMPTY_BOARD_COMMENT));
() -> new RestApiException(BoardCommentErrorCode.EMPTY_BOARD_COMMENT));
}
}
Loading

0 comments on commit 07038b7

Please sign in to comment.