-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
210 additions
and
66 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
JWT/src/main/java/JWTLogIn/JWT/common/exception/CommonException.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 JWTLogIn.JWT.common.exception; | ||
|
||
public abstract class CommonException extends RuntimeException { | ||
|
||
protected CommonException () { | ||
} | ||
|
||
protected CommonException(Exception e) { | ||
super(e); | ||
} | ||
|
||
protected CommonException(String message) { | ||
super(message); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
JWT/src/main/java/JWTLogIn/JWT/common/exception/ExceptionAdvice.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,32 @@ | ||
package JWTLogIn.JWT.common.exception; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class ExceptionAdvice { // Exception Handler | ||
Logger defaultLog = LoggerFactory.getLogger(ExceptionAdvice.class); | ||
Logger exceptionLog = LoggerFactory.getLogger("ExceptionLogger"); | ||
|
||
@ExceptionHandler(CommonException.class) | ||
public ResponseEntity<ExceptionResponse> handleCommonException(CommonException e) { | ||
ExceptionSituation exceptionSituation = ExceptionMapper.getSituationOf(e); | ||
|
||
defaultLog.warn(exceptionSituation.getMessage()); | ||
exceptionLog.warn(exceptionSituation.getMessage(), e); | ||
|
||
return ResponseEntity.status(exceptionSituation.getStatusCode()) | ||
.body(ExceptionResponse.from(exceptionSituation)); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<Void> handleException(Exception e) { | ||
defaultLog.error(e.getMessage()); | ||
exceptionLog.error(e.getMessage(), e); | ||
|
||
return ResponseEntity.internalServerError().build(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
JWT/src/main/java/JWTLogIn/JWT/common/exception/ExceptionMapper.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,42 @@ | ||
package JWTLogIn.JWT.common.exception; | ||
|
||
import JWTLogIn.JWT.post.exception.IsNotNoticeException; | ||
import JWTLogIn.JWT.post.exception.PostNotFoundException; | ||
import JWTLogIn.JWT.user.exception.HasNoAuthorityException; | ||
import JWTLogIn.JWT.user.exception.UserNotFoundException; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public class ExceptionMapper { // 예외 객체 -> 예외 상태로 바꿔주는 mapper | ||
|
||
private static final Map<Class<? extends Exception>, ExceptionSituation> mapper = new LinkedHashMap<>(); | ||
|
||
static { | ||
setUpUserException(); | ||
setUpPostException(); | ||
// setUpReplyException(); | ||
} | ||
|
||
private static void setUpUserException() { | ||
mapper.put(UserNotFoundException.class, | ||
ExceptionSituation.of("해당 유저를 찾을 수 없습니다", HttpStatus.NOT_FOUND, 404)); | ||
mapper.put(HasNoAuthorityException.class, | ||
ExceptionSituation.of("유저에 대한 접근 권한이 부족합니다", HttpStatus.FORBIDDEN, 403)); | ||
} | ||
|
||
private static void setUpPostException() { | ||
mapper.put(PostNotFoundException.class, | ||
ExceptionSituation.of("해당 공지를 찾을 수 없습니다", HttpStatus.NOT_FOUND, 404)); | ||
mapper.put(IsNotNoticeException.class, | ||
ExceptionSituation.of("요청된 게시글은 공지가 아닙니다", HttpStatus.BAD_REQUEST, 400)); | ||
} | ||
|
||
|
||
public static ExceptionSituation getSituationOf(Exception exception) { | ||
return mapper.get(exception.getClass()); | ||
} | ||
|
||
|
||
} |
11 changes: 11 additions & 0 deletions
11
JWT/src/main/java/JWTLogIn/JWT/common/exception/ExceptionResponse.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 JWTLogIn.JWT.common.exception; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public record ExceptionResponse(int code, String message) { | ||
|
||
public static ExceptionResponse from(ExceptionSituation exceptionSituation) { | ||
return new ExceptionResponse(exceptionSituation.getErrorCode(), exceptionSituation.getMessage()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
JWT/src/main/java/JWTLogIn/JWT/common/exception/ExceptionSituation.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,17 @@ | ||
package JWTLogIn.JWT.common.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class ExceptionSituation { | ||
private final String message; | ||
private final HttpStatus statusCode; | ||
private final int errorCode; | ||
|
||
public static ExceptionSituation of(String message, HttpStatus statusCode, int errorCode) { | ||
return new ExceptionSituation(message, statusCode, 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
6 changes: 6 additions & 0 deletions
6
JWT/src/main/java/JWTLogIn/JWT/post/exception/IsNotNoticeException.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,6 @@ | ||
package JWTLogIn.JWT.post.exception; | ||
|
||
import JWTLogIn.JWT.common.exception.CommonException; | ||
|
||
public class IsNotNoticeException extends CommonException { | ||
} |
9 changes: 9 additions & 0 deletions
9
JWT/src/main/java/JWTLogIn/JWT/post/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,9 @@ | ||
package JWTLogIn.JWT.post.exception; | ||
|
||
import JWTLogIn.JWT.common.exception.CommonException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
public class PostNotFoundException extends CommonException { | ||
} |
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
6 changes: 6 additions & 0 deletions
6
JWT/src/main/java/JWTLogIn/JWT/user/exception/HasNoAuthorityException.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,6 @@ | ||
package JWTLogIn.JWT.user.exception; | ||
|
||
import JWTLogIn.JWT.common.exception.CommonException; | ||
|
||
public class HasNoAuthorityException extends CommonException { | ||
} |
6 changes: 6 additions & 0 deletions
6
JWT/src/main/java/JWTLogIn/JWT/user/exception/UserNotFoundException.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,6 @@ | ||
package JWTLogIn.JWT.user.exception; | ||
|
||
import JWTLogIn.JWT.common.exception.CommonException; | ||
|
||
public class UserNotFoundException extends CommonException { | ||
} |