-
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.
Merge pull request #10 from PawWithU/feat/8-GlobalException
[Feature] 전역적 예외 처리 설정
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/main/java/com/pawwithu/connectdog/error/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,17 @@ | ||
package com.pawwithu.connectdog.error; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ErrorCode { | ||
|
||
TOKEN_NOT_EXIST("T1", "토큰이 존재하지 않습니다."), | ||
TOKEN_IS_EXPIRED("T2", "만료된 토큰입니다."), | ||
INVALID_TOKEN("T3", "잘못된 토큰입니다."); | ||
|
||
private final String code; | ||
private final String message; | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/pawwithu/connectdog/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.pawwithu.connectdog.error.dto; | ||
|
||
public record ErrorResponse(String code, String message) { | ||
public static ErrorResponse of(String code, String message){ | ||
return new ErrorResponse(code, message); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/pawwithu/connectdog/error/exception/GlobalControllerAdvice.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,35 @@ | ||
package com.pawwithu.connectdog.error.exception; | ||
|
||
import com.pawwithu.connectdog.error.dto.ErrorResponse; | ||
import com.pawwithu.connectdog.error.exception.custom.BadRequestException; | ||
import com.pawwithu.connectdog.error.exception.custom.TokenException; | ||
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; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalControllerAdvice { | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<String> handle(final Exception e) { | ||
log.error("Internal Error occurred", e); | ||
return ResponseEntity.internalServerError().body(e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<ErrorResponse> handle(final BadRequestException e) { | ||
log.info("Bad Request: {}", e); | ||
return ResponseEntity.badRequest().body(ErrorResponse.of(e.getCode(), e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<ErrorResponse> handle(final TokenException e) { | ||
log.info("Invalid Token: {}", e); | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ErrorResponse.of(e.getCode(), e.getMessage())); | ||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/pawwithu/connectdog/error/exception/custom/BadRequestException.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,16 @@ | ||
package com.pawwithu.connectdog.error.exception.custom; | ||
|
||
import com.pawwithu.connectdog.error.ErrorCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BadRequestException extends RuntimeException { | ||
|
||
private final String code; | ||
|
||
public BadRequestException(final ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.code = errorCode.getCode(); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/pawwithu/connectdog/error/exception/custom/TokenException.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.pawwithu.connectdog.error.exception.custom; | ||
|
||
import com.pawwithu.connectdog.error.ErrorCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class TokenException extends RuntimeException { | ||
|
||
private final String code; | ||
|
||
public TokenException(final ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.code = errorCode.getCode(); | ||
} | ||
|
||
|
||
} |