티스토리 뷰

jvm언어관련/java

Catch Timeout Exception

055055 2019. 5. 16. 11:32
반응형

Api 통신 작업을 하다가, Timeout Exception을 catch하여 처리해야 하는 상황이 있습니다.

		try{	
			//api 통신
			restTemplate.postForEntity(...)
		} catch (HttpStatusCodeException e) {
			Result result = ResultCode.builder().memberStatusCode(....).build();
			result.setResultCode(ServiceError.COMMON_SYSTEM_ERROR.getCode());
			result.setResultMessage(e.getResponseBodyAsString());
			return result;
		}catch (RestClientException re){
			if(re.getRootCause() instanceof SocketTimeoutException
					|| re.getRootCause() instanceof ConnectTimeoutException){
				log.error("[confirmPennyTest] socket || connect timeout error = {}", re);
			}
			log.error(" timeout error = {}", re);
			throw new ServiceException(ServiceError.COMMON_NETWORK_ERROR);
		} catch (Exception e) {
			e.printStackTrace();
						
			Result result = ResultCode.builder().memberStatusCode(....).build();
			result.setResultCode(ServiceError.COMMON_SYSTEM_ERROR.getCode());
			result.setResultMessage(e.getMessage());
			return result;
		}

먼저 HttpStatusCodeException 으로 api 통신시 4xx, 5xx 코드들에 대한 처리를 해줍니다.

그리고 그외 RestClientException으로

re.getRootCause() instanceof SocketTimeoutException || re.getRootCause() instanceof ConnectTimeoutException

문을 조건문을 주어 타임아웃 일 때 catch하여 Exception 처리를 해주면 됩니다.

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함