Commit 6a601cc5 authored by yanzg's avatar yanzg

Merge branch 'ver1.1' of http://192.168.0.204/yzg/yzg-util

parents fa7876ee d00a44f0
......@@ -64,19 +64,22 @@ public class ExceptionHelper {
* @return 返回的数据
*/
public static ResponseResult<?> getError(Exception e, boolean isFull) {
if (e instanceof CodeTargetException) {
CodeTargetException code = (CodeTargetException) e;
return getError(code.getCode(), code.getMessage(), code.getTarget(), true);
} else if (e instanceof RuntimeCodeException) {
boolean isFullTo = e instanceof CodeTargetException || isFull;
boolean codeError = e instanceof CodeException;
ResponseResult<?> result;
if (e instanceof RuntimeCodeException) {
RuntimeCodeException code = (RuntimeCodeException) e;
return getError(code.getCode(), code.getMessage(), code.getTarget(), isFull);
result = getError(code.getCode(), code.getMessage(), code.getTarget(), isFullTo);
} else {
String msg = e.getMessage();
if (StringHelper.isEmpty(msg)) {
msg = e.getClass().getName();
}
return getError(ResultConstants.UNKNOW_ERROR, msg, e, isFull);
result = getError(ResultConstants.UNKNOW_ERROR, msg, e, isFullTo);
}
result.setCodeError(codeError);
return result;
}
private static ResponseResult<?> getError(String code, String msg, Object target, boolean isFull) {
......
......@@ -2,6 +2,9 @@ package com.yanzuoguang.util.vo;
import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.CodeTargetException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.exception.RuntimeCodeException;
import com.yanzuoguang.util.helper.StringHelper;
import io.swagger.annotations.ApiModelProperty;
......@@ -37,6 +40,54 @@ public class ResponseResult<T> extends BaseVo {
*/
@ApiModelProperty(value = "异常数据", notes = "当抛出异常时的数据,通常和code进行组合", required = true)
private Object target;
/**
* 是否是code错误
*/
@ApiModelProperty(value = "异常数据", notes = "当抛出异常时的数据,通常和code进行组合", required = true)
private boolean codeError = false;
/**
* 构造函数
*/
public ResponseResult() {
this(ResultConstants.SUCCESS, "操作成功");
}
/**
* 构造函数
*
* @param code 结果状态
* @param message 结果消息
*/
public ResponseResult(String code, String message) {
this(code, message, null);
}
/**
* 构造函数
*
* @param code 结果状态
* @param message 结果熊希
* @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;
}
/**
* 检查数据是否合法,不合法则抛出异常,合法则返回数据
......@@ -45,7 +96,14 @@ public class ResponseResult<T> extends BaseVo {
if (StringHelper.compare(code, ResultConstants.SUCCESS)) {
return this.data;
}
throw new CodeException(this.code, this.message, this.target);
if (this.codeError && this.target != null) {
throw new CodeTargetException(this.code, this.message, this.target);
} else if (this.codeError) {
throw new CodeException(this.code, this.message);
} else {
throw new RuntimeCodeException(this.code, this.message, this.target);
}
}
/**
......@@ -121,46 +179,21 @@ public class ResponseResult<T> extends BaseVo {
}
/**
* 构造函数
*/
public ResponseResult() {
this(ResultConstants.SUCCESS, "操作成功");
}
/**
* 构造函数
* 是否是code错误
*
* @param code 结果状态
* @param message 结果消息
* @return 是否是code错误
*/
public ResponseResult(String code, String message) {
this(code, message, null);
public boolean isCodeError() {
return codeError;
}
/**
* 构造函数
* 设置code错误
*
* @param code 结果状态
* @param message 结果熊希
* @param data 结果
* @param codeError code错误
*/
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;
public void setCodeError(boolean codeError) {
this.codeError = codeError;
}
/**
......@@ -211,7 +244,7 @@ public class ResponseResult<T> extends BaseVo {
}
}
public static final ResponseResult OK = new ResponseResult();
public static final ResponseResult<Object> OK = new ResponseResult();
/**
* 处理错误
......@@ -219,7 +252,7 @@ public class ResponseResult<T> extends BaseVo {
* @param target 错误源对象
* @return 处理的错误
*/
public static final ResponseResult error(Object target) {
public static final ResponseResult<Object> error(Object target) {
return error(ResultConstants.UNKNOW_ERROR, "未知异常", target);
}
......@@ -230,7 +263,7 @@ public class ResponseResult<T> extends BaseVo {
* @param message 错误消息
* @return 处理的错误
*/
public static final ResponseResult error(String code, String message) {
public static final ResponseResult<Object> error(String code, String message) {
return error(code, message, null);
}
......@@ -241,8 +274,18 @@ public class ResponseResult<T> extends BaseVo {
* @param target 错误标记
* @return 处理的错误
*/
public static final ResponseResult error(String message, Object target) {
return error(ResultConstants.UNKNOW_ERROR, message, null);
public static final ResponseResult<Object> error(String message, Object target) {
return error(ResultConstants.UNKNOW_ERROR, message, target);
}
/**
* 处理错误
*
* @param ex 错误消息
* @return 处理的错误
*/
public static final ResponseResult error(Exception ex) {
return ExceptionHelper.getError(ex);
}
/**
......@@ -253,9 +296,8 @@ public class ResponseResult<T> extends BaseVo {
* @param target 错误标记
* @return 处理的错误
*/
public static final ResponseResult error(String code, String message, Object target) {
ResponseResult ret = new ResponseResult(code, message, null, target);
return ret;
public static final ResponseResult<Object> error(String code, String message, Object target) {
return new ResponseResult<>(code, message, null, target);
}
/**
......
package base;
import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.CodeTargetException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.exception.RuntimeCodeException;
import com.yanzuoguang.util.vo.ResponseResult;
import org.junit.Assert;
import org.junit.Test;
public class TestException {
@Test
public void testCodeException() {
CodeException codeException = new CodeException("05", "message", "target");
ResponseResult<?> error = ExceptionHelper.getError(codeException);
Assert.assertEquals(codeException.getCode(), error.getCode());
Assert.assertEquals(codeException.getMessage(), error.getMessage());
Assert.assertNull(error.getTarget());
try {
error.check();
} catch (CodeException ex) {
Assert.assertEquals(ex.getClass(), CodeException.class);
Assert.assertEquals(codeException.getCode(), ex.getCode());
Assert.assertEquals(codeException.getMessage(), ex.getMessage());
Assert.assertNull(ex.getTarget());
}
}
@Test
public void testCodeTargetException() {
CodeTargetException codeException = new CodeTargetException("05", "message", "target");
ResponseResult<?> error = ExceptionHelper.getError(codeException);
Assert.assertEquals(codeException.getCode(), error.getCode());
Assert.assertEquals(codeException.getMessage(), error.getMessage());
Assert.assertEquals(codeException.getTarget(), error.getTarget());
try {
error.check();
} catch (CodeTargetException ex) {
Assert.assertEquals(ex.getClass(), CodeTargetException.class);
Assert.assertEquals(codeException.getCode(), ex.getCode());
Assert.assertEquals(codeException.getMessage(), ex.getMessage());
Assert.assertEquals(codeException.getTarget(), ex.getTarget());
}
}
@Test
public void testRuntimeCodeException() {
RuntimeCodeException codeException = new RuntimeCodeException("05", "message", "target");
ResponseResult<?> error = ExceptionHelper.getError(codeException);
Assert.assertEquals(codeException.getCode(), error.getCode());
Assert.assertEquals(codeException.getMessage(), error.getMessage());
Assert.assertNull(error.getTarget());
try {
error.check();
} catch (RuntimeCodeException ex) {
Assert.assertEquals(ex.getClass(), RuntimeCodeException.class);
Assert.assertEquals(codeException.getCode(), ex.getCode());
Assert.assertEquals(codeException.getMessage(), ex.getMessage());
Assert.assertNull(ex.getTarget());
}
}
@Test
public void testException() {
Exception codeException = new Exception("message");
ResponseResult<?> error = ExceptionHelper.getError(codeException);
Assert.assertEquals(ResultConstants.UNKNOW_ERROR, error.getCode());
Assert.assertEquals(codeException.getMessage(), error.getMessage());
Assert.assertNull(error.getTarget());
try {
error.check();
} catch (RuntimeCodeException ex) {
Assert.assertEquals(ex.getClass(), RuntimeCodeException.class);
Assert.assertEquals(ResultConstants.UNKNOW_ERROR, ex.getCode());
Assert.assertEquals(codeException.getMessage(), ex.getMessage());
Assert.assertNull(ex.getTarget());
}
}
}
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.vo.LogVo;
......@@ -67,11 +66,8 @@ public class AspectFeign {
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
if (result instanceof ResponseResult) {
ResponseResult<?> responseResult = (ResponseResult<?>) result;
if (!ResultConstants.SUCCESS.equals(responseResult.getCode())) {
throw new CodeException(responseResult.getCode(), responseResult.getMessage(), responseResult.getTarget());
responseResult.check();
}
}
return result;
} catch (CodeException e) {
ex = e;
......
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