-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from f-lab-edu/feature/31
feat: 전역 예외 처리
- Loading branch information
Showing
12 changed files
with
240 additions
and
7 deletions.
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 { | ||
@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