-
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
[CHORE] 커스텀 에러 및 핸들러 구현 #35
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
336d2bb
feat: 에러 객체 구현
eb9a265
feat: 커스텀 에러 객체 변경
da30765
chore: gitkeep 삭제
9f1486f
feat: 커스텀 에러 객체 생성
95de609
feat: 예외 핸들러 구현
9f7e655
feat: 데이터베이스 클리너 추가
82640a4
feat: BaseControllerTest 추가
65f7a46
feat: 예외 핸들러 테스트 추가
2bd899a
test: handlerTest 삭제
5e0e68a
refactor: errorCode로 타입계층 분리
a41fa9d
refactor: 에러 핸들링 세분화
280135f
refactor: 핸들링 에러 추가
0caddbc
refactor: ErrorCode 인터페이스로 추상화
0133f99
refactor: clientErrorCode 반영
18302a8
refactor: BindException으로 통일
44bdd8c
style: 개행 추가
948f59c
style: 개행 추가
b48032d
fix: 잘못된 error 객체 수정
7a1d24d
style: 불필요한 import문 삭제
2a5a268
style: 개행 수정
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
Empty file.
13 changes: 13 additions & 0 deletions
13
src/main/java/com/debatetimer/controller/exception/ErrorResponse.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.debatetimer.controller.exception; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
@Schema(description = "예외 객체") | ||
public record ErrorResponse( | ||
@Schema(description = "사용자에게 보여줄 예외 메시지") | ||
@NotBlank | ||
String message | ||
) { | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/debatetimer/controller/exception/custom/DTClientErrorException.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,10 @@ | ||
package com.debatetimer.controller.exception.custom; | ||
|
||
import com.debatetimer.controller.exception.errorcode.ClientErrorCode; | ||
|
||
public class DTClientErrorException extends DTException { | ||
|
||
public DTClientErrorException(ClientErrorCode clientErrorCode) { | ||
super(clientErrorCode.getMessage(), clientErrorCode.getStatus()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/debatetimer/controller/exception/custom/DTException.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,15 @@ | ||
package com.debatetimer.controller.exception.custom; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public abstract class DTException extends RuntimeException { | ||
|
||
private final HttpStatus httpStatus; | ||
|
||
public DTException(String message, HttpStatus httpStatus) { | ||
super(message); | ||
this.httpStatus = httpStatus; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/debatetimer/controller/exception/custom/DTServerErrorException.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,10 @@ | ||
package com.debatetimer.controller.exception.custom; | ||
|
||
import com.debatetimer.controller.exception.errorcode.ServerErrorCode; | ||
|
||
public class DTServerErrorException extends DTException { | ||
|
||
public DTServerErrorException(ServerErrorCode serverErrorCode) { | ||
super(serverErrorCode.getMessage(), serverErrorCode.getStatus()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/debatetimer/controller/exception/errorcode/ClientErrorCode.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,25 @@ | ||
package com.debatetimer.controller.exception.errorcode; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public enum ClientErrorCode implements ErrorCode { | ||
|
||
FIELD_ERROR(HttpStatus.BAD_REQUEST, "입력이 잘못되었습니다."), | ||
URL_PARAMETER_ERROR(HttpStatus.BAD_REQUEST, "입력이 잘못되었습니다."), | ||
METHOD_ARGUMENT_TYPE_MISMATCH(HttpStatus.BAD_REQUEST, "입력한 값의 타입이 잘못되었습니다."), | ||
NO_RESOURCE_FOUND(HttpStatus.NOT_FOUND, "요청한 리소스를 찾을 수 없습니다."), | ||
METHOD_NOT_SUPPORTED(HttpStatus.METHOD_NOT_ALLOWED, "허용되지 않은 메서드입니다."), | ||
MEDIA_TYPE_NOT_SUPPORTED(HttpStatus.UNSUPPORTED_MEDIA_TYPE, "허용되지 않은 미디어 타입입니다."), | ||
ALREADY_DISCONNECTED(HttpStatus.BAD_REQUEST, "이미 클라이언트에서 요청이 종료되었습니다."), | ||
; | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
|
||
ClientErrorCode(HttpStatus status, String message) { | ||
this.status = status; | ||
this.message = message; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/debatetimer/controller/exception/errorcode/ErrorCode.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,10 @@ | ||
package com.debatetimer.controller.exception.errorcode; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public interface ErrorCode { | ||
|
||
HttpStatus getStatus(); | ||
|
||
String getMessage(); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/debatetimer/controller/exception/errorcode/ServerErrorCode.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,19 @@ | ||
package com.debatetimer.controller.exception.errorcode; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public enum ServerErrorCode implements ErrorCode { | ||
|
||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류가 발생했습니다. 관리자에게 문의하세요."), | ||
; | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
|
||
ServerErrorCode(HttpStatus status, String message) { | ||
this.status = status; | ||
this.message = message; | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/com/debatetimer/controller/exception/handler/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,99 @@ | ||
package com.debatetimer.controller.exception.handler; | ||
|
||
import com.debatetimer.controller.exception.ErrorResponse; | ||
import com.debatetimer.controller.exception.custom.DTClientErrorException; | ||
import com.debatetimer.controller.exception.custom.DTServerErrorException; | ||
import com.debatetimer.controller.exception.errorcode.ClientErrorCode; | ||
import com.debatetimer.controller.exception.errorcode.ErrorCode; | ||
import com.debatetimer.controller.exception.errorcode.ServerErrorCode; | ||
import jakarta.validation.ConstraintViolationException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.catalina.connector.ClientAbortException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BindException; | ||
import org.springframework.web.HttpMediaTypeNotSupportedException; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
import org.springframework.web.servlet.resource.NoResourceFoundException; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(BindException.class) | ||
public ResponseEntity<ErrorResponse> handleBindingException(BindException exception) { | ||
log.warn("message: {}", exception.getMessage()); | ||
return toResponse(ClientErrorCode.FIELD_ERROR); | ||
} | ||
|
||
@ExceptionHandler(ConstraintViolationException.class) | ||
public ResponseEntity<ErrorResponse> handleConstraintViolationException(ConstraintViolationException exception) { | ||
log.warn("message: {}", exception.getMessage()); | ||
return toResponse(ClientErrorCode.URL_PARAMETER_ERROR); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentTypeMismatchException.class) | ||
public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatchException( | ||
MethodArgumentTypeMismatchException exception) { | ||
log.warn("message: {}", exception.getMessage()); | ||
return toResponse(ClientErrorCode.METHOD_ARGUMENT_TYPE_MISMATCH); | ||
} | ||
|
||
@ExceptionHandler(ClientAbortException.class) | ||
public ResponseEntity<ErrorResponse> handleClientAbortException(ClientAbortException exception) { | ||
log.warn("message: {}", exception.getMessage()); | ||
return toResponse(ClientErrorCode.ALREADY_DISCONNECTED); | ||
} | ||
|
||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
public ResponseEntity<ErrorResponse> handleHttpRequestMethodNotSupportedException( | ||
HttpRequestMethodNotSupportedException exception | ||
) { | ||
log.warn("message: {}", exception.getMessage()); | ||
return toResponse(ClientErrorCode.METHOD_NOT_SUPPORTED); | ||
} | ||
|
||
@ExceptionHandler(HttpMediaTypeNotSupportedException.class) | ||
public ResponseEntity<ErrorResponse> handleHttpMediaTypeNotSupportedException( | ||
HttpMediaTypeNotSupportedException exception | ||
) { | ||
log.warn("message: {}", exception.getMessage()); | ||
return toResponse(ClientErrorCode.MEDIA_TYPE_NOT_SUPPORTED); | ||
} | ||
|
||
@ExceptionHandler(NoResourceFoundException.class) | ||
public ResponseEntity<ErrorResponse> handleNoResourceFoundException(NoResourceFoundException exception) { | ||
return toResponse(ClientErrorCode.NO_RESOURCE_FOUND); | ||
} | ||
|
||
@ExceptionHandler(DTClientErrorException.class) | ||
public ResponseEntity<ErrorResponse> handleClientException(DTClientErrorException exception) { | ||
log.warn("message: {}", exception.getMessage()); | ||
return toResponse(exception.getHttpStatus(), exception.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(DTServerErrorException.class) | ||
public ResponseEntity<ErrorResponse> handleServerException(DTServerErrorException exception) { | ||
log.warn("message: {}", exception.getMessage()); | ||
return toResponse(exception.getHttpStatus(), exception.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleException(Exception exception) { | ||
log.error("exception: {}", exception); | ||
return toResponse(ServerErrorCode.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
private ResponseEntity<ErrorResponse> toResponse(ErrorCode errorCode) { | ||
return toResponse(errorCode.getStatus(), errorCode.getMessage()); | ||
} | ||
|
||
private ResponseEntity<ErrorResponse> toResponse(HttpStatus httpStatus, String message) { | ||
ErrorResponse errorResponse = new ErrorResponse(message); | ||
return ResponseEntity.status(httpStatus) | ||
.body(errorResponse); | ||
} | ||
} |
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
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
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.
[질문] 전부 ResponseEntity를 사용하신 이유가 있나요?
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.
다른 PR에서도 말했지만
을 이유로 선호해요. 목요일 회의 이후에 명확히 컨벤션 정하고 반영하겠습니다!