Search in sources :

Example 51 with ForestRuntimeException

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);
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 52 with ForestRuntimeException

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());
    }
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestUnsupportException(com.dtflys.forest.exceptions.ForestUnsupportException) ForestInterceptorDefineException(com.dtflys.forest.exceptions.ForestInterceptorDefineException) ForestHandlerException(com.dtflys.forest.exceptions.ForestHandlerException) ForestVariableUndefinedException(com.dtflys.forest.exceptions.ForestVariableUndefinedException) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) ForestNoFileNameException(com.dtflys.forest.exceptions.ForestNoFileNameException) ForestFileNotFoundException(com.dtflys.forest.exceptions.ForestFileNotFoundException) Test(org.junit.Test)

Example 53 with ForestRuntimeException

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);
    }
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 54 with ForestRuntimeException

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;
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 55 with ForestRuntimeException

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.");
}
Also used : ForestRequestType(com.dtflys.forest.http.ForestRequestType) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Aggregations

ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)64 Test (org.junit.Test)14 Map (java.util.Map)9 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)7 MetaRequest (com.dtflys.forest.reflection.MetaRequest)6 ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)5 MappingParameter (com.dtflys.forest.mapping.MappingParameter)5 Method (java.lang.reflect.Method)5 Parameter (java.lang.reflect.Parameter)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 SSLKeyStore (com.dtflys.forest.ssl.SSLKeyStore)4 IOException (java.io.IOException)4 Annotation (java.lang.annotation.Annotation)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 MalformedURLException (java.net.MalformedURLException)4 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)4 ForestConverter (com.dtflys.forest.converter.ForestConverter)3 ForestRequest (com.dtflys.forest.http.ForestRequest)3 Interceptor (com.dtflys.forest.interceptor.Interceptor)3