feat:增加统一异常处理
This commit is contained in:
parent
154a744fdc
commit
29a5277cf7
33
spring-boot/exceptionTest/.gitignore
vendored
Normal file
33
spring-boot/exceptionTest/.gitignore
vendored
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
50
spring-boot/exceptionTest/pom.xml
Normal file
50
spring-boot/exceptionTest/pom.xml
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.5.5</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.fanxb</groupId>
|
||||||
|
<artifactId>exceptionTest</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>exceptionTest</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.fanxb.exceptiontest;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ExceptionTestApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ExceptionTestApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.fanxb.exceptiontest.config;
|
||||||
|
|
||||||
|
import com.fanxb.exceptiontest.entity.Result;
|
||||||
|
import com.fanxb.exceptiontest.entity.exception.BaseException;
|
||||||
|
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
|
||||||
|
import com.fasterxml.jackson.databind.ser.Serializers;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-24-下午4:37
|
||||||
|
*/
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
public class ExceptionHandle {
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public Result handleException(Exception e) {
|
||||||
|
BaseException be;
|
||||||
|
if (e instanceof BaseException) {
|
||||||
|
be = (BaseException) e;
|
||||||
|
//手动抛出的异常,仅记录message
|
||||||
|
log.info("baseException:{}", be.getMessage());
|
||||||
|
if (be instanceof CustomBusinessException) {
|
||||||
|
//可在这里进行一些针对特定异常的处理逻辑
|
||||||
|
log.info("customBusinessException:{}", be.getMessage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//其它异常,非自动抛出的,无需给前端返回具体错误内容(用户不需要看见空指针之类的异常信息)
|
||||||
|
log.error("other exception:{}", e.getMessage(), e);
|
||||||
|
be = new BaseException("系统异常,请稍后重试", e);
|
||||||
|
}
|
||||||
|
return new Result(be.getCode(), be.getMessage(), null);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.fanxb.exceptiontest.controller;
|
||||||
|
|
||||||
|
import com.fanxb.exceptiontest.entity.Result;
|
||||||
|
import com.fanxb.exceptiontest.entity.exception.BaseException;
|
||||||
|
import com.fanxb.exceptiontest.entity.exception.CustomBusinessException;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-24-下午4:46
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
@GetMapping("/test1")
|
||||||
|
public Result test1() {
|
||||||
|
throw new BaseException("我是test1", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/test2")
|
||||||
|
public Result test2() {
|
||||||
|
throw new CustomBusinessException("我是test2", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/test3")
|
||||||
|
public Result test3() {
|
||||||
|
throw new RuntimeException("我是test2");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-24-下午4:39
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Result implements Serializable {
|
||||||
|
private static final long serialVersionUID = 451834802206432869L;
|
||||||
|
/**
|
||||||
|
* 1:成功,其他失败,具体数值在自定义的异常类中
|
||||||
|
*/
|
||||||
|
private int code;
|
||||||
|
/**
|
||||||
|
* message提示内容,当接口异常时提示异常信息
|
||||||
|
*/
|
||||||
|
private String message;
|
||||||
|
/**
|
||||||
|
* 携带真正需要返回的数据
|
||||||
|
*/
|
||||||
|
private Object data;
|
||||||
|
|
||||||
|
public static Result success(Object obj) {
|
||||||
|
return new Result(1, null, obj);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity.exception;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-24-下午5:13
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public class BaseException extends RuntimeException {
|
||||||
|
private static final long serialVersionUID = -3149747381632839680L;
|
||||||
|
/**
|
||||||
|
* 基本错误code为0
|
||||||
|
*/
|
||||||
|
private static final int BASE_CODE = 0;
|
||||||
|
private final int code;
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
public BaseException() {
|
||||||
|
this(null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseException(String message) {
|
||||||
|
this(message, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseException(Exception e) {
|
||||||
|
this(null, null, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseException(String message, Exception e) {
|
||||||
|
this(message, null, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected BaseException(String message, Integer code, Exception e) {
|
||||||
|
super(e);
|
||||||
|
this.message = message == null ? "" : message;
|
||||||
|
this.code = code == null ? BASE_CODE : code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
if (this.message != null && this.message.length() > 0) {
|
||||||
|
return this.message;
|
||||||
|
}
|
||||||
|
return super.getMessage();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.fanxb.exceptiontest.entity.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-26-上午10:41
|
||||||
|
*/
|
||||||
|
public class CustomBusinessException extends BaseException {
|
||||||
|
private static final long serialVersionUID = 1564935267302330109L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义业务异常错误码
|
||||||
|
*/
|
||||||
|
private static final int ERROR_CODE = -1;
|
||||||
|
|
||||||
|
public CustomBusinessException() {
|
||||||
|
super("自定义义务异常", ERROR_CODE, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomBusinessException(String message, Exception e) {
|
||||||
|
super(message, ERROR_CODE, e);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
|
Loading…
x
Reference in New Issue
Block a user