Skip to content

Commit

Permalink
Merge pull request #6 from Leets-Official/feat/#5/예외처리-구축
Browse files Browse the repository at this point in the history
[feat] 예외처리 구축
  • Loading branch information
dyk-im authored Oct 4, 2024
2 parents fc9abd4 + b9805e4 commit c357a7f
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/leets/xcellentbe/global/error/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions src/main/java/com/leets/xcellentbe/global/error/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<ErrorResponse> 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<ErrorResponse> handleGenericException(Exception ex) {
ErrorCode errorCode = ErrorCode.INTERNAL_SERVER_ERROR;
ErrorResponse errorResponse = new ErrorResponse(errorCode);
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ class XcellentBeApplicationTests {
void contextLoads() {
}

}
}

0 comments on commit c357a7f

Please sign in to comment.