Commit b1cff2dd authored by yanzg's avatar yanzg

整理框架

parent acede980
......@@ -3,8 +3,6 @@ package com.yanzuoguang.util.exception;
import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.vo.ResponseResult;
import java.util.logging.Logger;
/**
* 异常处理帮助类
*/
......@@ -57,7 +55,7 @@ public class ExceptionHelper {
public static ResponseResult getError(Exception e) {
if (e instanceof CodeException) {
CodeException code = (CodeException) e;
return new ResponseResult(code.getCode(), code.getMessage());
return new ResponseResult(code.getCode(), code.getMessage(), null, code.getTarget());
} else {
return new ResponseResult(ResultConstants.UNKNOW_ERROR, e.getMessage());
}
......
......@@ -24,6 +24,11 @@ public class ResponseResult<T> extends BaseVo {
*/
private T data;
/**
* 数据源,用于显示异常数据
*/
private Object target;
/**
* 获取结果状态码
*
......@@ -78,12 +83,32 @@ public class ResponseResult<T> extends BaseVo {
this.data = data;
}
/**
* 获取数据源
*
* @return 数据源
*/
public Object getTarget() {
return target;
}
/**
* 设置数据源
*
* @param target
*/
public void setTarget(Object target) {
this.target = target;
}
/**
* 构造函数
*/
public ResponseResult() {
this(ResultConstants.SUCCESS, "操作成功");
}
/**
* 构造函数
*
......@@ -91,8 +116,7 @@ public class ResponseResult<T> extends BaseVo {
* @param message 结果消息
*/
public ResponseResult(String code, String message) {
this.code = code;
this.message = message;
this(code, message, null);
}
/**
......@@ -103,9 +127,21 @@ public class ResponseResult<T> extends BaseVo {
* @param data 结果
*/
public ResponseResult(String code, String message, T data) {
this(code, message, data, null);
}
/**
* 构造函数
*
* @param code 结果状态
* @param message 结果熊希
* @param data 结果
*/
public ResponseResult(String code, String message, T data, Object target) {
this.code = code;
this.message = message;
this.data = data;
this.target = target;
}
/**
......@@ -122,4 +158,18 @@ public class ResponseResult<T> extends BaseVo {
ret.setData(data);
return ret;
}
/**
* 处理错误
*
* @param code
* @param message
* @param target
* @param <T>
* @return
*/
public static final <T extends Object> ResponseResult<T> error(String code, String message, T data, Object target) {
ResponseResult<T> ret = new ResponseResult<>(code, message, data, target);
return ret;
}
}
......@@ -40,30 +40,31 @@ public class FeignAspect extends AbstractValidateAspect {
Object result = null;
long start = System.currentTimeMillis();
try {
logger.info("=================[ {} ] feign params is {} =================", name, joinPoint.getArgs());
logger.info("[ {} ] feign params is {}", name, joinPoint.getArgs());
result = joinPoint.proceed();
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
if (result instanceof ResponseResult) {
ResponseResult responseResult = (ResponseResult) result;
if (!ResultConstants.SUCCESS.equals(responseResult.getCode())) {
throw new CodeException(responseResult.getCode(), responseResult.getMessage());
throw new CodeException(responseResult.getCode(), responseResult.getMessage(), responseResult.getTarget());
}
}
// 记录服务调用时间,请求日志
long end = System.currentTimeMillis();
logger.info("[ {} ] feign time ({})ms, result is {}", name, (end - start), result);
return result;
} catch (CodeException ex) {
logger.info("=================[ {} ] feign is error {} =================", name, ex);
result = new ResponseResult(ex.getCode(), ex.getMessage());
throw ex;
} catch (Exception e) {
ResponseResult responseResult = ExceptionHelper.getError(e);
logger.error("=================[ " + name + " ] feign is error {} =================", e);
result = responseResult;
throw new CodeException(responseResult.getCode(), responseResult.getMessage());
} finally {
// 记录服务调用时间,请求日志
long end = System.currentTimeMillis();
logger.info("=================[ {} ] feign time ({})ms, result is {} =================", name, (end - start), result);
logger.error("[ {} ] feign time ({})ms , error {}", name, (end - start), e);
if (e instanceof CodeException) {
throw e;
} else {
ResponseResult responseResult = ExceptionHelper.getError(e);
throw new CodeException(responseResult.getCode(), responseResult.getMessage(), responseResult.getTarget());
}
}
}
......
......@@ -93,17 +93,22 @@ public class WebAspect extends AbstractValidateAspect implements ThreadNext.Next
long start = System.currentTimeMillis();
String name = joinPoint.getSignature().getName();
try {
logger.info("=================[ {} ] request params is {} =================", name, joinPoint.getArgs());
logger.info("[ {} ] request params is {}", name, joinPoint.getArgs());
Object result = executeMethod(joinPoint, name);
if (result instanceof ResponseResult) {
responseResult = (ResponseResult) result;
} else {
responseResult = ResponseResult.result(result);
}
long end = System.currentTimeMillis();
logger.info("[ {} ] time ({})ms, result is {}", name, (end - start), responseResult);
return result;
} catch (Exception e) {
logger.error("=================[ " + name + " ] is error {} =================", e);
long end = System.currentTimeMillis();
logger.error("[ {} ] time ({})ms, is error {}", name, (end - start), e);
responseResult = ExceptionHelper.getError(e);
if (getReturnType(joinPoint).getTypeName().indexOf(ResponseResult.class.getName()) > -1) {
return responseResult;
......@@ -111,10 +116,8 @@ public class WebAspect extends AbstractValidateAspect implements ThreadNext.Next
throw e;
}
} finally {
long end = System.currentTimeMillis();
Log.threadCommit();
saveInterLogs(joinPoint, responseResult);
logger.info("=================[ {} ] time ({})ms, result is {} =================", name, (end - start), responseResult);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment