🎨 优化代码

This commit is contained in:
Binary Wang
2020-09-26 16:15:56 +08:00
parent 5ecfaf7cd0
commit 1d7344309a
67 changed files with 277 additions and 234 deletions

View File

@@ -9,6 +9,7 @@ import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.session.StandardSessionManager;
import me.chanjar.weixin.common.session.WxSession;
import me.chanjar.weixin.common.session.WxSessionManager;
@@ -227,7 +228,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
if (retryTimes + 1 > this.maxRetryTimes) {
log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
//最后一次重试失败后,直接抛出异常,不再等待
throw new RuntimeException("微信服务端异常,超出重试次数");
throw new WxRuntimeException("微信服务端异常,超出重试次数");
}
WxError error = e.getError();
@@ -249,7 +250,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
} while (retryTimes++ < this.maxRetryTimes);
log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
throw new RuntimeException("微信服务端异常,超出重试次数");
throw new WxRuntimeException("微信服务端异常,超出重试次数");
}
protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
@@ -285,7 +286,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
return null;
} catch (IOException e) {
log.error("\n【请求地址】: {}\n【请求参数】{}\n【异常信息】{}", uriWithAccessToken, dataForLog, e.getMessage());
throw new RuntimeException(e);
throw new WxRuntimeException(e);
}
}

View File

@@ -6,6 +6,7 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxCpErrorMsgEnum;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.cp.api.WxCpExternalContactService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
@@ -30,7 +31,7 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
public WxCpContactWayResult addContactWay(@NonNull WxCpContactWayInfo info) throws WxErrorException {
if (info.getContactWay().getUsers() != null && info.getContactWay().getUsers().size() > 100) {
throw new RuntimeException("「联系我」使用人数默认限制不超过100人(包括部门展开后的人数)");
throw new WxRuntimeException("「联系我」使用人数默认限制不超过100人(包括部门展开后的人数)");
}
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(ADD_CONTACT_WAY);
@@ -52,10 +53,10 @@ public class WxCpExternalContactServiceImpl implements WxCpExternalContactServic
@Override
public WxCpBaseResp updateContactWay(@NonNull WxCpContactWayInfo info) throws WxErrorException {
if (StringUtils.isBlank(info.getContactWay().getConfigId())) {
throw new RuntimeException("更新「联系我」方式需要指定configId");
throw new WxRuntimeException("更新「联系我」方式需要指定configId");
}
if (info.getContactWay().getUsers() != null && info.getContactWay().getUsers().size() > 100) {
throw new RuntimeException("「联系我」使用人数默认限制不超过100人(包括部门展开后的人数)");
throw new WxRuntimeException("「联系我」使用人数默认限制不超过100人(包括部门展开后的人数)");
}
final String url = this.mainService.getWxCpConfigStorage().getApiUrl(UPDATE_CONTACT_WAY);

View File

@@ -31,7 +31,7 @@ public class WxCpGroupRobotServiceImpl implements WxCpGroupRobotService {
WxCpConfigStorage wxCpConfigStorage = this.cpService.getWxCpConfigStorage();
final String webhookKey = wxCpConfigStorage.getWebhookKey();
if (StringUtils.isEmpty(webhookKey)) {
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("请先设置WebhookKey").build());
throw new WxErrorException("请先设置WebhookKey");
}
return wxCpConfigStorage.getApiUrl(WxCpApiPathConsts.WEBHOOK_SEND) + webhookKey;
}

View File

@@ -7,6 +7,7 @@ import com.google.gson.reflect.TypeToken;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.api.WxCpOaService;
import me.chanjar.weixin.cp.api.WxCpService;
@@ -42,18 +43,18 @@ public class WxCpOaServiceImpl implements WxCpOaService {
public List<WxCpCheckinData> getCheckinData(Integer openCheckinDataType, Date startTime, Date endTime,
List<String> userIdList) throws WxErrorException {
if (startTime == null || endTime == null) {
throw new RuntimeException("starttime and endtime can't be null");
throw new WxRuntimeException("starttime and endtime can't be null");
}
if (userIdList == null || userIdList.size() > USER_IDS_LIMIT) {
throw new RuntimeException("用户列表不能为空,不超过 " + USER_IDS_LIMIT + " 个,若用户超过 " + USER_IDS_LIMIT + " 个,请分批获取");
throw new WxRuntimeException("用户列表不能为空,不超过 " + USER_IDS_LIMIT + " 个,若用户超过 " + USER_IDS_LIMIT + " 个,请分批获取");
}
long endTimestamp = endTime.getTime() / 1000L;
long startTimestamp = startTime.getTime() / 1000L;
if (endTimestamp - startTimestamp < 0 || endTimestamp - startTimestamp >= MONTH_SECONDS) {
throw new RuntimeException("获取记录时间跨度不超过一个月");
throw new WxRuntimeException("获取记录时间跨度不超过一个月");
}
JsonObject jsonObject = new JsonObject();
@@ -83,11 +84,11 @@ public class WxCpOaServiceImpl implements WxCpOaService {
@Override
public List<WxCpCheckinOption> getCheckinOption(Date datetime, List<String> userIdList) throws WxErrorException {
if (datetime == null) {
throw new RuntimeException("datetime can't be null");
throw new WxRuntimeException("datetime can't be null");
}
if (userIdList == null || userIdList.size() > USER_IDS_LIMIT) {
throw new RuntimeException("用户列表不能为空,不超过 " + USER_IDS_LIMIT + " 个,若用户超过 " + USER_IDS_LIMIT + " 个,请分批获取");
throw new WxRuntimeException("用户列表不能为空,不超过 " + USER_IDS_LIMIT + " 个,若用户超过 " + USER_IDS_LIMIT + " 个,请分批获取");
}
JsonArray jsonArray = new JsonArray();
@@ -186,7 +187,7 @@ public class WxCpOaServiceImpl implements WxCpOaService {
long starttimestamp = startTime.getTime() / 1000L;
if (endtimestamp - starttimestamp < 0 || endtimestamp - starttimestamp >= MONTH_SECONDS) {
throw new RuntimeException("受限于网络传输起止时间的最大跨度为30天如超过30天则以结束时间为基准向前取30天进行查询");
throw new WxRuntimeException("受限于网络传输起止时间的最大跨度为30天如超过30天则以结束时间为基准向前取30天进行查询");
}
jsonObject.addProperty("start_time", starttimestamp);

View File

@@ -5,6 +5,7 @@ import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
@@ -72,7 +73,7 @@ public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<Closeab
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.configStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
} catch (IOException e) {
throw new RuntimeException(e);
throw new WxRuntimeException(e);
}
}
return this.configStorage.getAccessToken();

View File

@@ -5,6 +5,7 @@ import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
import org.apache.http.client.config.RequestConfig;
@@ -68,7 +69,7 @@ public class WxCpServiceImpl extends WxCpServiceApacheHttpClientImpl {
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
getWxCpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
} catch (IOException e) {
throw new RuntimeException(e);
throw new WxRuntimeException(e);
}
} finally {
lock.unlock();