当前位置:网站首页>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
边栏推荐
- Interfaces and related concepts
- [set theory] order relation (partial order relation | partial order set | example of partial order set)
- Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
- Advanced API (byte stream & buffer stream)
- Advanced API (UDP connection & map set & collection set)
- Lombok -- simplify code
- [plus de détails] dernière entrevue complète redis (50)
- Split small interface
- 20220319
- Introduction of transformation flow
猜你喜欢

IO stream system and FileReader, filewriter

Map interface and method
![[solved] sqlexception: invalid value for getint() - 'Tian Peng‘](/img/bf/f6310304d58d964b3d09a9d011ddb5.png)
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘

Use of other streams

“百度杯”CTF比赛 2017 二月场,Web:爆破-1

1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log

VMWare网络模式-桥接,Host-Only,NAT网络

Basic components and intermediate components

专题 | 同步 异步

你开发数据API最快多长时间?我1分钟就足够了
随机推荐
II. D3.js draw a simple figure -- circle
sharepoint 2007 versions
论文学习——鄱阳湖星子站水位时间序列相似度研究
IP home online query platform
Book recommendation~
Leetcode 213: 打家劫舍 II
Advanced API (character stream & net for beginners)
Arduino Serial系列函数 有关print read 的总结
C code production YUV420 planar format file
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
Use of framework
JS monitors empty objects and empty references
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘
Win 2008 R2 crashed at the final installation stage
GStreamer ffmpeg avdec decoded data flow analysis
Le Seigneur des anneaux: l'anneau du pouvoir
Take you through the whole process and comprehensively understand the software accidents that belong to testing
3311. 最长算术
树莓派更新工具链
Wireshark software usage