forked from Kernel360/f1-Yigil
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Exception and Handler for handle error and send message for front…
… server
- Loading branch information
1 parent
df4960e
commit e64d645
Showing
14 changed files
with
131 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
pluginManagement { | ||
plugins { | ||
id 'org.jetbrains.kotlin.jvm' version '1.9.21' | ||
} | ||
} | ||
plugins { | ||
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0' | ||
} | ||
rootProject.name = 'yigil' |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/kr/co/yigil/global/exception/AuthException.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 kr.co.yigil.global.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class AuthException extends RuntimeException{ | ||
|
||
private final int code; | ||
private final String message; | ||
|
||
public AuthException(final ExceptionCode exceptionCode) { | ||
this.code = exceptionCode.getCode(); | ||
this.message = exceptionCode.getMessage(); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/kr/co/yigil/global/exception/ExceptionResponse.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,11 @@ | ||
package kr.co.yigil.global.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class ExceptionResponse { | ||
private final int code; | ||
private final String message; | ||
} |
41 changes: 41 additions & 0 deletions
41
backend/src/main/java/kr/co/yigil/global/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,41 @@ | ||
package kr.co.yigil.global.exception; | ||
|
||
import static kr.co.yigil.global.exception.ExceptionCode.INVALID_REQUEST; | ||
|
||
import java.util.Objects; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@RestControllerAdvice | ||
@Slf4j | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@Override | ||
protected ResponseEntity<Object> handleMethodArgumentNotValid( | ||
final MethodArgumentNotValidException e, | ||
final HttpHeaders headers, | ||
final HttpStatusCode status, | ||
final WebRequest request | ||
) { | ||
log.warn(e.getMessage(), e); | ||
|
||
final String errorMessage = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage(); | ||
return ResponseEntity.badRequest() | ||
.body(new ExceptionResponse(INVALID_REQUEST.getCode(), errorMessage)); | ||
} | ||
|
||
@ExceptionHandler(AuthException.class) | ||
public ResponseEntity<ExceptionResponse> handleAuthException(final AuthException e) { | ||
log.warn(e.getMessage(), e); | ||
|
||
return ResponseEntity.badRequest() | ||
.body(new ExceptionResponse(e.getCode(), e.getMessage())); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/kr/co/yigil/global/exception/InvalidTokenException.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,11 @@ | ||
package kr.co.yigil.global.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class InvalidTokenException extends AuthException { | ||
|
||
public InvalidTokenException(final ExceptionCode exceptionCode) { | ||
super(exceptionCode); | ||
} | ||
} |
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
22 changes: 21 additions & 1 deletion
22
backend/src/main/java/kr/co/yigil/login/application/strategy/KakaoLoginStrategy.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,20 +1,40 @@ | ||
package kr.co.yigil.login.application.strategy; | ||
|
||
import static kr.co.yigil.global.exception.ExceptionCode.INVALID_ACCESS_TOKEN; | ||
|
||
import kr.co.yigil.global.exception.InvalidTokenException; | ||
import kr.co.yigil.login.dto.request.KakaoLoginRequest; | ||
import kr.co.yigil.login.dto.request.LoginRequest; | ||
import kr.co.yigil.login.dto.response.KakaoLoginResponse; | ||
import kr.co.yigil.login.dto.response.LoginResponse; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.PropertySource; | ||
|
||
@PropertySource("classpath:url.properties") | ||
public class KakaoLoginStrategy implements LoginStrategy { | ||
|
||
private final String PROVIDER_NAME = "kakao"; | ||
|
||
@Value("${Kakao-Token-Info-Url}") | ||
private String KAKAO_TOKEN_INFO_URL; | ||
|
||
@Override | ||
public LoginResponse login(LoginRequest request) { | ||
public LoginResponse login(LoginRequest request, String accessToken) { | ||
KakaoLoginRequest loginRequest = (KakaoLoginRequest) request; | ||
|
||
if(!isTokenValid(accessToken)) { | ||
throw new InvalidTokenException(INVALID_ACCESS_TOKEN); | ||
} | ||
return new KakaoLoginResponse(); | ||
} | ||
|
||
@Override | ||
public String getProviderName() { | ||
return PROVIDER_NAME; | ||
} | ||
|
||
private boolean isTokenValid(String accessToken) { | ||
return true; | ||
} | ||
|
||
} |
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: 0 additions & 10 deletions
10
backend/src/main/java/kr/co/yigil/login/domain/OauthProvider.java
This file was deleted.
Oops, something went wrong.
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
backend/src/main/java/kr/co/yigil/login/util/LoginUtils.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 kr.co.yigil.login.util; | ||
|
||
public class LoginUtils { | ||
public static String extractToken(String authorizationHeader) { | ||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) { | ||
return authorizationHeader.substring(7); // "Bearer " 다음부터가 실제 토큰 | ||
} | ||
return null; | ||
} | ||
} |
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 @@ | ||
Kakao-Token-Info-Url=https://kapi.kakao.com/v1/user/access_token_info |