Skip to content

Commit

Permalink
AbstractBot isCatchException 过期, 使用新的参数 isIgnoreException更加直接易懂 fix #592
Browse files Browse the repository at this point in the history
  • Loading branch information
venusdrogon committed Sep 11, 2023
1 parent 22a419e commit 7e9fb99
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.feilong.net.bot;

import static com.feilong.core.lang.StringUtil.formatPattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.lang.StringUtil;

/**
* 通用.
*
Expand All @@ -35,83 +35,158 @@ public abstract class AbstractBot implements Bot{
/** 是否异步,默认false,表示是同步. */
protected boolean isAsync = false;

/** 是否捕获异常,默认false,表示不捕获异常. */
protected boolean isCatchException = false;

//---------------------------------------------------------------

/**
* 获得 是否异步,默认false,表示是同步.
*
* @return the isAsync
* 是否捕获异常,默认false,表示不捕获异常.
*
* @deprecated since 3.5.2 请使用 {@link #isThrowException} 如果两个值都设置以 {@link #isThrowException} 为准
*/
public boolean getIsAsync(){
return isAsync;
}
@Deprecated
protected boolean isCatchException = false;

/**
* 设置 是否异步,默认false,表示是同步.
*
* @param isAsync
* the isAsync to set
* 当出现异常, 是否抛出异常.
*
* <p>
* 默认false,表示不抛出exception; 如果是true 那么会抛出异常,需要自定捕获异常处理.
* </p>
*
* @since 3.5.2
*
*/
public void setIsAsync(boolean isAsync){
this.isAsync = isAsync;
}
protected boolean isThrowException = false;

/**
* 获得 是否捕获异常,默认false,表示不捕获异常.
*
* @return the isCatchException
*/
public boolean getIsCatchException(){
return isCatchException;
}
//---------------------------------------------------------------

/**
* 设置 是否捕获异常,默认false,表示不捕获异常.
* Send message.
*
* @param isCatchException
* the isCatchException to set
* @param content
* the content
* @return true, if successful
*/
public void setIsCatchException(boolean isCatchException){
this.isCatchException = isCatchException;
}

//---------------------------------------------------------------

@Override
public boolean sendMessage(String content){
if (!isAsync){
String type = StringUtil.formatPattern("[SyncSend[{}]Message]", this.getClass().getSimpleName());
String type = formatPattern("[SyncSend[{}]Message]", this.getClass().getSimpleName());
LOGGER.info("{},content:[{}]", type, content);
return handleSendMessage(content, type);
}

//异步
new Thread(() -> {
String type = StringUtil.formatPattern("[AsyncSend[{}]Message]", this.getClass().getSimpleName());
String type = formatPattern("[AsyncSend[{}]Message]", this.getClass().getSimpleName());
LOGGER.info("{},content:[{}]", type, content);
handleSendMessage(content, type);
}).start();

return true;
}

/**
* Handle send message.
*
* @param content
* the content
* @param type
* the type
* @return true, if successful
*/
protected boolean handleSendMessage(String content,String type){
try{
return doSendMessage(content);
}catch (Exception e){
if (!isCatchException){
if (!isCatchException || isThrowException){
throw e;
}
LOGGER.error(StringUtil.formatPattern("[{}],typecontent:[{}],returnFalse", type, content), e);

//只打印log 方便排查问题
LOGGER.error(formatPattern("[{}],typecontent:[{}],returnFalse", type, content), e);
return false;
}
}

//---------------------------------------------------------------

/**
* Do send message.
*
* @param content
* the content
* @return true, if successful
*/
protected abstract boolean doSendMessage(String content);

/**
* 获得 是否捕获异常,默认false,表示不捕获异常.
*
* @return the isCatchException
* @deprecated since 3.5.2 请使用 {@link #isThrowException} 如果两个值都设置以 {@link #isThrowException} 为准
*/
@Deprecated
public boolean getIsCatchException(){
return isCatchException;
}

/**
* 设置 是否捕获异常,默认false,表示不捕获异常.
*
* @param isCatchException
* the isCatchException to set
* @deprecated since 3.5.2 请使用 {@link #isThrowException} 如果两个值都设置以 {@link #isThrowException} 为准
*/
@Deprecated
public void setIsCatchException(boolean isCatchException){
this.isCatchException = isCatchException;
}

/**
* 当出现异常, 是否抛出异常.
*
* <p>
* 默认false,表示不抛出exception; 如果是true 那么会抛出异常,需要自定捕获异常处理.
* </p>
*
* @return 默认false,表示抛出, 会抛出exception;如果是true 那么不会抛出异常.
* @since 3.5.2
*/
public boolean getIsThrowException(){
return isThrowException;
}

/**
* 当出现异常, 是否抛出异常.
*
* <p>
* 默认false,表示不抛出exception; 如果是true 那么会抛出异常,需要自定捕获异常处理.
* </p>
*
* @param isThrowException
* 默认false,表示抛出, 会抛出exception;如果是true 那么不会抛出异常.
* @since 3.5.2
*/
public void setIsThrowException(boolean isThrowException){
this.isThrowException = isThrowException;
}

//---------------------------------------------------------------

/**
* 获得 是否异步,默认false,表示是同步.
*
* @return the isAsync
*/
public boolean getIsAsync(){
return isAsync;
}

/**
* 设置 是否异步,默认false,表示是同步.
*
* @param isAsync
* the isAsync to set
*/
public void setIsAsync(boolean isAsync){
this.isAsync = isAsync;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
* 默认的钉钉机器人.
*
* @author <a href="https://github.com/ifeilong/feilong">feilong</a>
* @see <a href=
* "https://developers.dingtalk.com/document/robots/custom-robot-access-1?spm=ding_open_doc.document.0.0.6d9d10afLWgSfH#topic-2026027">如何配置群机器人?</a>
*
* @since 3.1.0
*/
public class DefaultDingTalkBot extends AbstractBot implements DingTalkBot{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ public static DingTalkBot newAsyncCatchExceptionDingTalkBot(String accessToken,S
defaultDingTalkBot.setAccessToken(accessToken);
defaultDingTalkBot.setDefaultTitle(defaultTitle);
defaultDingTalkBot.setSecret(secret);

//是否异步,默认false,表示是同步.
defaultDingTalkBot.setIsAsync(true);
defaultDingTalkBot.setIsCatchException(true);

//当出现异常, 是否抛出异常.
//默认false,表示抛出, 会抛出exception;如果是true 那么不会抛出异常.
defaultDingTalkBot.setIsThrowException(false);
return defaultDingTalkBot;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (C) 2008 feilong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.net.bot;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.net.bot.dingtalk.DefaultDingTalkBot;
import com.feilong.net.bot.dingtalk.DingTalkBotBuilder;

/**
*
* @author <a href="https://github.com/ifeilong/feilong">feilong</a>
* @since 3.5.2
*/
public class LogAndBotCombinationTest{

private static final Logger LOGGER = LoggerFactory.getLogger(LogAndBotCombinationTest.class);

/** The bot. */
private final DefaultDingTalkBot bot = (DefaultDingTalkBot) DingTalkBotBuilder.newAsyncCatchExceptionDingTalkBot(
getKey(),
"SECd2325d14c67a3ec585568e00b49d749c7094a2a1579beb86369d88a5b161c981",
"test");

{

bot.setIsAsync(false);
}

@Test
public void test(){
LogAndBotCombination.error(LOGGER, bot, "hello {} error", "jim");
}

@Test
public void testerror(){
LogAndBotCombination.error(LOGGER, bot, "hello {} errorLastExceptionException", "jim", new NullPointerException("test"));
}

//---------------------------------------------------------------
@Test
public void testWarn(){
LogAndBotCombination.warn(LOGGER, bot, "hello {} warn", "jim");
}

@Test
public void testWarn2(){
LogAndBotCombination.warn(LOGGER, bot, "hello {} warnLastExceptionException", "jim", new NullPointerException("test"));
}

//---------------------------------------------------------------
@Test
public void testInfo(){
LogAndBotCombination.info(LOGGER, bot, "hello {} info", "jim");
}

@Test
public void testInfo2(){
LogAndBotCombination.info(LOGGER, bot, "hello {} infoLastExceptionException", "jim", new NullPointerException("test"));
}

//---------------------------------------------------------------
@Test
public void testDebug(){
LogAndBotCombination.debug(LOGGER, bot, "hello {} Debug", "jim");
}

@Test
public void testDebug2(){
LogAndBotCombination.debug(LOGGER, bot, "hello {} DebugLastExceptionException", "jim", new NullPointerException("test"));
}

//---------------------------------------------------------------
protected String getKey(){
//测试机器人
return "a99ef44aad3b80810d86529e9fb16902255a5928908f13fa15e9dbd75c7749d2";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
import com.feilong.net.http.HttpRequest;

/**
* 默认的微信机器人.
* 默认的企业微信机器人.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
* @see <a href="https://work.weixin.qq.com/help?person_id=1&doc_id=13376">如何配置群机器人?</a>
* @since 3.1.0 change packagename from com.feilong.net.bot.wxwork to com.feilong.net.bot.wxwork
*/
public class DefaultWxworkBot extends AbstractBot implements WxworkBot{
Expand Down

0 comments on commit 7e9fb99

Please sign in to comment.