diff --git a/src/main/java/com/moongeul/backend/api/member/service/GoogleOAuthService.java b/src/main/java/com/moongeul/backend/api/member/service/GoogleOAuthService.java index d510640..5319b6a 100644 --- a/src/main/java/com/moongeul/backend/api/member/service/GoogleOAuthService.java +++ b/src/main/java/com/moongeul/backend/api/member/service/GoogleOAuthService.java @@ -2,10 +2,7 @@ import com.moongeul.backend.api.member.dto.GoogleInfoResponseDTO; import com.moongeul.backend.api.member.dto.AccessTokenResponseDTO; -import com.moongeul.backend.common.exception.BadRequestException; -import com.moongeul.backend.common.exception.InternalServerException; -import com.moongeul.backend.common.exception.UnauthorizedException; -import com.moongeul.backend.common.response.ErrorStatus; +import com.moongeul.backend.common.config.webclient.WebClientErrorHandler; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -28,6 +25,7 @@ public class GoogleOAuthService { private static final String GOOGLE_TOKEN_URL = "https://oauth2.googleapis.com/token"; private static final String GOOGLE_USERINFO_URL = "https://www.googleapis.com/oauth2/v3/userinfo"; + private final WebClient webClient; @Value("${spring.security.oauth2.client.registration.google.client-id}") @@ -64,15 +62,7 @@ public AccessTokenResponseDTO getGoogleToken(String code) { .contentType(MediaType.APPLICATION_FORM_URLENCODED) .body(BodyInserters.fromFormData(params)) .retrieve() - .onStatus(status -> status.value() == 401, response -> { - throw new UnauthorizedException(ErrorStatus.AUTH_UNAUTHORIZED.getMessage()); - }) - .onStatus(HttpStatusCode::is4xxClientError, response -> { - throw new BadRequestException(ErrorStatus.INVALID_TOKEN_REQUEST.getMessage()); - }) - .onStatus(HttpStatusCode::is5xxServerError, response -> { - throw new InternalServerException(ErrorStatus.SERVER_ERROR.getMessage()); - }) + .onStatus(HttpStatusCode::isError, res -> WebClientErrorHandler.handleApiError(res, "getGoogleToken")) .bodyToMono(AccessTokenResponseDTO.class) .block(); } @@ -85,15 +75,7 @@ public GoogleInfoResponseDTO getGoogleUserInfo(String googleAccessToken) { .header(HttpHeaders.AUTHORIZATION, "Bearer " + googleAccessToken) .accept(MediaType.APPLICATION_JSON) .retrieve() - .onStatus(status -> status.value() == 401, response -> { - throw new UnauthorizedException(ErrorStatus.AUTH_UNAUTHORIZED.getMessage()); - }) - .onStatus(HttpStatusCode::is4xxClientError, response -> { - throw new BadRequestException(ErrorStatus.INVALID_INFO_REQUEST.getMessage()); - }) - .onStatus(HttpStatusCode::is5xxServerError, response -> { - throw new InternalServerException(ErrorStatus.SERVER_ERROR.getMessage()); - }) + .onStatus(HttpStatusCode::isError, res -> WebClientErrorHandler.handleApiError(res, "getGoogleUserInfo")) .bodyToMono(GoogleInfoResponseDTO.class) .block(); // 동기 방식으로 결과 대기 } diff --git a/src/main/java/com/moongeul/backend/api/member/service/KakaoOAuthService.java b/src/main/java/com/moongeul/backend/api/member/service/KakaoOAuthService.java index 3924538..2b4d761 100644 --- a/src/main/java/com/moongeul/backend/api/member/service/KakaoOAuthService.java +++ b/src/main/java/com/moongeul/backend/api/member/service/KakaoOAuthService.java @@ -2,10 +2,7 @@ import com.moongeul.backend.api.member.dto.AccessTokenResponseDTO; import com.moongeul.backend.api.member.dto.KakaoInfoResponseDTO; -import com.moongeul.backend.common.exception.BadRequestException; -import com.moongeul.backend.common.exception.InternalServerException; -import com.moongeul.backend.common.exception.UnauthorizedException; -import com.moongeul.backend.common.response.ErrorStatus; +import com.moongeul.backend.common.config.webclient.WebClientErrorHandler; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -51,15 +48,7 @@ public AccessTokenResponseDTO getKakaoToken(String code){ .contentType(MediaType.APPLICATION_FORM_URLENCODED) .body(BodyInserters.fromFormData(params)) .retrieve() - .onStatus(status -> status.value() == 401, response -> { - throw new UnauthorizedException(ErrorStatus.AUTH_UNAUTHORIZED.getMessage()); - }) - .onStatus(HttpStatusCode::is4xxClientError, response -> { - throw new BadRequestException(ErrorStatus.INVALID_TOKEN_REQUEST.getMessage()); - }) - .onStatus(HttpStatusCode::is5xxServerError, response -> { - throw new InternalServerException(ErrorStatus.SERVER_ERROR.getMessage()); - }) + .onStatus(HttpStatusCode::isError, res -> WebClientErrorHandler.handleApiError(res, "getKakaoToken")) .bodyToMono(AccessTokenResponseDTO.class) .block(); } @@ -72,15 +61,7 @@ public KakaoInfoResponseDTO getKakaoUserInfo(String kakaoAccessToken){ .header(HttpHeaders.AUTHORIZATION, "Bearer " + kakaoAccessToken) .accept(MediaType.APPLICATION_JSON) .retrieve() - .onStatus(status -> status.value() == 401, response -> { - throw new UnauthorizedException(ErrorStatus.AUTH_UNAUTHORIZED.getMessage()); - }) - .onStatus(HttpStatusCode::is4xxClientError, response -> { - throw new BadRequestException(ErrorStatus.INVALID_INFO_REQUEST.getMessage()); - }) - .onStatus(HttpStatusCode::is5xxServerError, response -> { - throw new InternalServerException(ErrorStatus.SERVER_ERROR.getMessage()); - }) + .onStatus(HttpStatusCode::isError, res -> WebClientErrorHandler.handleApiError(res, "getKakaoUserInfo")) .bodyToMono(KakaoInfoResponseDTO.class) .block(); // 동기 방식으로 결과 대기 } diff --git a/src/main/java/com/moongeul/backend/common/config/webclient/WebClientErrorHandler.java b/src/main/java/com/moongeul/backend/common/config/webclient/WebClientErrorHandler.java new file mode 100644 index 0000000..a4437b9 --- /dev/null +++ b/src/main/java/com/moongeul/backend/common/config/webclient/WebClientErrorHandler.java @@ -0,0 +1,29 @@ +package com.moongeul.backend.common.config.webclient; + +import com.moongeul.backend.common.exception.BadRequestException; +import com.moongeul.backend.common.exception.InternalServerException; +import com.moongeul.backend.common.exception.UnauthorizedException; +import com.moongeul.backend.common.response.ErrorStatus; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.reactive.function.client.ClientResponse; +import reactor.core.publisher.Mono; + +@Slf4j +public class WebClientErrorHandler { + + public static Mono handleApiError(ClientResponse response, String apiName) { + return response.bodyToMono(String.class) + .defaultIfEmpty("No error body content") + .flatMap(errorBody -> { + log.error("[{}] API Error: Status Code: {}, Body: {}", apiName, response.statusCode(), errorBody); + + if (response.statusCode().value() == 401) { + return Mono.error(new UnauthorizedException(ErrorStatus.AUTH_UNAUTHORIZED.getMessage())); + } else if (response.statusCode().is4xxClientError()) { + return Mono.error(new BadRequestException(ErrorStatus.INVALID_TOKEN_REQUEST.getMessage())); + } else { + return Mono.error(new InternalServerException(ErrorStatus.SERVER_ERROR.getMessage())); + } + }); + } +}