-
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.
- Exception Super Class(Client Side, Server Side) 제작 - BindException, ServletException, 커스텀 Super Class 두개 핸들러에 메서드 추가 - 테스트 및 예시용 Service 패키지에 NoMatchIdException 추가 - ExceptionResponse Dto 추가 - ExceptionCode enums 추가 : 내용은 Exception 생성 시마다 추가 권장
- Loading branch information
Showing
7 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/com/skklub/admin/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,58 @@ | ||
package com.skklub.admin; | ||
|
||
import com.skklub.admin.exception.ClientSideException; | ||
import com.skklub.admin.exception.ExceptionResponse; | ||
import com.skklub.admin.exception.ServerSideException; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BindException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
/** | ||
* 입력값이 잘못되어 예외가 터진 경우 | ||
*/ | ||
@ExceptionHandler(ClientSideException.class) | ||
public ResponseEntity<ExceptionResponse> clientSideException(ClientSideException clientSideException, HttpServletRequest request) { | ||
ExceptionResponse exceptionResponse = new ExceptionResponse(clientSideException); | ||
return ResponseEntity | ||
.badRequest() | ||
.body(exceptionResponse); | ||
} | ||
|
||
/** | ||
* 서버 내부 문제로 예외가 터진 경우 | ||
*/ | ||
@ExceptionHandler(ServerSideException.class) | ||
public ResponseEntity<ExceptionResponse> serverSideException(ServerSideException serverSideException, HttpServletRequest request) { | ||
ExceptionResponse exceptionResponse = new ExceptionResponse(serverSideException); | ||
return ResponseEntity | ||
.internalServerError() | ||
.body(exceptionResponse); | ||
} | ||
|
||
/** | ||
* Spring Bean Validation 관련 예외 | ||
*/ | ||
@ExceptionHandler(BindException.class) | ||
public ResponseEntity<ExceptionResponse> bindException(BindException bindException, HttpServletRequest request) { | ||
return ResponseEntity | ||
.badRequest() | ||
.body(null); | ||
} | ||
|
||
/** | ||
* Spring MVC Controller 파라미터 매칭 예외 | ||
*/ | ||
@ExceptionHandler(ServletException.class) | ||
public ResponseEntity<ExceptionResponse> servletException(ServletException servletException, HttpServletRequest request) { | ||
return ResponseEntity | ||
.badRequest() | ||
.body(null); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/skklub/admin/exception/ClientSideException.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,22 @@ | ||
package com.skklub.admin.exception; | ||
|
||
public class ClientSideException extends RuntimeException { | ||
public ClientSideException() { | ||
} | ||
|
||
public ClientSideException(String message) { | ||
super(message); | ||
} | ||
|
||
public ClientSideException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ClientSideException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public ClientSideException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
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,5 @@ | ||
package com.skklub.admin.exception; | ||
|
||
public enum ExceptionCode { | ||
NO_MATCH_ID() | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/skklub/admin/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,26 @@ | ||
package com.skklub.admin.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ExceptionResponse { | ||
private ExceptionCode exceptionCode; | ||
private LocalDateTime timeStamp; | ||
private String detailMessage; | ||
private Class exceptionClassName; | ||
|
||
public ExceptionResponse(RuntimeException exception) { | ||
this.exceptionCode = null; | ||
this.timeStamp = LocalDateTime.now(); | ||
this.detailMessage = exception.getMessage(); | ||
this.exceptionClassName = exception.getCause().getClass(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/skklub/admin/exception/ServerSideException.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,22 @@ | ||
package com.skklub.admin.exception; | ||
|
||
public class ServerSideException extends RuntimeException { | ||
public ServerSideException() { | ||
} | ||
|
||
public ServerSideException(String message) { | ||
super(message); | ||
} | ||
|
||
public ServerSideException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ServerSideException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public ServerSideException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/skklub/admin/service/exception/NoMatchIdException.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 com.skklub.admin.service.exception; | ||
|
||
import com.skklub.admin.exception.ClientSideException; | ||
|
||
public class NoMatchIdException extends ClientSideException { | ||
public NoMatchIdException(Long invalidId) { | ||
super("제공된 ID값(" + invalidId + ")과 일치하는 데이터가 존재하지 않습니다"); | ||
} | ||
} |