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