当前位置:网站首页>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
边栏推荐
- Some experiences of Arduino soft serial port communication
- Hisat2 - stringtie - deseq2 pipeline for bulk RNA seq
- “百度杯”CTF比赛 2017 二月场,Web:爆破-1
- Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
- Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
- Advanced APL (realize group chat room)
- 691. Cube IV
- Specified interval inversion in the linked list
- 你开发数据API最快多长时间?我1分钟就足够了
- Map interface and method
猜你喜欢

IP home online query platform

PAT甲级真题1166

Summary of Arduino serial functions related to print read
![[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)](/img/d8/b4f39d9637c9886a8c81ca125d6944.jpg)
[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)

Leetcode 198: 打家劫舍

IO stream system and FileReader, filewriter

带你全流程,全方位的了解属于测试的软件事故

3311. 最长算术

Circuit, packet and message exchange

Comparison of advantages and disadvantages between most complete SQL and NoSQL
随机推荐
Advanced API (UDP connection & map set & collection set)
Talk about floating
Advanced API (use of file class)
"Moss ma not found" solution
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
Summary of abnormal mechanism of interview
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
Various postures of CS without online line
Take you through the whole process and comprehensively understand the software accidents that belong to testing
Win 2008 R2 crashed at the final installation stage
树莓派更新工具链
Visit Google homepage to display this page, which cannot be displayed
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
Map interface and method
The embodiment of generics in inheritance and wildcards
SharePoint modification usage analysis report is more than 30 days
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
Introduction of transformation flow
Distributed transactions