钉钉告警模版
r;
lkRobotClient;
wnMessage;
ssage;
sultEntity;
sultEntity;
StatusEnum;
OperationException;
xRuntimeException;
4j;
red;
ier;
nment;
tegrityViolationException;
ateKeyException;
atus;
ssageNotReadableException;
ception;
gResult;
rror;
diaTypeNotSupportedException;
questMethodNotSupportedException;
ArgumentNotValidException;
gServletRequestParameterException;
llerAdvice;
ionHandler;
Body;
Status;
lerFoundException;
aintViolation;
aintViolationException;
tionException;
ption;
riter;
riter;
Writer;
;
/**
*异常处理
*
*@authorHighEmotional
*@throws
*@date2019/5/2010:39
*/
@Slf4j
@ControllerAdvice
@ResponBody
publicclassGlobalHandlerException{
@Autowired
privateEnvironmentenvironment;
@Autowired
@Qualifier("dingTalkRobotClient")
privateDingTalkRobotClientclient;
/**
*@return默认处理所有的异常
*/
@ExceptionHandler()
publicBaResultEntitydefaultExceptionHandler(Exceptione){
StringmarkInfo=getMarkInfo(e,"系统异常业务告警");
rkdownMessage(newMarkdownMessage("系统异常业务告警",markInfo));
(sage(),e);
returnnewFailResultEntity("系统异常!");
}
/**
*@return调⽤其他服务异常
*/
@ExceptionHandler()
publicBaResultEntityHystrixRuntimeException(HystrixRuntimeExceptione){
StringrviceName=().getLocalizedMessage().replace("Exception:Loadbalancerdoesnothaveavailablerverforclie
StringmarkInfo=getMarkInfo(e,rviceName+"服务调⽤失败!");
rkdownMessage(newMarkdownMessage("服务调⽤异常业务告警",markInfo));
returnnewFailResultEntity("系统异常!");
}
/**
*@throws
*@Description:唯⼀约束异常
*@authorHighEmotional
*@date2019/3/716:15
*/
@ExceptionHandler()
publicBaResultEntityduplicateKeyException(DuplicateKeyExceptione){
String[]activeProfiles=iveProfiles();
ssage(newTextMessage("环境:"+activeProfiles[0]+""+newTextMessage(sage())));
(sage(),e);
returnnewFailResultEntity("数据重复!");
}
privatefinalstaticStringDATA_TOO_LONG_ERROR_MSG="Datatoolongforcolumn";
/**
*@throws
*@Description:唯⼀约束异常
*@Description:唯⼀约束异常
*@authorHighEmotional
*@date2019/3/716:15
*/
@ExceptionHandler()
publicBaResultEntitydataIntegrityViolationException(DataIntegrityViolationExceptione){
String[]activeProfiles=iveProfiles();
ssage(newTextMessage("环境:"+activeProfiles[0]+""+newTextMessage(sage())));
(sage(),e);
if(().getMessage().contains(DATA_TOO_LONG_ERROR_MSG)){
returnnewFailResultEntity("数据长度超出数据库设定长度!");
}
returnnewFailResultEntity("操作数据库异常!");
}
/**
*@throws
*@Description:错误的sql异常
*@authorHighEmotional
*@date2019/3/716:15
*/
@ExceptionHandler()
@ResponBody
publicBaResultEntitystreamOperationException(StreamOperationExceptione){
BaResultEntityresult=ult();
StringmarkInfo=getMarkInfo(e,ult().getErrmsg());
//SQL错误屏蔽
if(result!=null){
result=_e==code()?newFailResultEntity("系统异常!"):result;
rkdownMessage(newMarkdownMessage("SQL错误告警",markInfo));
returnresult;
}
rkdownMessage(newMarkdownMessage("⾃定义业务告警",markInfo));
returnresult;
}
/**
*400-BadRequest
*/
@ResponStatus()
@ExceptionHandler()
publicBaResultEntityhandleMissingServletRequestParServiceExceptionameterException(MissingServletRequestParameterExceptione){
("缺少请求参数",e);
returnnewFailResultEntity("缺少请求参数:"+ameterName());
}
/**
*400-BadRequest
*/
@ResponStatus()
@ExceptionHandler()
publicBaResultEntityhandleHttpMessageNotReadableException(HttpMessageNotReadableExceptione){
("参数解析失败",e);
returnnewFailResultEntity("参数解析失败");
}
/**
*400-BadRequest
*/
@ResponStatus()
@ExceptionHandler()
publicBaResultEntityhandleMethodArgumentNotValidException(MethodArgumentNotValidExceptione){
("参数验证失败",e);
("参数验证失败",e);
BindingResultresult=dingResult();
FieldErrorerror=ldError();
Stringcode=aultMessage();
Stringmessage=("%s",code);
returnnewFailResultEntity(message);
}
/**
*400-BadRequest
*/
@ResponStatus()
@ExceptionHandler()
publicBaResultEntityhandleBindException(BindExceptione){
("参数绑定失败",e);
BindingResultresult=dingResult();
FieldErrorerror=ldError();
Stringfield=ld();
Stringcode=aultMessage();
Stringmessage=("%s:%s",field,code);
returnnewFailResultEntity(message);
}
/**
*400-BadRequest
*/
@ResponStatus()
@ExceptionHandler()
publicBaResultEntityhandleServiceException(ConstraintViolationExceptione){
("参数验证失败",e);
Set
ConstraintViolation<?>violation=or().next();
Stringmessage=sage();
returnnewFailResultEntity(message);
}
/**
*400-BadRequest
*/
@ResponStatus()
@ExceptionHandler()
publicBaResultEntityhandleValidationException(ValidationExceptione){
("参数验证失败",e);
returnnewFailResultEntity("参数验证失败");
}
/**
*404-NotFound
*/
@ResponStatus()
@ExceptionHandler()
publicBaResultEntitynoHandlerFoundException(NoHandlerFoundExceptione){
("NotFound",e);
returnnewFailResultEntity("NotFound="+e);
}
/**
*405-MethodNotAllowed
*/
@ResponStatus()
@ExceptionHandler()
publicBaResultEntityhandleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedExceptione){
("不⽀持当前请求⽅法",e);
returnnewFailResultEntity("不⽀持当前请求⽅法");
returnnewFailResultEntity("不⽀持当前请求⽅法");
}
/**
*415-UnsupportedMediaType
*/
@ResponStatus()
@ExceptionHandler()
publicBaResultEntityhandleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedExceptione){
("不⽀持当前媒体类型",e);
returnnewFailResultEntity("不⽀持当前媒体类型");
}
/**
*得到标记信息
*
*@paramee
*@return{@linkString}
*/
StringgetMarkInfo(Exceptione,Stringmsg){
String[]activeProfiles=iveProfiles();
StringBuilderenv=newStringBuilder();
for(StringactiveProfile:activeProfiles){
(activeProfile+"|");
}
Stringtemplate="**告警环境:**"+env+"n"+"n"+
"**告警服务:**"+"web"+"n"+"n"+
"**告警信息:**"+msg+"n"+"n"+
"**告警堆栈:**"+"n"+
"```java"+"n"+"n"+getStackTraceInfo(e)+"n"+"```";
returntemplate;
}
/**
*得到堆栈跟踪信息
*
*@parameException
*@tackTrace()中的信息
*/
publicstaticStringgetStackTraceInfo(Exceptione){
StringWritersw=null;
PrintWriterpw=null;
try{
sw=newStringWriter();
pw=newPrintWriter(sw);
//将出错的栈信息输出到printWriter中
tackTrace(pw);
();
();
Strings=ng();
()>500?ing(0,499):s;
}catch(Exceptionex){
return"发⽣错误";
}finally{
if(sw!=null){
try{
();
}catch(IOExceptione1){
tackTrace();
}
}
if(pw!=null){
();
}
}
}
}
}
本文发布于:2022-11-25 06:58:26,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/16963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |