use of com.dtflys.forest.exceptions.ForestHandlerException in project forest by dromara.
the class ResultHandler method getResult.
public Object getResult(ForestRequest request, ForestResponse response, Type resultType, Class resultClass) {
if (request.isDownloadFile()) {
return null;
}
Object result = response.getResult();
if (result != null && resultClass.isAssignableFrom(result.getClass())) {
return result;
}
if (isReceivedResponseData(response)) {
try {
if (void.class.isAssignableFrom(resultClass)) {
return null;
}
if (ForestResponse.class.isAssignableFrom(resultClass) || ForestRequest.class.isAssignableFrom(resultClass)) {
if (resultType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) resultType;
Class rowClass = (Class) parameterizedType.getRawType();
if (ForestResponse.class.isAssignableFrom(rowClass) || ForestRequest.class.isAssignableFrom(resultClass)) {
Type realType = parameterizedType.getActualTypeArguments()[0];
Class realClass = ReflectUtils.toClass(parameterizedType.getActualTypeArguments()[0]);
if (realClass == null) {
realClass = String.class;
}
Object realResult = getResult(request, response, realType, realClass);
response.setResult(realResult);
}
} else {
Object realResult = getResult(request, response, Object.class, Object.class);
response.setResult(realResult);
}
return response;
}
if (Future.class.isAssignableFrom(resultClass)) {
if (resultType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) resultType;
Class rowClass = (Class) parameterizedType.getRawType();
if (Future.class.isAssignableFrom(rowClass)) {
Type realType = parameterizedType.getActualTypeArguments()[0];
Class realClass = ReflectUtils.toClass(parameterizedType.getActualTypeArguments()[0]);
return getResult(request, response, realType, realClass);
}
}
}
if (resultClass.isArray()) {
if (byte[].class.isAssignableFrom(resultClass)) {
return response.getByteArray();
}
}
Object attFile = request.getAttachment(DownloadLifeCycle.ATTACHMENT_NAME_FILE);
if (attFile != null && attFile instanceof File) {
ForestConverter converter = request.getConfiguration().getConverter(ForestDataType.JSON);
return converter.convertToJavaObject(attFile, resultClass);
}
String responseText = null;
if (result != null && CharSequence.class.isAssignableFrom(result.getClass())) {
responseText = result.toString();
} else if (CharSequence.class.isAssignableFrom(resultClass)) {
try {
responseText = response.readAsString();
} catch (Throwable th) {
request.getLifeCycleHandler().handleError(request, response, th);
}
} else {
try {
responseText = response.getContent();
} catch (Throwable th) {
request.getLifeCycleHandler().handleError(request, response, th);
}
}
response.setContent(responseText);
if (CharSequence.class.isAssignableFrom(resultClass)) {
return responseText;
}
if (InputStream.class.isAssignableFrom(resultClass)) {
return response.getInputStream();
}
ContentType contentType = response.getContentType();
if (request.getDecoder() != null) {
if (contentType != null && contentType.canReadAsString()) {
return request.getDecoder().convertToJavaObject(responseText, resultType);
} else {
return request.getDecoder().convertToJavaObject(response.getByteArray(), resultType);
}
}
ForestDataType dataType = request.getDataType();
ForestConverter converter = request.getConfiguration().getConverter(dataType);
if (contentType != null && contentType.canReadAsString()) {
return converter.convertToJavaObject(responseText, resultType);
}
Charset charset = null;
String resCharset = response.getCharset();
if (resCharset != null) {
charset = Charset.forName(resCharset);
}
return converter.convertToJavaObject(response.getByteArray(), resultType, charset);
} catch (Exception e) {
throw new ForestHandlerException(e, request, response);
}
} else if (ForestResponse.class.isAssignableFrom(resultClass)) {
return response;
}
return null;
}
use of com.dtflys.forest.exceptions.ForestHandlerException in project forest by dromara.
the class TestExceptions method testForestHandlerException.
@Test
public void testForestHandlerException() {
ForestRequest<?> request = mock(ForestRequest.class);
ForestResponse<?> response = mock(ForestResponse.class);
try {
throw new ForestHandlerException("misc", request, response);
} catch (ForestHandlerException e) {
assertEquals("misc", e.getMessage());
assertEquals(request, e.getRequest());
assertEquals(response, e.getResponse());
e.setRequest(null);
assertNull(e.getRequest());
e.setResponse(null);
assertNull(e.getResponse());
}
try {
throw new Exception("first Exception");
} catch (Exception e) {
try {
throw new ForestHandlerException("second Exception", e, request, response);
} catch (ForestHandlerException fe) {
assertEquals("second Exception", fe.getMessage());
assertEquals(e, fe.getCause());
assertEquals(request, fe.getRequest());
assertEquals(response, fe.getResponse());
}
}
try {
throw new Exception("first Exception");
} catch (Exception e) {
try {
throw new ForestHandlerException(e, request, response);
} catch (ForestHandlerException fe) {
assertEquals(e, fe.getCause());
assertEquals(request, fe.getRequest());
assertEquals(response, fe.getResponse());
}
}
}
Aggregations