use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class TestForestFastjsonConverter method testConvertToJsonError.
@Test
public void testConvertToJsonError() {
ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
Map map = new HashMap();
map.put("ref", map);
boolean error = false;
try {
forestFastjsonConverter.encodeToString(map);
} catch (ForestRuntimeException e) {
error = true;
assertNotNull(e.getCause());
}
assertTrue(error);
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class TestExceptions method testForestRuntimeException.
@Test
public void testForestRuntimeException() {
try {
throw new Exception("first Exception");
} catch (Exception e) {
try {
throw new ForestRuntimeException(e);
} catch (ForestRuntimeException fe) {
assertEquals(e, fe.getCause());
}
}
try {
throw new Exception("first Exception");
} catch (Exception e) {
try {
throw new ForestRuntimeException("second Exception", e);
} catch (ForestRuntimeException fe) {
assertEquals("second Exception", fe.getMessage());
assertEquals(e, fe.getCause());
}
}
try {
throw new ForestRuntimeException("runtime exception");
} catch (ForestRuntimeException fe) {
assertEquals("runtime exception", fe.getMessage());
}
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class ForestMultipartFactory method create.
public <M extends ForestMultipart<T, ?>> M create(Class<T> pType, String name, String fileName, T data, String contentType) {
if (data instanceof ForestMultipart) {
ForestMultipart multipart = (ForestMultipart) data;
if (StringUtils.isEmpty(multipart.getName()) && StringUtils.isNotEmpty(name)) {
multipart.setName(name);
}
if (StringUtils.isEmpty(multipart.getOriginalFileName()) && StringUtils.isNotEmpty(fileName)) {
multipart.setFileName(name);
}
return (M) multipart;
}
if (pType == null) {
pType = paramType;
}
Class<M> multipartType = multipartTypeMap.get(pType);
try {
M multipart = multipartType.newInstance();
multipart.setName(name);
multipart.setFileName(fileName);
multipart.setData(data);
multipart.setContentType(contentType);
return multipart;
} catch (InstantiationException e) {
throw new ForestRuntimeException(e);
} catch (IllegalAccessException e) {
throw new ForestRuntimeException(e);
}
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class DefaultObjectFactory method getObject.
/**
* 获取Forest接口对象(默认方式)
* <p>适用于Forest相关接口(非请求客户端接口)和回调函数的工厂接口
* <p>当这些类没有实例的情况下,会先实例化并缓存下来,以后再取会通过缓存获取对象
* <p>实例化方式:通过JDK反射去实例化对象
*
* @param clazz Forest对象接口类
* @param <T> Forest对象接口类泛型
* @return Forest对象实例
*/
@Override
public <T> T getObject(Class<T> clazz) {
if (clazz == null) {
return null;
}
Object obj = getObjectFromCache(clazz);
if (obj != null) {
return (T) obj;
}
ObjectConstructor<T> constructor = constructorMap.get(clazz);
if (constructor != null) {
obj = constructor.construct();
if (obj != null) {
forestObjectCache.put(clazz, obj);
return (T) obj;
}
}
try {
if (!clazz.isInterface()) {
obj = clazz.newInstance();
forestObjectCache.put(clazz, obj);
return (T) obj;
}
} catch (InstantiationException e) {
throw new ForestRuntimeException(e);
} catch (IllegalAccessException e) {
throw new ForestRuntimeException(e);
}
return null;
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class ForestMethod method type.
/**
* 获得最终的请求类型
* @param args 调用本对象对应方法时传入的参数数组
* @return 请求类型,{@link ForestRequestType}枚举实例
*/
private ForestRequestType type(Object[] args) {
String renderedType = typeTemplate.render(args);
if (StringUtils.isBlank(renderedType)) {
String typeFromName = methodNameItems[0];
ForestRequestType type = ForestRequestType.findType(typeFromName);
if (type != null) {
return type;
}
return ForestRequestType.GET;
}
ForestRequestType type = ForestRequestType.findType(renderedType);
if (type != null) {
return type;
}
throw new ForestRuntimeException("Http request type \"" + renderedType + "\" is not be supported.");
}
Aggregations