-
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.
[BSVR-63] custom exception & global exception handler 추가 (#15)
* feat: common 모듈 추가 및 errorCode enum 생성 * feat: spot application global exception handler 추가 * feat: custom exception 예제 코드 추가 * feat: 예제 수정 * feat: gitignore에 docker db 추가
- Loading branch information
Showing
14 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
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
2 changes: 1 addition & 1 deletion
2
...ication/config/SpotApplicationConfig.java → .../common/config/SpotApplicationConfig.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
10 changes: 10 additions & 0 deletions
10
application/src/main/java/org/depromeet/spot/application/common/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,10 @@ | ||
package org.depromeet.spot.application.common.exception; | ||
|
||
import org.depromeet.spot.common.exception.BusinessException; | ||
|
||
public record ErrorResponse(String code, String message) { | ||
|
||
public static ErrorResponse from(BusinessException e) { | ||
return new ErrorResponse(e.getCode(), e.getMessage()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...rc/main/java/org/depromeet/spot/application/common/exception/SpotAppExceptionHandler.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,27 @@ | ||
package org.depromeet.spot.application.common.exception; | ||
|
||
import org.depromeet.spot.common.exception.BusinessException; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class SpotAppExceptionHandler { | ||
|
||
private static final String EXCEPTION_LOG_TEMPLATE = "code = {}, message = {}"; | ||
|
||
@ExceptionHandler(BusinessException.class) | ||
protected ResponseEntity<ErrorResponse> handleBusinessException(BusinessException e) { | ||
var code = e.getCode(); | ||
var message = e.getMessage(); | ||
var httpStatus = e.getHttpStatus(); | ||
|
||
log.error(EXCEPTION_LOG_TEMPLATE, code, message); | ||
var response = ErrorResponse.from(e); | ||
|
||
return ResponseEntity.status(httpStatus).body(response); | ||
} | ||
} |
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 @@ | ||
.gradle | ||
build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea/modules.xml | ||
.idea/jarRepositories.xml | ||
.idea/compiler.xml | ||
.idea/libraries/ | ||
*.iws | ||
*.iml | ||
*.ipr | ||
out/ | ||
!**/src/main/**/out/ | ||
!**/src/test/**/out/ | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
bin/ | ||
!**/src/main/**/bin/ | ||
!**/src/test/**/bin/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
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 @@ | ||
dependencies { | ||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
} | ||
|
||
tasks.jar { enabled = true } | ||
tasks.bootJar { enabled = false } |
18 changes: 18 additions & 0 deletions
18
common/src/main/java/org/depromeet/spot/common/exception/BusinessException.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,18 @@ | ||
package org.depromeet.spot.common.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BusinessException extends RuntimeException { | ||
|
||
private final HttpStatus httpStatus; | ||
private final String code; | ||
|
||
public BusinessException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.httpStatus = errorCode.getStatus(); | ||
this.code = errorCode.getCode(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
common/src/main/java/org/depromeet/spot/common/exception/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,12 @@ | ||
package org.depromeet.spot.common.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public interface ErrorCode { | ||
|
||
HttpStatus getStatus(); | ||
|
||
String getCode(); | ||
|
||
String getMessage(); | ||
} |
27 changes: 27 additions & 0 deletions
27
common/src/main/java/org/depromeet/spot/common/exception/member/MemberErrorCode.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,27 @@ | ||
package org.depromeet.spot.common.exception.member; | ||
|
||
import org.depromeet.spot.common.exception.ErrorCode; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum MemberErrorCode implements ErrorCode { | ||
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "M001", "요청 유저가 존재하지 않습니다."), | ||
; | ||
|
||
private final HttpStatus status; | ||
private final String code; | ||
private String message; | ||
|
||
MemberErrorCode(HttpStatus status, String code, String message) { | ||
this.status = status; | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
public MemberErrorCode appended(Object o) { | ||
message = message + " {" + o.toString() + "}"; | ||
return this; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
common/src/main/java/org/depromeet/spot/common/exception/member/MemberException.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,20 @@ | ||
package org.depromeet.spot.common.exception.member; | ||
|
||
import org.depromeet.spot.common.exception.BusinessException; | ||
|
||
public abstract class MemberException extends BusinessException { | ||
|
||
protected MemberException(MemberErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
|
||
public static class MemberNotFoundException extends MemberException { | ||
public MemberNotFoundException() { | ||
super(MemberErrorCode.MEMBER_NOT_FOUND); | ||
} | ||
|
||
public MemberNotFoundException(Object o) { | ||
super(MemberErrorCode.MEMBER_NOT_FOUND.appended(o)); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
dependencies { | ||
implementation(project(":common")) | ||
implementation(project(":domain")) | ||
|
||
// spring | ||
|
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