Skip to content

Commit

Permalink
统一异常添加
Browse files Browse the repository at this point in the history
  • Loading branch information
kawhii committed Aug 5, 2018
1 parent df25c93 commit 01b06df
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.karl.debugger.ui.model.dto.MethodExecuteInstance;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


/**
* 默认的方法执行
Expand All @@ -17,6 +19,8 @@ public class DefaultMethodInvoker implements IMethodInvoker {
public Object invoke(MethodExecuteInstance instance) throws MethodInvokeException {
Object[] args = (instance.getArgs() != null && instance.getArgs().size() > 0) ? instance.getArgs().toArray() : null;
try {
Method method = instance.getMethod();
method.setAccessible(true);
return instance.getMethod().invoke(instance.getInstance(), args);
} catch (Exception e) {
throw new MethodInvokeException(e, instance);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.karl.debugger.ui.core;

import com.karl.debugger.ui.core.exception.InstanceException;
import com.karl.debugger.ui.core.exception.MethodInstanceBuilderException;
import com.karl.debugger.ui.model.dto.MethodExecuteInstance;
import com.karl.debugger.ui.model.dto.MethodExecuteOriginal;

Expand All @@ -15,7 +16,8 @@ public interface IMethodExecuteInstanceBuilder {
* 根据前端原始对象构建执行实例
* @param original 原始对象
* @return
* @throws ClassNotFoundException
* @throws MethodInstanceBuilderException 方法实例构建时抛出
* @throws InstanceException 获取实例失败时抛出
*/
MethodExecuteInstance build(MethodExecuteOriginal original) throws ClassNotFoundException, InstanceException, NoSuchMethodException;
MethodExecuteInstance build(MethodExecuteOriginal original) throws MethodInstanceBuilderException, InstanceException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.karl.debugger.ui.core.exception.InstanceException;
import com.karl.debugger.ui.core.exception.MethodInstanceBuilderException;
import com.karl.debugger.ui.model.dto.MethodExecuteInstance;
import com.karl.debugger.ui.model.dto.MethodExecuteOriginal;
import com.karl.debugger.ui.utils.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

Expand All @@ -30,18 +30,37 @@ public class SpringMethodExecuteInstanceBuilder implements IMethodExecuteInstanc
private SpringApplicationContextInstanceStrategy instanceStrategy;

@Override
public MethodExecuteInstance build(MethodExecuteOriginal original) throws ClassNotFoundException, InstanceException, NoSuchMethodException {
public MethodExecuteInstance build(MethodExecuteOriginal original) throws MethodInstanceBuilderException, InstanceException {
MethodExecuteInstance instance = new MethodExecuteInstance();
//获取类名
Class<?> clazz = ClassUtils.forName(original.getClassName(), null);
Class<?> clazz;
try {
clazz = ClassUtils.forName(original.getClassName(), null);
} catch (ClassNotFoundException e) {
throw new MethodInstanceBuilderException(e, original);
}
//执行实例
instance.setInstance(instanceStrategy.getInstance(clazz));

//参数类型
List<Class<?>> types = getTypes(original.getParamsTypes());
List<Class<?>> types ;
try {
types = getTypes(original.getParamsTypes());
} catch (ClassNotFoundException e) {
throw new MethodInstanceBuilderException(e, original);
}
Class<?>[] typesArr = types != null ? types.toArray(new Class[]{}) : null;
Method method = clazz.getDeclaredMethod(original.getMethodName(), typesArr);

//执行方法
Method method;
try {
//获取自身方法
method = clazz.getDeclaredMethod(original.getMethodName(), typesArr);
} catch (NoSuchMethodException e) {
throw new MethodInstanceBuilderException(e, original);
}
instance.setMethod(method);

try {
instance.setArgs(getParamsInstance(original.getParamsValue(), types));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.karl.debugger.ui.core.exception;

import com.karl.debugger.ui.model.dto.MethodExecuteOriginal;

/**
* 方法构建实例时出现异常时抛出
*
* @author karl
* @date 2018/8/5
*/
public class MethodInstanceBuilderException extends Exception {
private MethodExecuteOriginal original;

public MethodExecuteOriginal getOriginal() {
return original;
}

public MethodInstanceBuilderException(String message, Throwable cause, MethodExecuteOriginal original) {
super(message, cause);
this.original = original;
}

public MethodInstanceBuilderException(Throwable cause, MethodExecuteOriginal original) {
super(cause);
this.original = original;
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/karl/debugger/ui/endpoint/BaseEndpoint.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
package com.karl.debugger.ui.endpoint;

import com.karl.debugger.ui.core.exception.InstanceException;
import com.karl.debugger.ui.core.exception.MethodInstanceBuilderException;
import com.karl.debugger.ui.core.exception.MethodInvokeException;
import com.karl.debugger.ui.model.dto.EndpointResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
* 抽象的接口调用,主要负责上层统一异常
*
* @author karl
* @date 2018/8/5
*/
@ControllerAdvice
public abstract class BaseEndpoint {
/**
* 方法执行异常时调用
*
* @param e
* @return
*/
@ExceptionHandler(MethodInvokeException.class)
public EndpointResponse<?> invokeException(MethodInvokeException e) {

return EndpointResponse.fail("方法执行异常[" + exceptionToString(e) + "]" ,
e.getInstance().getArgs());

}

@ExceptionHandler(MethodInstanceBuilderException.class)
public EndpointResponse<?> methodInstanceBuilderException(MethodInstanceBuilderException e) {
return EndpointResponse.fail("构建方法实例异常[" + exceptionToString(e) + "]");
}

@ExceptionHandler(InstanceException.class)
public EndpointResponse<?> instanceException(InstanceException e) {
return EndpointResponse.fail("构建执行实例异常-[" + exceptionToString(e) + "]");
}

@ExceptionHandler(IOException.class)
public EndpointResponse<?> ioException(IOException e) {
e.printStackTrace();
return EndpointResponse.fail(500, "出现io异常-[" + exceptionToString(e) + "]");
}

@ExceptionHandler(Throwable.class)
public EndpointResponse<?> ioException(Throwable e) {
e.printStackTrace();
return EndpointResponse.fail(500, "系统异常[" + exceptionToString(e) + "]");
}

/**
* 把错误信息转成字符串
* @param throwable
* @return
*/
private String exceptionToString(Throwable throwable) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.toByteArray();
throwable.printStackTrace(new PrintStream(outputStream));
return new String(outputStream.toByteArray());
}
}

0 comments on commit 01b06df

Please sign in to comment.