-
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! : 예외 처리 설정 #68
Merged
Merged
Feat! : 예외 처리 설정 #68
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fdb562d
[feat] Custom Exception handler
drbug2000 134b7ff
[Refactoring] exception Handler package name chage
drbug2000 0395b06
Merge branch 'develop' into feat/#66/exception-handler
drbug2000 09c8990
[Fix] baseResponse paramter
drbug2000 b609cdc
Merge branch 'develop' into feat/#66/exception-handler
drbug2000 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
19 changes: 19 additions & 0 deletions
19
src/main/java/space/space_spring/exception/CustomException.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 space.space_spring.exception; | ||
|
||
import lombok.Getter; | ||
import space.space_spring.response.status.ResponseStatus; | ||
@Getter | ||
public class CustomException extends RuntimeException{ | ||
|
||
private final ResponseStatus exceptionStatus; | ||
|
||
public CustomException(ResponseStatus exceptionStatus) { | ||
super(exceptionStatus.getMessage()); | ||
this.exceptionStatus = exceptionStatus; | ||
} | ||
|
||
public CustomException(ResponseStatus exceptionStatus, String message) { | ||
super(message); | ||
this.exceptionStatus = exceptionStatus; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/space/space_spring/exception/InternalServerErrorException.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 space.space_spring.exception; | ||
|
||
import lombok.Getter; | ||
import space.space_spring.response.status.ResponseStatus; | ||
@Getter | ||
public class InternalServerErrorException extends RuntimeException{ | ||
private final ResponseStatus exceptionStatus; | ||
|
||
public InternalServerErrorException(ResponseStatus exceptionStatus) { | ||
super(exceptionStatus.getMessage()); | ||
this.exceptionStatus = exceptionStatus; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/space/space_spring/exceptionHandler/BaseExceptionControllerAdvice.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,65 @@ | ||
package space.space_spring.exceptionHandler; | ||
|
||
import jakarta.validation.ConstraintViolationException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.TypeMismatchException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.NoHandlerFoundException; | ||
import space.space_spring.exception.BadRequestException; | ||
import space.space_spring.exception.InternalServerErrorException; | ||
import space.space_spring.response.BaseErrorResponse; | ||
|
||
import static space.space_spring.response.status.BaseExceptionResponseStatus.*; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class BaseExceptionControllerAdvice { | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler({BadRequestException.class, NoHandlerFoundException.class, TypeMismatchException.class}) | ||
public BaseErrorResponse handle_BadRequest(Exception e) { | ||
log.error("[handle_BadRequest]", e); | ||
return new BaseErrorResponse(URL_NOT_FOUND); | ||
} | ||
|
||
// 위와 동일 (return ResponseEntity<>) | ||
/* | ||
@ExceptionHandler({BadRequestException.class, NoHandlerFoundException.class, TypeMismatchException.class, HttpRequestMethodNotSupportedException.class}) | ||
public ResponseEntity<BaseErrorResponse> handle_BadRequest(BadRequestException e) { | ||
log.error("[handle_BadRequest]", e); | ||
return ResponseEntity.badRequest().body(new BaseErrorResponse(e.getExceptionStatus())); | ||
} | ||
*/ | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||
public BaseErrorResponse handle_HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { | ||
log.error("[handle_HttpRequestMethodNotSupportedException]", e); | ||
return new BaseErrorResponse(METHOD_NOT_ALLOWED); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(ConstraintViolationException.class) | ||
public BaseErrorResponse handle_ConstraintViolationException(ConstraintViolationException e) { | ||
log.error("[handle_ConstraintViolationException]", e); | ||
return new BaseErrorResponse(BAD_REQUEST, e.getMessage()); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(InternalServerErrorException.class) | ||
public BaseErrorResponse handle_InternalServerError(InternalServerErrorException e) { | ||
log.error("[handle_InternalServerError]", e); | ||
return new BaseErrorResponse(e.getExceptionStatus()); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(RuntimeException.class) | ||
public BaseErrorResponse handle_RuntimeException(Exception e) { | ||
log.error("[handle_RuntimeException]", e); | ||
return new BaseErrorResponse(SERVER_ERROR); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/space/space_spring/exceptionHandler/CustomExceptionControllerAdvice.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 space.space_spring.exceptionHandler; | ||
|
||
import jakarta.annotation.Priority; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import space.space_spring.exception.CustomException; | ||
import space.space_spring.response.BaseErrorResponse; | ||
|
||
@Slf4j | ||
@Priority(0) | ||
@RestControllerAdvice | ||
public class CustomExceptionControllerAdvice { | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(CustomException.class) | ||
public ResponseEntity<BaseErrorResponse> handle_JwtBadRequestException(CustomException e) { | ||
log.error("[handle_JwtBadRequestException]", e); | ||
BaseErrorResponse errorResponse = new BaseErrorResponse(e.getExceptionStatus()); | ||
return ResponseEntity.status(e.getExceptionStatus().getStatus()).body(errorResponse); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/space/space_spring/exceptionHandler/JwtExceptionControllerAdvice.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,31 @@ | ||
package space.space_spring.exceptionHandler; | ||
|
||
import jakarta.annotation.Priority; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import space.space_spring.exception.jwt.bad_request.JwtBadRequestException; | ||
import space.space_spring.exception.jwt.unauthorized.JwtUnauthorizedTokenException; | ||
import space.space_spring.response.BaseErrorResponse; | ||
|
||
@Slf4j | ||
@Priority(0) | ||
@RestControllerAdvice | ||
public class JwtExceptionControllerAdvice { | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(JwtBadRequestException.class) | ||
public BaseErrorResponse handle_JwtBadRequestException(JwtBadRequestException e) { | ||
log.error("[handle_JwtBadRequestException]", e); | ||
return new BaseErrorResponse(e.getExceptionStatus()); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.UNAUTHORIZED) | ||
@ExceptionHandler(JwtUnauthorizedTokenException.class) | ||
public BaseErrorResponse handle_JwtUnauthorizedException(JwtUnauthorizedTokenException e) { | ||
log.error("[handle_JwtUnauthorizedException]", e); | ||
return new BaseErrorResponse(e.getExceptionStatus()); | ||
} | ||
} |
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
5 changes: 4 additions & 1 deletion
5
src/main/java/space/space_spring/response/status/ResponseStatus.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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package space.space_spring.response.status; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public interface ResponseStatus { | ||
|
||
|
||
int getCode(); | ||
|
||
int getStatus(); | ||
HttpStatus getStatus(); | ||
|
||
String getMessage(); | ||
} |
Oops, something went wrong.
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.
int 에서 HttpStatus로 타입을 변경해서 .values() 메서드의 중복사용을 없앨 수 있어 좋아보입니다!