Skip to content

Commit

Permalink
[Feat]: 외부 API 통신 에러핸들링 추가 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
bayy1216 committed May 30, 2024
1 parent 9fee8e9 commit 6680c42
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.extern.slf4j.Slf4j;
import org.haedal.zzansuni.core.api.ApiResponse;
import org.haedal.zzansuni.core.api.ErrorCode;
import org.haedal.zzansuni.global.exception.ExternalServerConnectionException;
import org.haedal.zzansuni.global.exception.UnauthorizedException;
import org.slf4j.MDC;
import org.springframework.context.support.DefaultMessageSourceResolvable;
Expand Down Expand Up @@ -109,6 +110,17 @@ public ApiResponse<Void> onException(Exception e) {
return ApiResponse.fail(ErrorCode.COMMON_SYSTEM_ERROR);
}

/**
* http status: 500 AND result: FAIL
* 외부 서버 연결 실패
*/
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ApiResponse<Void> externalServerConnectionException(ExternalServerConnectionException e) {
log.error("외부 서버 연결 실패", e);
return ApiResponse.fail("EXTERNAL_SERVER_ERROR", e.getMessage());
}




Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.haedal.zzansuni.global.api;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

Expand All @@ -13,4 +16,12 @@ public class WebConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(commonHttpRequestInterceptor);
}

@Bean
public ClientHttpRequestFactory getClientHttpRequestFactory() {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
clientHttpRequestFactory.setConnectTimeout(3000); // 3초
clientHttpRequestFactory.setReadTimeout(3000); // 3초
return clientHttpRequestFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.haedal.zzansuni.global.exception;

public class ExternalServerConnectionException extends RuntimeException{
public ExternalServerConnectionException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
package org.haedal.zzansuni.infrastructure.auth;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.auth.OAuth2Client;
import org.haedal.zzansuni.domain.auth.OAuth2Provider;
import org.haedal.zzansuni.domain.auth.OAuthUserInfoModel;
import org.haedal.zzansuni.global.exception.ExternalServerConnectionException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClient;

@RequiredArgsConstructor
@Component
@Profile({"prod", "dev"})
public class KakaoOAuth2Client implements OAuth2Client {
private final AuthTokenGenerator authTokenGenerator;
private final RestClient restClient = RestClient.create();
private final RestClient restClient;

public KakaoOAuth2Client(AuthTokenGenerator authTokenGenerator, ClientHttpRequestFactory clientHttpRequestFactory) {
this.authTokenGenerator = authTokenGenerator;
// 타임아웃 설정, 4xx, 5xx 에러 핸들러 설정
this.restClient = RestClient
.builder()
.requestFactory(clientHttpRequestFactory)
.defaultStatusHandler(HttpStatusCode::is4xxClientError, (request, response) -> {
throw new IllegalStateException("카카오 인증 code 요청에 실패했습니다.");
})
.defaultStatusHandler(HttpStatusCode::is5xxServerError, (request, response) -> {
throw new ExternalServerConnectionException("카카오 인증 서버로 부터 문제가 발생했습니다.");
})
.build();
}


private static final String GRANT_TYPE = "authorization_code";

@Value("${kakao.client-id}") private String clientId;
@Value("${kakao.redirect-uri}") private String redirectUri;
@Value("${kakao.client-secret}") private String clientSecret;
@Value("${kakao.client-id}")
private String clientId;
@Value("${kakao.redirect-uri}")
private String redirectUri;
@Value("${kakao.client-secret}")
private String clientSecret;

@Override
public boolean canHandle(OAuth2Provider provider) {
Expand Down

0 comments on commit 6680c42

Please sign in to comment.