-
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.
feat: Error 관련 코드, 전역 예외 처리 핸들러 추가 (#5)
- Loading branch information
1 parent
c39b500
commit 6204b38
Showing
4 changed files
with
36 additions
and
1 deletion.
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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/project/mapdagu/error/dto/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,8 @@ | ||
package com.project.mapdagu.error.dto; | ||
|
||
public record ErrorResponse(int code, String message) { | ||
public static ErrorResponse of(int code, String message){ | ||
return new ErrorResponse(code, message); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/project/mapdagu/error/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 |
---|---|---|
@@ -1,9 +1,19 @@ | ||
package com.project.mapdagu.error.exception; | ||
|
||
import com.project.mapdagu.error.dto.ErrorResponse; | ||
import com.project.mapdagu.error.exception.custom.BusinessException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<ErrorResponse> handle(final BusinessException e) { | ||
log.info("businessException: {}", e); | ||
return ResponseEntity.badRequest().body(ErrorResponse.of(e.getCode(), e.getMessage())); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/project/mapdagu/error/exception/custom/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,15 @@ | ||
package com.project.mapdagu.error.exception.custom; | ||
|
||
import com.project.mapdagu.error.ErrorCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BusinessException extends RuntimeException{ | ||
|
||
private final int code; | ||
|
||
public BusinessException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.code = errorCode.getCode(); | ||
} | ||
} |