-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/karl/debugger/ui/core/exception/MethodInstanceBuilderException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
src/main/java/com/karl/debugger/ui/endpoint/BaseEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |