当前位置:网站首页>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
边栏推荐
- Common methods of file class
- Use of generics
- LeetCode
- [untitled]
- How to specify the execution order for multiple global exception handling classes
- Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
- II. D3.js draw a simple figure -- circle
- Some experiences of Arduino soft serial port communication
- 1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
- High concurrency memory pool
猜你喜欢
你开发数据API最快多长时间?我1分钟就足够了
Common APIs
PAT甲级真题1166
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
How long is the fastest time you can develop data API? One minute is enough for me
Margin left: -100% understanding in the Grail layout
Selenium key knowledge explanation
IPv4 address
Specified interval inversion in the linked list
随机推荐
4279. 笛卡尔树
sharepoint 2007 versions
C代码生产YUV420 planar格式文件
C WinForm framework
[set theory] partition (partition | partition example | partition and equivalence relationship)
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
Qtip2 solves the problem of too many texts
Selenium key knowledge explanation
4279. Cartesian tree
Longest common prefix and
高并发内存池
Topic | synchronous asynchronous
Download address collection of various versions of devaexpress
SecureCRT password to cancel session recording
20220319
Store WordPress media content on 4everland to complete decentralized storage
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
c语言指针的概念
PgSQL converts string to double type (to_number())
Some basic operations of reflection