Skip to content

Commit

Permalink
CustomException -> CustomApiException 으로 네이밍 변경
Browse files Browse the repository at this point in the history
- 기존 Exception -> ApiException

ResponseUtil
- jwt 토큰 expired 시, 혹은 유효하지 않은 접근시, response의 error 응답을 json 응답구현
  • Loading branch information
DEREVGEN committed Oct 1, 2023
1 parent d761bae commit f9ebf26
Show file tree
Hide file tree
Showing 41 changed files with 197 additions and 216 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package site.chagok.server.common.exception;

public class AuthorizationApiException extends CustomApiException {

public AuthorizationApiException(String code, String state) {
super(401, code, state);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package site.chagok.server.common.exception;

public class BoardNotFoundApiException extends NotFoundApiException {

public BoardNotFoundApiException() {
super("board_001", "cannot find board");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import site.chagok.server.common.dto.ErrorDto;

public class CustomException extends RuntimeException{
public class CustomApiException extends RuntimeException{

ErrorDto errorDto;

public CustomException(int status, String code, String state) {
public CustomApiException(int status, String code, String state) {
this.errorDto = new ErrorDto(status, code, state);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package site.chagok.server.common.exception;

public class NotFoundApiException extends CustomApiException {

public NotFoundApiException(String code, String state) {
super(404, code, state);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package site.chagok.server.common.exception;


public class UpdateInfoApiException extends CustomApiException {

public UpdateInfoApiException(String code, String state) {
super(400, code, state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import site.chagok.server.common.exception.AuthorizationException;
import site.chagok.server.common.exception.NotFoundException;
import site.chagok.server.member.exception.UpdateInfoException;
import site.chagok.server.common.exception.AuthorizationApiException;
import site.chagok.server.common.exception.NotFoundApiException;
import site.chagok.server.common.exception.UpdateInfoApiException;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(AuthorizationException.class)
public ResponseEntity<?> invalidMemberExceptionHandler(AuthorizationException e) {
@ExceptionHandler(AuthorizationApiException.class)
public ResponseEntity<?> invalidMemberExceptionHandler(AuthorizationApiException e) {
return ResponseEntity.status(e.getHttpStatus()).body(e.getErrorDto());
}

@ExceptionHandler(NotFoundException.class)
public ResponseEntity<?> notFoundExceptionHandler(NotFoundException e) {
@ExceptionHandler(NotFoundApiException.class)
public ResponseEntity<?> notFoundExceptionHandler(NotFoundApiException e) {
return ResponseEntity.status(e.getHttpStatus()).body(e.getErrorDto());
}

@ExceptionHandler(UpdateInfoException.class)
public ResponseEntity<?> updateInfoExceptionHandler(UpdateInfoException e) {
@ExceptionHandler(UpdateInfoApiException.class)
public ResponseEntity<?> updateInfoExceptionHandler(UpdateInfoApiException e) {
return ResponseEntity.status(e.getHttpStatus()).body(e.getErrorDto());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package site.chagok.server.contest.exception;

import site.chagok.server.common.exception.NotFoundApiException;

public class CommentNotFoundApiException extends NotFoundApiException {

public CommentNotFoundApiException() {
super("comment_01", "cannot find comment");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.AuthorizationServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.chagok.server.common.exception.BoardNotFoundException;
import site.chagok.server.common.exception.BoardNotFoundApiException;
import site.chagok.server.contest.dto.*;
import site.chagok.server.contest.exception.CommentNotFoundException;
import site.chagok.server.contest.exception.CommentNotFoundApiException;
import site.chagok.server.contest.repository.CommentRepository;
import site.chagok.server.contest.util.CommentSorter;
import site.chagok.server.contest.domain.Comment;
import site.chagok.server.contest.domain.Contest;
import site.chagok.server.contest.repository.ContestRepository;
import site.chagok.server.member.domain.Member;
import site.chagok.server.member.exception.InvalidMemberException;
import site.chagok.server.member.exception.InvalidMemberApiException;
import site.chagok.server.member.service.MemberCredentialService;

import javax.persistence.EntityNotFoundException;
import java.util.List;

@Service
Expand All @@ -31,7 +29,7 @@ public class ContestService {

@Transactional
public GetContestDto getContest(Long contestId){
Contest foundContest = contestRepository.findById(contestId).orElseThrow(BoardNotFoundException::new);
Contest foundContest = contestRepository.findById(contestId).orElseThrow(BoardNotFoundApiException::new);
foundContest.addViewCount();
return GetContestDto.builder()
.contestId(foundContest.getId())
Expand Down Expand Up @@ -65,7 +63,7 @@ public Page<GetContestPreviewDto> getContests(Pageable pageable){
// 사용자 공모전 스크랩 미리보기
@Transactional(readOnly = true)
public GetContestPreviewDto getContestPreview(Long contestId) {
Contest contest = contestRepository.findById(contestId).orElseThrow(BoardNotFoundException::new);
Contest contest = contestRepository.findById(contestId).orElseThrow(BoardNotFoundApiException::new);

// 공모전 스크랩 미리보기 dto 반환
return GetContestPreviewDto.builder()
Expand All @@ -83,7 +81,7 @@ public GetContestPreviewDto getContestPreview(Long contestId) {
// 공모전 글에 대한 댓글 조회
@Transactional(readOnly = true)
public List<GetContestCommentDto> getContestComments(Long contestId){
Contest contest = contestRepository.findContestByIdFetchCommentsAndMemberName(contestId).orElseThrow(BoardNotFoundException::new);
Contest contest = contestRepository.findContestByIdFetchCommentsAndMemberName(contestId).orElseThrow(BoardNotFoundApiException::new);

List<Comment> comments = contest.getComments();
return CommentSorter.getSort(comments);
Expand All @@ -93,7 +91,7 @@ public List<GetContestCommentDto> getContestComments(Long contestId){
// 댓글 추가
@Transactional
public Long makeComment(CommentDto commentDto){
Contest contest = contestRepository.findById(commentDto.getContestId()).orElseThrow(BoardNotFoundException::new);
Contest contest = contestRepository.findById(commentDto.getContestId()).orElseThrow(BoardNotFoundApiException::new);
// 로그인 사용자 조회
Member member = credentialService.getMember();

Expand Down Expand Up @@ -124,10 +122,10 @@ public Long updateComment(CommentUpdateDto commentUpdateDto) {
Member member = credentialService.getMember();

// 댓글 조회
Comment comment = commentRepository.findById(commentUpdateDto.getCommentId()).orElseThrow(CommentNotFoundException::new);
Comment comment = commentRepository.findById(commentUpdateDto.getCommentId()).orElseThrow(CommentNotFoundApiException::new);

if (member.getNickName() != comment.getMember().getNickName())
throw new InvalidMemberException();
throw new InvalidMemberApiException();

comment.updateComment(commentUpdateDto.getContent(), commentUpdateDto.getKakaoRef());

Expand All @@ -142,11 +140,11 @@ public Long deleteComment(Long commentId) {
Member member = credentialService.getMember();

// 댓글 조회
Comment comment = commentRepository.findById(commentId).orElseThrow(CommentNotFoundException::new);
Comment comment = commentRepository.findById(commentId).orElseThrow(CommentNotFoundApiException::new);
comment.getContest().minusCommentCount();

if (member.getNickName() != comment.getMember().getNickName())
throw new InvalidMemberException();
throw new InvalidMemberApiException();

comment.setDeleted();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.chagok.server.member.constants.ActionType;
import site.chagok.server.member.dto.BoardScrapDto;
import site.chagok.server.member.exception.NickNameExistsException;
import site.chagok.server.member.service.MemberImgService;
import site.chagok.server.member.service.MemberUpdateService;

import javax.persistence.EntityNotFoundException;
import java.io.IOException;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package site.chagok.server.member.exception;

import site.chagok.server.common.exception.NotFoundApiException;

public class ImgFileNotFoundApiException extends NotFoundApiException {

public ImgFileNotFoundApiException() {
super("image_01", "cannot get profile image");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package site.chagok.server.member.exception;

import site.chagok.server.common.exception.AuthorizationApiException;

public class InvalidMemberApiException extends AuthorizationApiException {
public InvalidMemberApiException() {
super("invalid_01", "invalid member");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package site.chagok.server.member.exception;

import site.chagok.server.common.exception.NotFoundApiException;

public class MemberNotFoundApiException extends NotFoundApiException {

public MemberNotFoundApiException() {
super("member_01", "cannot find member");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package site.chagok.server.member.exception;

import site.chagok.server.common.exception.UpdateInfoApiException;

public class NickNameExistsApiException extends UpdateInfoApiException {

public NickNameExistsApiException() {
super("nickname_01", "nickname already exists");
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package site.chagok.server.member.exception;

import site.chagok.server.common.exception.NotFoundApiException;

public class ScrapNotFoundApiException extends NotFoundApiException {

public ScrapNotFoundApiException() {
super("scrap_01", "cannot find board scrap data");
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit f9ebf26

Please sign in to comment.