Skip to content

Commit

Permalink
Add Exception and Handler for handle error and send message for front…
Browse files Browse the repository at this point in the history
… server
  • Loading branch information
stoneHee99 committed Dec 14, 2023
1 parent df4960e commit e64d645
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 16 deletions.
5 changes: 4 additions & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.4'
id 'org.jetbrains.kotlin.jvm'
}

group = 'kr.co'
version = '0.0.1-SNAPSHOT'

java {
sourceCompatibility = '21'
}

configurations {
Expand Down Expand Up @@ -44,3 +44,6 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}
kotlin {
jvmToolchain(21)
}
8 changes: 8 additions & 0 deletions backend/settings.gradle
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'
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
@Getter
public enum ExceptionCode {

INVALID_REQUEST(1000, "올바르지 않은 요청입니다.");
INVALID_REQUEST(1000, "올바르지 않은 요청입니다."),

INVALID_ACCESS_TOKEN(9101, "올바르지 않은 형식의 Access Token입니다.");

private final int code;
private final String message;
Expand Down
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;
}
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()));
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class GoogleLoginStratgy implements LoginStrategy{

@Override
public LoginResponse login(LoginRequest request) {
public LoginResponse login(LoginRequest request, String accessToken) {
return new GoogleLoginResponse();
}

Expand Down
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
import kr.co.yigil.login.dto.response.LoginResponse;

public interface LoginStrategy {
LoginResponse login(LoginRequest request);
LoginResponse login(LoginRequest request, String accessToken);
String getProviderName();
}
10 changes: 0 additions & 10 deletions backend/src/main/java/kr/co/yigil/login/domain/OauthProvider.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kr.co.yigil.login.presentation;

import static kr.co.yigil.login.util.LoginUtils.extractToken;

import jakarta.servlet.http.HttpSession;
import kr.co.yigil.login.application.LoginStrategyManager;
import kr.co.yigil.login.application.strategy.LoginStrategy;
Expand All @@ -25,8 +27,9 @@ public ResponseEntity<LoginResponse> login(
@RequestHeader("Authorization") String authorizationHeader,
@RequestBody LoginRequest loginRequest) {

String accessToken = extractToken(authorizationHeader);
LoginStrategy strategy = loginStrategyManager.getLoginStrategy(provider);
LoginResponse response = strategy.login(loginRequest);
LoginResponse response = strategy.login(loginRequest, accessToken);
return ResponseEntity.ok(response);
}
}
10 changes: 10 additions & 0 deletions backend/src/main/java/kr/co/yigil/login/util/LoginUtils.java
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;
}
}
1 change: 1 addition & 0 deletions backend/src/main/resources/url.properties
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

0 comments on commit e64d645

Please sign in to comment.