forked from alibaba/COLA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fulan.zjf
committed
Jan 8, 2019
1 parent
194efe8
commit 5ac3bc3
Showing
7 changed files
with
135 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...framework/cola-core/src/main/java/com/alibaba/cola/exception/DefaultExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.alibaba.cola.exception; | ||
|
||
import com.alibaba.cola.dto.Command; | ||
import com.alibaba.cola.dto.Response; | ||
import com.alibaba.cola.logger.Logger; | ||
import com.alibaba.cola.logger.LoggerFactory; | ||
|
||
/** | ||
* DefaultExceptionHandler | ||
* | ||
* @author Frank Zhang | ||
* @date 2019-01-08 9:51 AM | ||
*/ | ||
public class DefaultExceptionHandler implements ExceptionHandlerI{ | ||
|
||
private Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class); | ||
|
||
public static DefaultExceptionHandler singleton = new DefaultExceptionHandler(); | ||
|
||
@Override | ||
public void handleException(Command cmd, Response response, Exception exception) { | ||
buildResponse(response, exception); | ||
printLog(cmd, response, exception); | ||
} | ||
|
||
private void printLog(Command cmd, Response response, Exception exception) { | ||
if(exception instanceof BizException || exception instanceof ParamException){ | ||
//biz exception is expected, only warn it | ||
logger.warn(buildErrorMsg(cmd, response)); | ||
} | ||
else{ | ||
//sys exception should be monitored, and pay attention to it | ||
logger.error(buildErrorMsg(cmd, response), exception); | ||
} | ||
} | ||
|
||
private String buildErrorMsg(Command cmd, Response response) { | ||
return "Process [" + cmd + "] failed, errorCode: " | ||
+ response.getErrCode() + " errorMsg:" | ||
+ response.getErrMessage(); | ||
} | ||
|
||
private void buildResponse(Response response, Exception exception) { | ||
if (exception instanceof AppException) { | ||
ErrorCodeI errCode = ((AppException) exception).getErrCode(); | ||
response.setErrCode(errCode.getErrCode()); | ||
} | ||
else { | ||
response.setErrCode(BasicErrorCode.S_UNKNOWN.getErrCode()); | ||
} | ||
response.setErrMessage(exception.getMessage()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...framework/cola-core/src/main/java/com/alibaba/cola/exception/ExceptionHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.alibaba.cola.exception; | ||
|
||
import com.alibaba.cola.common.ApplicationContextHelper; | ||
import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||
|
||
/** | ||
* ExceptionHandlerFactory | ||
* | ||
* @author Frank Zhang | ||
* @date 2019-01-08 9:51 AM | ||
*/ | ||
public class ExceptionHandlerFactory { | ||
|
||
public static ExceptionHandlerI getExceptionHandler(){ | ||
try { | ||
return ApplicationContextHelper.getBean(ExceptionHandlerI.class); | ||
} | ||
catch (NoSuchBeanDefinitionException ex){ | ||
return DefaultExceptionHandler.singleton; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters