-
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.
Browse files
Browse the repository at this point in the history
[feat] 예외처리 환경 구축
- Loading branch information
Showing
7 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/com/backend/soullive_a/controller/ExceptionController.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,24 @@ | ||
package com.backend.soullive_a.controller; | ||
|
||
import com.backend.soullive_a.exception.base.BaseResponse; | ||
import com.backend.soullive_a.exception.custom.NotFoundUserException; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class ExceptionController { | ||
@GetMapping("/success") | ||
public BaseResponse<Object> getUser() { | ||
return BaseResponse.builder() | ||
.isSuccess(true) | ||
.code(200) | ||
.message("사용자를 찾았습니다") | ||
.data("somin") | ||
.build(); | ||
} | ||
|
||
@GetMapping("/fail") | ||
public BaseResponse<Object> notFoundUser() { | ||
throw new NotFoundUserException(); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/backend/soullive_a/exception/base/BaseException.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.backend.soullive_a.exception.base; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class BaseException extends RuntimeException { | ||
|
||
private final int errorCode; | ||
private final String message; | ||
private final HttpStatus httpStatus; | ||
|
||
public BaseException(ErrorCode errorCode) { | ||
super(); | ||
this.errorCode = errorCode.getCode(); | ||
this.message = errorCode.getMessage(); | ||
this.httpStatus = errorCode.getHttpStatus(); | ||
} | ||
|
||
public BaseException(ErrorCode errorCode, String message) { | ||
super(); | ||
this.errorCode = errorCode.getCode(); | ||
this.message = message; | ||
this.httpStatus = errorCode.getHttpStatus(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/backend/soullive_a/exception/base/BaseResponse.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.backend.soullive_a.exception.base; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class BaseResponse<T> { | ||
|
||
private boolean isSuccess; | ||
private int code; | ||
private String message; | ||
private T data; | ||
|
||
@Builder | ||
public BaseResponse(boolean isSuccess, int code, String message, T data) { | ||
this.isSuccess = isSuccess; | ||
this.code = code; | ||
this.message = message; | ||
this.data = data; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/backend/soullive_a/exception/base/CustomExceptionHandler.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,52 @@ | ||
package com.backend.soullive_a.exception.base; | ||
|
||
import jakarta.validation.ValidationException; | ||
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.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class CustomExceptionHandler extends ResponseEntityExceptionHandler { | ||
/** | ||
* Custom Exception 핸들링 | ||
*/ | ||
@ExceptionHandler(BaseException.class) | ||
public ResponseEntity<Object> onBaseException(BaseException exception) { | ||
return new ResponseEntity<>(new BaseResponse<>( | ||
false, | ||
exception.getErrorCode(), | ||
exception.getMessage(), | ||
null), | ||
null, | ||
exception.getHttpStatus()); | ||
} | ||
|
||
/** | ||
* 클라이언트로부터 넘어오는 값 Validation Exception 핸들링 | ||
*/ | ||
@ExceptionHandler(ValidationException.class) | ||
public ResponseEntity<Object> onValidationException(Exception exception) { | ||
return new ResponseEntity<>(new BaseResponse<>( | ||
false, | ||
400, | ||
exception.getMessage(), | ||
null), | ||
null, | ||
HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<Object> onException(Exception exception) { | ||
return new ResponseEntity<>(new BaseResponse<>( | ||
false, | ||
500, | ||
exception.getMessage(), | ||
null), | ||
null, | ||
HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/backend/soullive_a/exception/base/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,29 @@ | ||
package com.backend.soullive_a.exception.base; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public enum ErrorCode { | ||
// 성공 | ||
SUCCESS(true, 1000, "요청에 성공했습니다.", HttpStatus.OK), | ||
|
||
//실패 - 필요시 만들어 쓰세요 | ||
/** | ||
* 2000 : User 오류 | ||
*/ | ||
NOT_FOUND_USER(false, 2001, "사용자를 찾을 수 없습니다", HttpStatus.NOT_FOUND); | ||
|
||
private final boolean isSuccess; | ||
private final int code; | ||
private final String message; | ||
private final HttpStatus httpStatus; | ||
|
||
ErrorCode(boolean isSuccess, int code, String message, HttpStatus httpStatus) { | ||
this.isSuccess = isSuccess; | ||
this.code = code; | ||
this.message = message; | ||
this.httpStatus = httpStatus; | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
src/main/java/com/backend/soullive_a/exception/custom/NotFoundUserException.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.backend.soullive_a.exception.custom; | ||
|
||
import com.backend.soullive_a.exception.base.BaseException; | ||
import com.backend.soullive_a.exception.base.ErrorCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class NotFoundUserException extends BaseException { | ||
|
||
public NotFoundUserException() { | ||
super(ErrorCode.NOT_FOUND_USER); | ||
} | ||
|
||
public NotFoundUserException(String message) { | ||
super(ErrorCode.NOT_FOUND_USER, message); | ||
} | ||
} |