/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.http.handler;

import com.crunii.micro.common.constants.CommonConstants;
import com.crunii.micro.common.exception.MicroException;
import com.crunii.micro.common.http.HttpClientHolder;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.util.Date;

/**
 * @author 田平 create 2020年1月10日下午12:00:51
 */
public class HttpReqErrorHandler implements HttpRequestRetryHandler {

	/**
	 * @see org.apache.http.client.HttpRequestRetryHandler#retryRequest(java.io.IOException, int, org.apache.http.protocol.HttpContext)
	 */
	@Override
	public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
		try {
			Date callTime = (Date) HttpClientHolder.busiLogInfo.get().getOrDefault("callTime", new Date());
			long useTime = System.currentTimeMillis() - callTime.getTime();
			HttpClientHolder.busiLogInfo.get().put("useTime", useTime);
			HttpClientHolder.busiLogInfo.get().put("errorMsg", MicroException.getErrorMsg(exception));
			HttpClientHolder.busiLogInfo.get().put("callResult", CommonConstants.RESULT_FAILURE);
			HttpClientHolder.doHttpLog();
		} finally {
			HttpClientHolder.busiLogInfo.get().clear();
		}
		return false;
	}

}
