-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: 전역 예외 처리 #32
Merged
Merged
feat: 전역 예외 처리 #32
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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: 13 additions & 0 deletions
13
src/main/java/com/inmybook/application/service/exception/PostDeletionFailedException.java
This file contains 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,13 @@ | ||
package com.inmybook.application.service.exception; | ||
|
||
import com.inmybook.error.ErrorCode; | ||
import com.inmybook.error.exception.InMyBookException; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class PostDeletionFailedException extends InMyBookException { | ||
public PostDeletionFailedException(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...main/java/com/inmybook/application/service/exception/PostModificationFailedException.java
This file contains 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,13 @@ | ||
package com.inmybook.application.service.exception; | ||
|
||
import com.inmybook.error.ErrorCode; | ||
import com.inmybook.error.exception.InMyBookException; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class PostModificationFailedException extends InMyBookException { | ||
public PostModificationFailedException(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...main/java/com/inmybook/application/service/exception/PostRegistrationFailedException.java
This file contains 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,13 @@ | ||
package com.inmybook.application.service.exception; | ||
|
||
import com.inmybook.error.ErrorCode; | ||
import com.inmybook.error.exception.InMyBookException; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class PostRegistrationFailedException extends InMyBookException { | ||
public PostRegistrationFailedException(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |
This file contains 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
This file contains 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,29 @@ | ||
package com.inmybook.error; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ErrorCode { | ||
INVALID_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), | ||
INVALID_REQUIRED_PARAMETER(HttpStatus.BAD_REQUEST, "필수 파라미터가 존재하지 않습니다."), | ||
UNAUTHORIZED_ACCESS(HttpStatus.UNAUTHORIZED, "리소스에 접근하려면 인증이 필요합니다."), | ||
FORBIDDEN_ACCESS(HttpStatus.FORBIDDEN, "접근 권한이 없습니다."), | ||
NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 정보입니다."), | ||
NOT_FOUND_HTTP_METHOD(HttpStatus.NOT_FOUND, "존재하지 않는 HTTP 메소드입니다."), | ||
NOT_ALLOWED_HTTP_METHOD(HttpStatus.METHOD_NOT_ALLOWED, "허용되지 않는 HTTP 메소드입니다."), | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러가 발생하였습니다. 다시 시도해주세요."), | ||
|
||
POST_NOT_FOUND(HttpStatus.NOT_FOUND, "독서록 정보가 존재하지 않습니다."), | ||
POST_REGISTRATION_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "독서록 등록 중 문제가 발생했습니다. 다시 시도해주세요."), | ||
POST_MODIFICATION_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "독서록 수정 중 문제가 발생했습니다. 다시 시도해주세요."), | ||
POST_DELETION_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "독서록 삭제 중 문제가 발생했습니다. 다시 시도해주세요."), | ||
|
||
FILE_READ_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "파일 읽기 작업 중 문제가 발생했습니다. 다시 시도해주세요."); | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
This file contains 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,32 @@ | ||
package com.inmybook.error; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@RequiredArgsConstructor | ||
public class ErrorResponseDto { | ||
public final int httpStatus; | ||
public final String code; | ||
public final String message; | ||
|
||
public ErrorResponseDto(ErrorCode errorCode) { | ||
this.httpStatus = errorCode.getHttpStatus().value(); | ||
this.code = errorCode.name(); | ||
this.message = errorCode.getMessage(); | ||
} | ||
|
||
public static ResponseEntity<ErrorResponseDto> error(ErrorCode errorCode) { | ||
return ResponseEntity | ||
.status(errorCode.getHttpStatus()) | ||
.body(ErrorResponseDto.builder() | ||
.httpStatus(errorCode.getHttpStatus().value()) | ||
.code(errorCode.name()) | ||
.message(errorCode.getMessage()) | ||
.build()); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/com/inmybook/error/GlobalExceptionHandler.java
This file contains 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,87 @@ | ||
package com.inmybook.error; | ||
|
||
import java.nio.file.AccessDeniedException; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.NoHandlerFoundException; | ||
|
||
import com.inmybook.application.service.exception.PostDeletionFailedException; | ||
import com.inmybook.application.service.exception.PostModificationFailedException; | ||
import com.inmybook.application.service.exception.PostRegistrationFailedException; | ||
import com.inmybook.error.exception.PostNotFoundException; | ||
import com.inmybook.error.exception.UnauthorizedException; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
dani820 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@ExceptionHandler(UnauthorizedException.class) | ||
protected ResponseEntity<ErrorResponseDto> handleUnauthorizedException( | ||
UnauthorizedException ex) { | ||
log.error("handleUnauthorizedException", ex); | ||
ErrorCode errorCode = ErrorCode.UNAUTHORIZED_ACCESS; | ||
return ErrorResponseDto.error(errorCode); | ||
} | ||
|
||
@ExceptionHandler(AccessDeniedException.class) | ||
protected ResponseEntity<ErrorResponseDto> handleAccessDeniedException( | ||
AccessDeniedException ex) { | ||
log.error("handleAccessDeniedException", ex); | ||
ErrorCode errorCode = ErrorCode.FORBIDDEN_ACCESS; | ||
return ErrorResponseDto.error(errorCode); | ||
} | ||
|
||
@ExceptionHandler(NoHandlerFoundException.class) | ||
protected ResponseEntity<ErrorResponseDto> handleNoHandlerFoundException( | ||
NoHandlerFoundException ex) { | ||
log.error("handleNoHandlerFoundException", ex); | ||
ErrorCode errorCode = ErrorCode.NOT_FOUND_HTTP_METHOD; | ||
return ErrorResponseDto.error(errorCode); | ||
} | ||
|
||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
protected ResponseEntity<ErrorResponseDto> handleHttpRequestMethodNotSupportedException( | ||
HttpRequestMethodNotSupportedException ex) { | ||
log.error("handleHttpRequestMethodNotSupportedException", ex); | ||
ErrorCode errorCode = ErrorCode.NOT_ALLOWED_HTTP_METHOD; | ||
return ErrorResponseDto.error(errorCode); | ||
} | ||
|
||
@ExceptionHandler(PostModificationFailedException.class) | ||
protected ResponseEntity<ErrorResponseDto> handlePostModificationFailedException( | ||
PostModificationFailedException ex) { | ||
log.error("handlePostModificationFailedException", ex); | ||
return ErrorResponseDto.error(ex.getErrorCode()); | ||
} | ||
|
||
@ExceptionHandler(PostNotFoundException.class) | ||
protected ResponseEntity<ErrorResponseDto> handlePostNotFoundException(PostNotFoundException ex) { | ||
log.error("handlePostNotFoundException", ex); | ||
return ErrorResponseDto.error(ex.getErrorCode()); | ||
} | ||
|
||
@ExceptionHandler(PostRegistrationFailedException.class) | ||
protected ResponseEntity<ErrorResponseDto> handlePostRegistrationFailedException( | ||
PostRegistrationFailedException ex) { | ||
log.error("handlePostRegistrationFailedException", ex); | ||
return ErrorResponseDto.error(ex.getErrorCode()); | ||
} | ||
|
||
@ExceptionHandler(PostDeletionFailedException.class) | ||
protected ResponseEntity<ErrorResponseDto> handlePostDeletionFailedException(PostDeletionFailedException ex) { | ||
log.error("handlePostDeletionFailedException", ex); | ||
return ErrorResponseDto.error(ex.getErrorCode()); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
protected ResponseEntity<ErrorResponseDto> handleAllException(Exception ex) { | ||
log.error("handleAllException", ex); | ||
ErrorCode errorCode = ErrorCode.INTERNAL_SERVER_ERROR; | ||
return ErrorResponseDto.error(errorCode); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/inmybook/error/exception/InMyBookException.java
This file contains 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,16 @@ | ||
package com.inmybook.error.exception; | ||
|
||
import com.inmybook.error.ErrorCode; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class InMyBookException extends RuntimeException { | ||
// ErrorCode가 HttpStatus, message를 가짐으로써 에러 변경 범위 최소화 | ||
private final ErrorCode errorCode; | ||
|
||
public InMyBookException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/inmybook/error/exception/PostNotFoundException.java
This file contains 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 com.inmybook.error.exception; | ||
|
||
import com.inmybook.error.ErrorCode; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class PostNotFoundException extends InMyBookException { | ||
public PostNotFoundException(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/inmybook/error/exception/UnauthorizedException.java
This file contains 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,11 @@ | ||
package com.inmybook.error.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.UNAUTHORIZED) | ||
public class UnauthorizedException extends RuntimeException { | ||
public UnauthorizedException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이건 어떻게 동작 할까요~?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
롬복에서 제공하는 @slf4j 어노테이션을 사용하면 컴파일 타임에 log 객체가 생성되어 따로 초기화 코드를 작성해주지 않아도 'log'라는 고정된 변수명을 통해 로그 적용이 가능합�니다. 스프링부트에서는 기본적으로 Logback을 사용하도록 되어있으며 LogBack은 Slf4j의 구현체이므로 해당 어노테이션을 사용해서 로깅 처리 시 LogBack을 통해 로그가 출력됩니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logback 과 log4j2 의 차이점은 뭘까요?
뭘 사용 하실 건가요???
그리고 스프링에서 기본적으로 설정되어있는 logback 을 log4j 로 변경 하려면 어떻게 해야 할까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logback과 log4j2의 가장 큰 차이점은 비동기 로거 제공 여부에 있습니다. log4j2는 비동기 로거를 지원함으로써 멀티 스레드 환경에서 많은 처리량과 짧은 대기 시간을 제공합니다. 그 외에도 람다식에 기반한 lazy evaluation 지원, GC 작동에 의한 지연 회피를 위해 garbage-free mode 도 지원하는 등 여러 성능 상의 이점이 있다고 판단되어 log4j2를 사용할 예정입니다.
log4j2로 변경하기 위해서는 spring-boot-starter에 포함되어 있는 spring-boot-starter-logging 프레임워크 제거 후 spring-boot-starter-log4j2 프레임워크를 추가하면 됩니다.