-
Notifications
You must be signed in to change notification settings - Fork 3
[feat] 커스텀 예외 공통 처리 #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b64ea43
:sparkles: feat: 글로벌 예외 처리 핸들러 구현 및 validation 예외 처리 추가
LimKangHyun f623dda
chore: Java 스타일 수정
b546a5e
:bug: fix: password validation @NotBlank로 변경
LimKangHyun 314a581
:bug: fix: password validation @NotNull로 변경
LimKangHyun 8e7eac5
:recycle: 리뷰 반영 수정 및 예외 로그 추가
LimKangHyun 83ff8d0
chore: Java 스타일 수정
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 11 additions & 2 deletions
13
backend/src/main/java/io/f1/backend/global/exception/CustomException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,17 @@ | ||
| package io.f1.backend.global.exception; | ||
|
|
||
| import io.f1.backend.global.exception.errorcode.ErrorCode; | ||
|
|
||
| public class CustomException extends RuntimeException { | ||
|
|
||
| public CustomException(String message) { | ||
| super(message); | ||
| private final ErrorCode errorCode; | ||
|
|
||
| public CustomException(ErrorCode errorCode) { | ||
| super(errorCode.getMessage()); | ||
| this.errorCode = errorCode; | ||
| } | ||
|
|
||
| public ErrorCode getErrorCode() { | ||
| return errorCode; | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/io/f1/backend/global/exception/errorcode/AuthErrorCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package io.f1.backend.global.exception.errorcode; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum AuthErrorCode implements ErrorCode { | ||
| UNAUTHORIZED("E401001", HttpStatus.UNAUTHORIZED, "로그인이 필요합니다."), | ||
| AUTH_SESSION_NOT_FOUND("E401002", HttpStatus.UNAUTHORIZED, "세션이 존재하지 않습니다. 로그인 후 이용해주세요."), | ||
| AUTH_SESSION_EXPIRED("E401003", HttpStatus.UNAUTHORIZED, "세션이 만료되었습니다. 다시 로그인해주세요."), | ||
| AUTH_SESSION_LOST("E401004", HttpStatus.UNAUTHORIZED, "세션 정보가 유실되었습니다. 다시 로그인해주세요."), | ||
| FORBIDDEN("E403001", HttpStatus.FORBIDDEN, "권한이 없습니다."), | ||
|
|
||
| LOGIN_FAILED("E401005", HttpStatus.UNAUTHORIZED, "아이디 또는 비밀번호가 일치하지 않습니다."); | ||
|
|
||
| private final String code; | ||
|
|
||
| private final HttpStatus httpStatus; | ||
|
|
||
| private final String message; | ||
| } |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/io/f1/backend/global/exception/errorcode/CommonErrorCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package io.f1.backend.global.exception.errorcode; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum CommonErrorCode implements ErrorCode { | ||
| BAD_REQUEST_DATA("E400001", HttpStatus.BAD_REQUEST, "잘못된 요청 데이터입니다."), | ||
| INVALID_PAGINATION("E400006", HttpStatus.BAD_REQUEST, "page와 size는 1 이상의 정수여야 합니다."), | ||
| INTERNAL_SERVER_ERROR( | ||
| "E500001", HttpStatus.INTERNAL_SERVER_ERROR, "서버에러가 발생했습니다. 관리자에게 문의해주세요."); | ||
|
|
||
| private final String code; | ||
|
|
||
| private final HttpStatus httpStatus; | ||
|
|
||
| private final String message; | ||
| } |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/io/f1/backend/global/exception/errorcode/ErrorCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.f1.backend.global.exception.errorcode; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public interface ErrorCode { | ||
|
|
||
| String getCode(); | ||
|
|
||
| HttpStatus getHttpStatus(); | ||
|
|
||
| String getMessage(); | ||
| } |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/io/f1/backend/global/exception/errorcode/QuestionErrorCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package io.f1.backend.global.exception.errorcode; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum QuestionErrorCode implements ErrorCode { | ||
| QUESTION_NOT_FOUND("E404003", HttpStatus.NOT_FOUND, "존재하지 않는 문제입니다."); | ||
|
|
||
| private final String code; | ||
|
|
||
| private final HttpStatus httpStatus; | ||
|
|
||
| private final String message; | ||
| } |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/io/f1/backend/global/exception/errorcode/QuizErrorCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package io.f1.backend.global.exception.errorcode; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum QuizErrorCode implements ErrorCode { | ||
| FILE_SIZE_TOO_LARGE("E400005", HttpStatus.BAD_REQUEST, "파일 크기가 너무 큽니다."), | ||
| UNSUPPORTED_MEDIA_TYPE("E415001", HttpStatus.UNSUPPORTED_MEDIA_TYPE, "지원하지 않는 파일 형식입니다."), | ||
| INVALID_FILTER("E400007", HttpStatus.BAD_REQUEST, "title 또는 creator 중 하나만 입력 가능합니다."), | ||
| QUIZ_NOT_FOUND("E404002", HttpStatus.NOT_FOUND, "존재하지 않는 퀴즈입니다."); | ||
|
|
||
| private final String code; | ||
|
|
||
| private final HttpStatus httpStatus; | ||
|
|
||
| private final String message; | ||
| } |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/io/f1/backend/global/exception/errorcode/RoomErrorCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package io.f1.backend.global.exception.errorcode; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum RoomErrorCode implements ErrorCode { | ||
| ROOM_USER_LIMIT_REACHED("E403002", HttpStatus.FORBIDDEN, "정원이 모두 찼습니다."), | ||
| ROOM_GAME_IN_PROGRESS("E403003", HttpStatus.FORBIDDEN, "게임이 진행 중 입니다."), | ||
| ROOM_NOT_FOUND("E404005", HttpStatus.NOT_FOUND, "존재하지 않는 방입니다."), | ||
| WRONG_PASSWORD("E401006", HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지않습니다."); | ||
|
|
||
| private final String code; | ||
|
|
||
| private final HttpStatus httpStatus; | ||
|
|
||
| private final String message; | ||
| } |
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/io/f1/backend/global/exception/errorcode/UserErrorCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package io.f1.backend.global.exception.errorcode; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum UserErrorCode implements ErrorCode { | ||
| NICKNAME_EMPTY("E400002", HttpStatus.BAD_REQUEST, "닉네임은 필수 입력입니다."), | ||
| NICKNAME_TOO_LONG("E400003", HttpStatus.BAD_REQUEST, "닉네임은 6글자 이하로 입력해야 합니다."), | ||
| NICKNAME_NOT_ALLOWED("E400004", HttpStatus.BAD_REQUEST, "한글, 영문, 숫자만 입력해주세요."), | ||
| NICKNAME_CONFLICT("E409001", HttpStatus.CONFLICT, "중복된 닉네임입니다."), | ||
| USER_NOT_FOUND("E404001", HttpStatus.NOT_FOUND, "존재하지 않는 회원입니다."); | ||
|
|
||
| private final String code; | ||
|
|
||
| private final HttpStatus httpStatus; | ||
|
|
||
| private final String message; | ||
| } |
49 changes: 49 additions & 0 deletions
49
backend/src/main/java/io/f1/backend/global/exception/handler/GlobalExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package io.f1.backend.global.exception.handler; | ||
|
|
||
| import io.f1.backend.global.exception.CustomException; | ||
| import io.f1.backend.global.exception.errorcode.CommonErrorCode; | ||
| import io.f1.backend.global.exception.errorcode.ErrorCode; | ||
| import io.f1.backend.global.exception.response.ErrorResponse; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.validation.FieldError; | ||
| import org.springframework.web.bind.MethodArgumentNotValidException; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(CustomException.class) | ||
| public ResponseEntity<ErrorResponse> handleCustomException(CustomException e) { | ||
| ErrorCode errorCode = e.getErrorCode(); | ||
|
|
||
| ErrorResponse response = new ErrorResponse(errorCode.getCode(), errorCode.getMessage()); | ||
| return new ResponseEntity<>(response, errorCode.getHttpStatus()); | ||
| } | ||
|
|
||
| @ExceptionHandler(Exception.class) | ||
| public ResponseEntity<ErrorResponse> handleException(Exception e) { | ||
| CommonErrorCode errorCode = CommonErrorCode.INTERNAL_SERVER_ERROR; | ||
|
|
||
| ErrorResponse response = new ErrorResponse(errorCode.getCode(), errorCode.getMessage()); | ||
| return new ResponseEntity<>(response, errorCode.getHttpStatus()); | ||
| } | ||
|
|
||
| @ExceptionHandler(MethodArgumentNotValidException.class) | ||
| public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException( | ||
| MethodArgumentNotValidException e) { | ||
|
|
||
| CommonErrorCode code = CommonErrorCode.BAD_REQUEST_DATA; | ||
|
|
||
| String message = | ||
| e.getBindingResult().getFieldErrors().stream() | ||
| .map(FieldError::getDefaultMessage) | ||
| .findFirst() | ||
| .orElse(code.getMessage()); | ||
|
|
||
| ErrorResponse response = new ErrorResponse(code.getCode(), message); | ||
|
|
||
| return new ResponseEntity<>(response, code.getHttpStatus()); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/io/f1/backend/global/exception/response/ErrorResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.f1.backend.global.exception.response; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public class ErrorResponse { | ||
|
|
||
| private final String code; | ||
| private final String message; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.