当前位置:网站首页>Unified handling and interception of exception exceptions of vertx
Unified handling and interception of exception exceptions of vertx
2022-07-03 07:25:00 【Sleeping Empire】
Introduce
Vertx Unified exception interception processing
1. maven Project dependence
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.lance.common</groupId>
<artifactId>vertx-common-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2. Unified exception definition
public class AbsException extends RuntimeException {
public AbsException() {
}
public AbsException(String message) {
super(message);
}
public AbsException(String message, Throwable cause) {
super(message, cause);
}
public AbsException(Throwable cause) {
super(cause);
}
public AbsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
public class ServiceException extends AbsException {
private final static String DEFAULT_FAIL_CODE = "-1";
private final static String DEFAULT_MESSAGE = "failure";
@Getter
private final String code;
private ServiceException(String code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
public static ServiceException of(String code) {
return new ServiceException(code, DEFAULT_MESSAGE, null);
}
public static ServiceException of(Throwable cause) {
return new ServiceException(DEFAULT_FAIL_CODE, DEFAULT_MESSAGE, cause);
}
public static ServiceException of(String message, Throwable cause) {
return new ServiceException(DEFAULT_FAIL_CODE, message, cause);
}
public static ServiceException of(String code, String message) {
return new ServiceException(code, message, null);
}
public static ServiceException of(String code, String message, Throwable cause) {
return new ServiceException(code, message, cause);
}
public static ServiceException of(IResultCode errorCode) {
return new ServiceException(errorCode.getCode(), errorCode.getMessage(), null);
}
}
3. Exception unified interception implementation
@Slf4j
public final class DefaultExceptionHandler implements Handler<RoutingContext> {
private DefaultExceptionHandler() {
}
@Override
public void handle(RoutingContext ctx) {
Throwable throwable = ctx.failure();
log.error("===> Default exception fail: ", throwable);
if (throwable instanceof NullPointerException) {
ctx.json(R.fail(" Null pointer exception error ."));
return;
}
if (throwable instanceof ServiceException) {
ServiceException ex = (ServiceException) throwable;
ctx.json(R.fail(ex.getCode(), ex.getMessage()));
return;
}
// Define other exception types
// .......
ctx.json(R.fail(ResultCode.FAILURE));
}
public static DefaultExceptionHandler of() {
return new DefaultExceptionHandler();
}
}
4. Exception unified interception configuration
// Unified exception handling interception
mainRouter.route().last().failureHandler(DefaultExceptionHandler.of());
5. Full address of the project
Vertx And Exception Exception unified processing interception Github Address
Vertx And Exception Exception unified processing interception Gitee Address
边栏推荐
- 深度学习参数初始化(一)Xavier初始化 含代码
- TypeScript let與var的區別
- The difference between typescript let and VaR
- Advanced API (character stream & net for beginners)
- 【CMake】CMake链接SQLite库
- PAT甲级真题1166
- Spa single page application
- Advanced API (UDP connection & map set & collection set)
- "Moss ma not found" solution
- Le Seigneur des anneaux: l'anneau du pouvoir
猜你喜欢

Custom generic structure

Qtip2 solves the problem of too many texts

不出网上线CS的各种姿势

Use of file class

TCP cumulative acknowledgement and window value update

你开发数据API最快多长时间?我1分钟就足够了
![[solved] sqlexception: invalid value for getint() - 'Tian Peng‘](/img/bf/f6310304d58d964b3d09a9d011ddb5.png)
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘

Common architectures of IO streams

Topic | synchronous asynchronous

7.2刷题两个
随机推荐
Basic knowledge about SQL database
[solved] win10 cannot find a solution to the local group policy editor
When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
Raspberry pie update tool chain
【CMake】CMake链接SQLite库
LeetCode
Use of framework
Advanced API (local simulation download file)
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
Margin left: -100% understanding in the Grail layout
2021-07-18
Hash table, generic
Dora (discover offer request recognition) process of obtaining IP address
3311. Longest arithmetic
Qtip2 solves the problem of too many texts
Final, override, polymorphism, abstraction, interface
Download address collection of various versions of devaexpress
Pat grade a real problem 1166
Jeecg menu path display problem
Visit Google homepage to display this page, which cannot be displayed