diff --git a/src/main/java/com/leets/xcellentbe/domain/chatroom/domain/exception/ChatRoomForbiddenException.java b/src/main/java/com/leets/xcellentbe/domain/chatroom/domain/exception/ChatRoomForbiddenException.java new file mode 100644 index 0000000..507176f --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/domain/chatroom/domain/exception/ChatRoomForbiddenException.java @@ -0,0 +1,11 @@ +package com.leets.xcellentbe.domain.chatroom.domain.exception; + +import com.leets.xcellentbe.global.error.ErrorCode; +import com.leets.xcellentbe.global.error.exception.CommonException; + +public class ChatRoomForbiddenException extends CommonException { + public ChatRoomForbiddenException() { + + super(ErrorCode.CHAT_ROOM_FORBIDDEN); + } +} diff --git a/src/main/java/com/leets/xcellentbe/domain/chatroom/domain/exception/ChatRoomNotFoundException.java b/src/main/java/com/leets/xcellentbe/domain/chatroom/domain/exception/ChatRoomNotFoundException.java new file mode 100644 index 0000000..105f827 --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/domain/chatroom/domain/exception/ChatRoomNotFoundException.java @@ -0,0 +1,11 @@ +package com.leets.xcellentbe.domain.chatroom.domain.exception; + +import com.leets.xcellentbe.global.error.ErrorCode; +import com.leets.xcellentbe.global.error.exception.CommonException; + +public class ChatRoomNotFoundException extends CommonException { + public ChatRoomNotFoundException() { + + super(ErrorCode.CHAT_ROOM_NOT_FOUND); + } +} diff --git a/src/main/java/com/leets/xcellentbe/domain/post/exception/ArticleNotFoundException.java b/src/main/java/com/leets/xcellentbe/domain/post/exception/ArticleNotFoundException.java new file mode 100644 index 0000000..0064d6d --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/domain/post/exception/ArticleNotFoundException.java @@ -0,0 +1,11 @@ +package com.leets.xcellentbe.domain.post.exception; + +import com.leets.xcellentbe.global.error.ErrorCode; +import com.leets.xcellentbe.global.error.exception.CommonException; + +public class ArticleNotFoundException extends CommonException { + public ArticleNotFoundException() { + + super(ErrorCode.ARTICLE_NOT_FOUND); + } +} diff --git a/src/main/java/com/leets/xcellentbe/domain/user/exception/UserNotFoundException.java b/src/main/java/com/leets/xcellentbe/domain/user/exception/UserNotFoundException.java new file mode 100644 index 0000000..9d03c98 --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/domain/user/exception/UserNotFoundException.java @@ -0,0 +1,11 @@ +package com.leets.xcellentbe.domain.user.exception; + +import com.leets.xcellentbe.global.error.ErrorCode; +import com.leets.xcellentbe.global.error.exception.CommonException; + +public class UserNotFoundException extends CommonException { + public UserNotFoundException() { + + super(ErrorCode.USER_NOT_FOUND); + } +} diff --git a/src/main/java/com/leets/xcellentbe/global/error/ErrorCode.java b/src/main/java/com/leets/xcellentbe/global/error/ErrorCode.java new file mode 100644 index 0000000..55728ea --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/global/error/ErrorCode.java @@ -0,0 +1,23 @@ +package com.leets.xcellentbe.global.error; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum ErrorCode { + + INVALID_INPUT_VALUE(400, "INVALID_INPUT_VALUE", "유효하지 않은 입력값입니다."), + INVALID_TOKEN(401, "INVALID_TOKEN", "유효하지 않은 토큰입니다."), + CHAT_ROOM_FORBIDDEN(403, "CHAT_ROOM_FORBIDDEN","권한이 없는 채팅방입니다."), + EXPIRED_TOKEN(403, "EXPIRED_TOKEN", "만료된 토큰입니다."), + ARTICLE_NOT_FOUND(404, "ARTICLE_NOT_FOUND", "게시물을 찾을 수 없습니다."), + CHAT_ROOM_NOT_FOUND(404, "CHAT_ROOM_NOT_FOUND", "채팅방을 찾을 수 없습니다."), + USER_NOT_FOUND(404, "USER_NOT_FOUND", "유저를 찾을 수 없습니다."), + REJECT_DUPLICATION(409,"REJECT_DUPLICATION","중복된 값입니다."), + INTERNAL_SERVER_ERROR(500, "INTERNAL_SERVER_ERROR", "서버 오류가 발생했습니다."); + + private final int status; + private final String code; + private final String message; +} diff --git a/src/main/java/com/leets/xcellentbe/global/error/ErrorResponse.java b/src/main/java/com/leets/xcellentbe/global/error/ErrorResponse.java new file mode 100644 index 0000000..fa3001f --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/global/error/ErrorResponse.java @@ -0,0 +1,19 @@ +package com.leets.xcellentbe.global.error; + +import lombok.Getter; + +@Getter +public class ErrorResponse { + private final int status; + private final String message; + private final String code; + + public ErrorResponse(ErrorCode errorCode) { + this.status = errorCode.getStatus(); + this.message = errorCode.getMessage(); + this.code = errorCode.getCode(); + } + public static ErrorResponse of(ErrorCode errorCode) { + return new ErrorResponse(errorCode); + } +} diff --git a/src/main/java/com/leets/xcellentbe/global/error/exception/CommonException.java b/src/main/java/com/leets/xcellentbe/global/error/exception/CommonException.java new file mode 100644 index 0000000..948fbd4 --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/global/error/exception/CommonException.java @@ -0,0 +1,13 @@ +package com.leets.xcellentbe.global.error.exception; + +import com.leets.xcellentbe.global.error.ErrorCode; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class CommonException extends RuntimeException { + + private final ErrorCode errorCode; +} diff --git a/src/main/java/com/leets/xcellentbe/global/error/exception/GlobalExceptionHandler.java b/src/main/java/com/leets/xcellentbe/global/error/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..010b6cc --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/global/error/exception/GlobalExceptionHandler.java @@ -0,0 +1,25 @@ +package com.leets.xcellentbe.global.error.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import com.leets.xcellentbe.global.error.*; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(CommonException.class) // Custom Exception을 포괄적으로 처리 + public ResponseEntity handleCommonException(CommonException ex) { + ErrorCode errorCode = ex.getErrorCode(); // 전달된 예외에서 에러 코드 가져오기 + ErrorResponse errorResponse = new ErrorResponse(errorCode); + return new ResponseEntity<>(errorResponse, HttpStatus.valueOf(errorCode.getStatus())); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleGenericException(Exception ex) { + ErrorCode errorCode = ErrorCode.INTERNAL_SERVER_ERROR; + ErrorResponse errorResponse = new ErrorResponse(errorCode); + return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); + } +} diff --git a/src/main/java/com/leets/xcellentbe/global/error/exception/custom/ExpiredTokenException.java b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/ExpiredTokenException.java new file mode 100644 index 0000000..f963971 --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/ExpiredTokenException.java @@ -0,0 +1,11 @@ +package com.leets.xcellentbe.global.error.exception.custom; + +import com.leets.xcellentbe.global.error.ErrorCode; +import com.leets.xcellentbe.global.error.exception.CommonException; + +public class ExpiredTokenException extends CommonException { + public ExpiredTokenException() { + + super(ErrorCode.EXPIRED_TOKEN); + } +} diff --git a/src/main/java/com/leets/xcellentbe/global/error/exception/custom/InternalServerErrorException.java b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/InternalServerErrorException.java new file mode 100644 index 0000000..897d3cb --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/InternalServerErrorException.java @@ -0,0 +1,11 @@ +package com.leets.xcellentbe.global.error.exception.custom; + +import com.leets.xcellentbe.global.error.ErrorCode; +import com.leets.xcellentbe.global.error.exception.CommonException; + +public class InternalServerErrorException extends CommonException { + public InternalServerErrorException() { + + super(ErrorCode.INTERNAL_SERVER_ERROR); + } +} diff --git a/src/main/java/com/leets/xcellentbe/global/error/exception/custom/InvalidInputValueException.java b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/InvalidInputValueException.java new file mode 100644 index 0000000..51ec39f --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/InvalidInputValueException.java @@ -0,0 +1,11 @@ +package com.leets.xcellentbe.global.error.exception.custom; + +import com.leets.xcellentbe.global.error.ErrorCode; +import com.leets.xcellentbe.global.error.exception.CommonException; + +public class InvalidInputValueException extends CommonException { + public InvalidInputValueException() { + + super(ErrorCode.INVALID_INPUT_VALUE); + } +} diff --git a/src/main/java/com/leets/xcellentbe/global/error/exception/custom/InvalidTokenException.java b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/InvalidTokenException.java new file mode 100644 index 0000000..7df255b --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/InvalidTokenException.java @@ -0,0 +1,11 @@ +package com.leets.xcellentbe.global.error.exception.custom; + +import com.leets.xcellentbe.global.error.ErrorCode; +import com.leets.xcellentbe.global.error.exception.CommonException; + +public class InvalidTokenException extends CommonException { + public InvalidTokenException() { + + super(ErrorCode.INVALID_TOKEN); + } +} diff --git a/src/main/java/com/leets/xcellentbe/global/error/exception/custom/RejectDuplicationException.java b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/RejectDuplicationException.java new file mode 100644 index 0000000..f1c40cc --- /dev/null +++ b/src/main/java/com/leets/xcellentbe/global/error/exception/custom/RejectDuplicationException.java @@ -0,0 +1,11 @@ +package com.leets.xcellentbe.global.error.exception.custom; + +import com.leets.xcellentbe.global.error.ErrorCode; +import com.leets.xcellentbe.global.error.exception.CommonException; + +public class RejectDuplicationException extends CommonException { + public RejectDuplicationException() { + + super(ErrorCode.REJECT_DUPLICATION); + } +} diff --git a/src/test/java/com/leets/xcellentbe/XcellentBeApplicationTests.java b/src/test/java/com/leets/xcellentbe/XcellentBeApplicationTests.java index 9c0836f..cb934ff 100644 --- a/src/test/java/com/leets/xcellentbe/XcellentBeApplicationTests.java +++ b/src/test/java/com/leets/xcellentbe/XcellentBeApplicationTests.java @@ -10,4 +10,4 @@ class XcellentBeApplicationTests { void contextLoads() { } -} +} \ No newline at end of file