-
Notifications
You must be signed in to change notification settings - Fork 2
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
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/com/mpnp/baechelin/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,13 @@ | ||
package com.mpnp.baechelin.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CustomException extends RuntimeException{ | ||
private ErrorCode errorCode; | ||
|
||
public CustomException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/mpnp/baechelin/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,29 @@ | ||
package com.mpnp.baechelin.exception; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Builder | ||
public class ErrorResponse { | ||
private final LocalDateTime timestamp = LocalDateTime.now(); | ||
private final int status; | ||
private final String error; | ||
private final String code; | ||
private final String message; | ||
|
||
public static ResponseEntity<ErrorResponse> toResponseEntity(ErrorCode errorCode) { | ||
return ResponseEntity | ||
.status(errorCode.getStatus()) | ||
.body(ErrorResponse.builder() | ||
.status(errorCode.getStatus()) | ||
.error(errorCode.getCode()) | ||
.code(errorCode.name()) | ||
.message(errorCode.getMessage()) | ||
.build() | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/mpnp/baechelin/exception/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,17 @@ | ||
package com.mpnp.baechelin.exception; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
@ExceptionHandler(value = CustomException.class) | ||
protected ResponseEntity<ErrorResponse> handleCustomException(CustomException e) { | ||
log.error("handleCustomException throw CustomException : {}", e.getErrorCode()); | ||
return ErrorResponse.toResponseEntity(e.getErrorCode()); | ||
} | ||
} |