Skip to content

Commit

Permalink
REFACTOR(exception) :: custom errorcode 삭제 및 spring 제공 Status 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
Woongbin06 committed Nov 9, 2024
1 parent f4e8ba1 commit 004e408
Show file tree
Hide file tree
Showing 65 changed files with 177 additions and 284 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-ui:1.7.0'
Expand All @@ -35,7 +34,6 @@ dependencies {
testRuntimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public class ApplicationFacade {
private final ApplicationRepository applicationRepository;

public Application getApplication(Long id) {
return applicationRepository.findById(id).orElseThrow(() -> ApplicationNotFoundException.EXCEPTION);
return applicationRepository.findById(id).orElseThrow(() -> new ApplicationNotFoundException(id));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.woongeya.zoing.domain.application.exception;

import com.woongeya.zoing.global.error.exception.ErrorCode;
import com.woongeya.zoing.global.error.JJoingException;
import org.springframework.http.HttpStatus;

public class AlreadyApplicationException extends JJoingException {
import com.woongeya.zoing.global.exception.JJoingException;

public static final JJoingException EXCEPTION = new AlreadyApplicationException();
public class AlreadyApplicationException extends JJoingException {

public AlreadyApplicationException() {
super(ErrorCode.ALREADY_APPLICATION);
public AlreadyApplicationException(Long projectId) {
super(HttpStatus.BAD_REQUEST, String.format("%s는 이미 신청한 프로젝트입니다.", projectId));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.woongeya.zoing.domain.application.exception;

import com.woongeya.zoing.global.error.exception.ErrorCode;
import com.woongeya.zoing.global.error.JJoingException;
import org.springframework.http.HttpStatus;

public class ApplicationNotFoundException extends JJoingException {
import com.woongeya.zoing.global.exception.JJoingException;

public static final JJoingException EXCEPTION = new ApplicationNotFoundException();
public class ApplicationNotFoundException extends JJoingException {

public ApplicationNotFoundException () {
super(ErrorCode.APPLICATION_NOT_FOUND);
public ApplicationNotFoundException (Long id) {
super(HttpStatus.NOT_FOUND, String.format("%s의 아이디를 가진 신청을 찾을 수 없습니다.", id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void execute(Long id) {
Application application = applicationFacade.getApplication(id);
Project project = projectFacade.getProject(application.getProjectId());
Member member = memberRepository.findByUserIdAndProjectId(writer.getId(), project.getId())
.orElseThrow(() -> MemberNotFoundException.EXCEPTION);
.orElseThrow(MemberNotFoundException::new);

member.acceptApplication(application);
memberRepository.save(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.woongeya.zoing.domain.application.service;

import com.woongeya.zoing.domain.application.domain.Application;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woongeya.zoing.domain.application.domain.repository.ApplicationRepository;
import com.woongeya.zoing.domain.application.domain.type.ApplicationState;
import com.woongeya.zoing.domain.application.exception.AlreadyApplicationException;
import com.woongeya.zoing.domain.application.presetation.dto.request.ApplicationCreateRequest;
import com.woongeya.zoing.domain.auth.repository.AuthRepository;
Expand All @@ -16,9 +17,8 @@
import com.woongeya.zoing.domain.project.facade.ProjectFacade;
import com.woongeya.zoing.domain.user.UserFacade;
import com.woongeya.zoing.domain.user.domain.User;

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

@Service
@RequiredArgsConstructor
Expand All @@ -40,7 +40,7 @@ public void execute(ApplicationCreateRequest request, Long id) {
User writer = userFacade.getUserById(member.getUserId());

applicationRepository.findByUserIdAndProjectId(user.getId(), project.getId())
.ifPresent(application -> { throw new AlreadyApplicationException(); });
.ifPresent(application -> { throw new AlreadyApplicationException(project.getId());});

Long applicationId = applicationRepository.save(
request.toEntity(user, project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class FindProjectApplicationService {
public List<ApplicationResponse> execute(Long id) {
User user = authRepository.getCurrentUser();
Member member = memberRepository.findByUserIdAndProjectId(user.getId(), id)
.orElseThrow(() -> MemberNotFoundException.EXCEPTION);
.orElseThrow(MemberNotFoundException::new);

if (!member.isWriter()) {
throw new IsNotWriterException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.woongeya.zoing.domain.application.service;

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

import com.woongeya.zoing.domain.application.ApplicationFacade;
import com.woongeya.zoing.domain.application.domain.Application;
import com.woongeya.zoing.domain.application.domain.repository.ApplicationRepository;
Expand All @@ -12,11 +15,9 @@
import com.woongeya.zoing.domain.project.domain.repository.MemberRepository;
import com.woongeya.zoing.domain.project.exception.MemberNotFoundException;
import com.woongeya.zoing.domain.project.facade.ProjectFacade;
import com.woongeya.zoing.domain.user.UserFacade;
import com.woongeya.zoing.domain.user.domain.User;

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

@Service
@RequiredArgsConstructor
Expand All @@ -34,7 +35,7 @@ public void execute(Long id) {
User user = authRepository.getCurrentUser();
Application application = applicationFacade.getApplication(id);
Member member = memberRepository.findByUserIdAndProjectId(user.getId(), application.getProjectId())
.orElseThrow(() -> MemberNotFoundException.EXCEPTION);
.orElseThrow(MemberNotFoundException::new);
Project project = projectFacade.getProject(application.getId());

member.rejectApplication(application);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

import com.woongeya.zoing.global.error.JJoingException;
import com.woongeya.zoing.global.exception.JJoingException;

public class TokenExpiredException extends JJoingException {
public TokenExpiredException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

import com.woongeya.zoing.global.error.JJoingException;
import com.woongeya.zoing.global.exception.JJoingException;

public class TokenInvalidException extends JJoingException {
public TokenInvalidException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.http.HttpStatus;

import com.woongeya.zoing.global.error.JJoingException;
import com.woongeya.zoing.global.exception.JJoingException;

public class TokenMissingException extends JJoingException {
public TokenMissingException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.springframework.http.HttpStatus.*;

import com.woongeya.zoing.global.error.JJoingException;
import com.woongeya.zoing.global.exception.JJoingException;

public class TokenNotExistException extends JJoingException {
public TokenNotExistException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.springframework.http.HttpStatus.*;

import com.woongeya.zoing.global.error.JJoingException;
import com.woongeya.zoing.global.exception.JJoingException;

public class UserIsNotAdminException extends JJoingException {
public UserIsNotAdminException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import static org.springframework.http.HttpStatus.*;

import org.springframework.http.HttpStatus;

import com.woongeya.zoing.global.error.JJoingException;
import com.woongeya.zoing.global.exception.JJoingException;

public class UserNotLoginException extends JJoingException {
public UserNotLoginException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long>, CustomChatRoomRepository {

default public ChatRoom getById(Long chatRoomId) {
return findById(chatRoomId).orElseThrow(ChatRoomNotFoundException::new);
return findById(chatRoomId).orElseThrow(() -> new ChatRoomNotFoundException(chatRoomId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.woongeya.zoing.domain.chat.domain.ChatRoom;
import com.woongeya.zoing.domain.chat.domain.QChat;
import com.woongeya.zoing.domain.chat.domain.QChatRoom;
import com.woongeya.zoing.domain.user.domain.User;

import lombok.RequiredArgsConstructor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.woongeya.zoing.domain.chat.exception;

import com.woongeya.zoing.global.error.exception.ErrorCode;
import com.woongeya.zoing.global.error.JJoingException;
import org.springframework.http.HttpStatus;

import com.woongeya.zoing.global.exception.JJoingException;

public class ChatRoomNotFoundException extends JJoingException {

public ChatRoomNotFoundException() {
super(ErrorCode.CHAT_ROOM_NOT_FOUND);
public ChatRoomNotFoundException(Long id) {
super(HttpStatus.NOT_FOUND, String.format("%s의 아이디를 가진 채팅방을 찾을 수 없습니다.", id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.stereotype.Service;

import com.woongeya.zoing.domain.auth.repository.AuthRepository;
import com.woongeya.zoing.domain.chat.domain.ChatRoom;
import com.woongeya.zoing.domain.chat.domain.repository.ChatRepository;
import com.woongeya.zoing.domain.chat.domain.repository.ChatRoomRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.springframework.stereotype.Service;

import com.woongeya.zoing.domain.auth.repository.AuthRepository;
import com.woongeya.zoing.domain.chat.domain.ChatRoom;
import com.woongeya.zoing.domain.chat.domain.repository.ChatRoomRepository;
import com.woongeya.zoing.domain.chat.presentation.dto.response.ChatRoomResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.woongeya.zoing.domain.comment.exception;

import com.woongeya.zoing.global.error.exception.ErrorCode;
import com.woongeya.zoing.global.error.JJoingException;
import org.springframework.http.HttpStatus;

public class CommentNotFoundException extends JJoingException {
import com.woongeya.zoing.global.exception.JJoingException;

public static final JJoingException EXCEPTION = new CommentNotFoundException();
public class CommentNotFoundException extends JJoingException {

public CommentNotFoundException() {
super(ErrorCode.COMMENT_NOT_FOUND);
public CommentNotFoundException(Long id) {
super(HttpStatus.NOT_FOUND, String.format("%s의 아이디를 가진 댓글을 찾을 수 없습니다", id));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.woongeya.zoing.domain.comment.exception;

import com.woongeya.zoing.global.error.exception.ErrorCode;
import com.woongeya.zoing.global.error.JJoingException;
import org.springframework.http.HttpStatus;

public class ReCommentNotFoundException extends JJoingException {
import com.woongeya.zoing.global.exception.JJoingException;

public static final JJoingException EXCEPTION = new CommentNotFoundException();
public class ReCommentNotFoundException extends JJoingException {

public ReCommentNotFoundException() {
super(ErrorCode.RECOMMENT_NOT_FOUND);
public ReCommentNotFoundException(Long id) {
super(HttpStatus.NOT_FOUND, String.format("%s의 아이디를 가진 대댓글을 찾을 수 없습니다", id));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class CreateCommentService {
public void execute(Long postId, CreateCommentRequest request) {
User user = authRepository.getCurrentUser();
Post post = postRepository.findById(postId)
.orElseThrow(() -> PostNotFoundException.EXCEPTION);
.orElseThrow(() -> new PostNotFoundException(postId));
commentRepository.save(request.toEntity(postId, user));
post.increaseCommentCount();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.woongeya.zoing.domain.comment.service.command;

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

import com.woongeya.zoing.domain.auth.repository.AuthRepository;
import com.woongeya.zoing.domain.comment.domain.Comment;
import com.woongeya.zoing.domain.comment.domain.ReComment;
import com.woongeya.zoing.domain.comment.domain.repository.CommentRepository;
import com.woongeya.zoing.domain.comment.domain.repository.ReCommentRepository;
import com.woongeya.zoing.domain.comment.exception.CommentNotFoundException;
import com.woongeya.zoing.domain.comment.presetation.dto.request.CreateCommentRequest;
import com.woongeya.zoing.domain.user.UserFacade;
import com.woongeya.zoing.domain.user.domain.User;

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

@Service
@RequiredArgsConstructor
Expand All @@ -25,7 +26,7 @@ public class CreateReCommentService {
public void execute(Long id, CreateCommentRequest request) {
User user = authRepository.getCurrentUser();
Comment comment = commentRepository.findById(id)
.orElseThrow(() -> CommentNotFoundException.EXCEPTION);
.orElseThrow(() -> new CommentNotFoundException(id));
reCommentRepository.save(
new ReComment(request.content(), id, user.getId())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public class DeleteCommentService {
public void execute(Long id) {
User user = authRepository.getCurrentUser();
Comment comment = commentRepository.findById(id)
.orElseThrow(() -> CommentNotFoundException.EXCEPTION);
.orElseThrow(() -> new CommentNotFoundException(id));

if(!comment.isWriter(user.getId())) {
throw new IsNotWriterException();
}

Post post = postRepository.findById(comment.getPostId())
.orElseThrow(() -> PostNotFoundException.EXCEPTION);
.orElseThrow(() -> new PostNotFoundException(comment.getPostId()));
commentRepository.delete(comment);
post.decreaseCommentCount();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public class DeleteReCommentService {
public void execute(Long id) {
User user = authRepository.getCurrentUser();
ReComment reComment = reCommentRepository.findById(id)
.orElseThrow(() -> ReCommentNotFoundException.EXCEPTION);
.orElseThrow(() -> new ReCommentNotFoundException(id));

if(!reComment.isWriter(user.getId())) {
throw new IsNotWriterException();
}

Comment comment = commentRepository.findById(reComment.getCommentId())
.orElseThrow(() -> CommentNotFoundException.EXCEPTION);
.orElseThrow(() -> new CommentNotFoundException(reComment.getCommentId()));
reCommentRepository.delete(reComment);
comment.decreaseReCommentCount();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class UpdateCommentService {
public void execute(Long id, CreateCommentRequest request) {
User user = authRepository.getCurrentUser();
Comment comment = commentRepository.findById(id)
.orElseThrow(() -> CommentNotFoundException.EXCEPTION);
.orElseThrow(() -> new CommentNotFoundException(id));

if(!comment.isWriter(user.getId())) {
throw new IsNotWriterException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class UpdateReCommentService {
public void execute(Long id, CreateCommentRequest request) {
User user = authRepository.getCurrentUser();
ReComment reComment = reCommentRepository.findById(id)
.orElseThrow(() -> ReCommentNotFoundException.EXCEPTION);
.orElseThrow(() -> new ReCommentNotFoundException(id));

if(!reComment.isWriter(user.getId())) {
throw new IsNotWriterException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Transactional(readOnly = true)
public class QueryCommentListService {

private final AuthRepository authRepository;
private final UserFacade userFacade;
private final CommentRepository commentRepository;

public List<CommentResponse> execute(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Transactional(readOnly = true)
public class QueryReCommentListService {

private final AuthRepository authRepository;
private final UserFacade userFacade;
private final ReCommentRepository reCommentRepository;

public List<ReCommentResponse> execute(Long id) {
Expand Down
Loading

0 comments on commit 004e408

Please sign in to comment.