package com.example.chiptest.exception;
import com.example.chiptest.Util.ResponseInformation;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.io.InvalidClassException;
import java.sql.SQLDataException;
import java.sql.SQLException;
/**
* @author luzey
* 统一异常处理
*/
@RestControllerAdvice //捕获各种异常 ,用文字描述
@ResponseBody
public class ExceptionControllerAdvice {
// @ExceptionHandler(value = 要处理的异常字节码)
@ExceptionHandler(value = IncorrectCredentialsException.class)
public ResponseInformation incorrectCredentialsException(IncorrectCredentialsException incorrectCredentialsException) {
System.err.println(incorrectCredentialsException.getMessage());
for (StackTraceElement stackTraceElement : incorrectCredentialsException.getStackTrace()) {
System.err.println(stackTraceElement);
}
return ResponseInformation.ERROR("用户名或密码错误");
}
@ExceptionHandler(value = IndexOutOfBoundsException.class)
public ResponseInformation indexOutOfBoundsException(IndexOutOfBoundsException indexOutOfBoundsException) {
System.err.println(indexOutOfBoundsException.getMessage());
for (StackTraceElement stackTraceElement : indexOutOfBoundsException.getStackTrace()) {
System.err.println(stackTraceElement);
}
return ResponseInformation.ERROR("数组角标越界");
}
@ExceptionHandler(value = NullPointerException.class)
public ResponseInformation nullPointerException(NullPointerException nullPointerException) {
System.err.println(nullPointerException.getMessage());
for (StackTraceElement stackTraceElement : nullPointerException.getStackTrace()) {
System.out.println("****************************************************************");
System.err.println(stackTraceElement);
System.out.println("****************************************************************");
}
return ResponseInformation.ERROR("空指针异常");
}
@ExceptionHandler(value = InvalidClassException.class)
public ResponseInformation invalidClassException(InvalidClassException invalidClassException) {
System.err.println(invalidClassException.getMessage());
for (StackTraceElement stackTraceElement : invalidClassException.getStackTrace()) {
System.err.println(stackTraceElement);
}
return ResponseInformation.ERROR("更改了可序列化的文件(例:BaseEntity),缓存中还在里面,clean+install即可解决问题");
}
@ExceptionHandler(value = SQLException.class)
public ResponseInformation invalidClassException(SQLException SQLException) {
System.err.println(SQLException.getMessage());
for (StackTraceElement stackTraceElement : SQLException.getStackTrace()) {
System.err.println(stackTraceElement);
}
return ResponseInformation.ERROR("SQL语句出现异常");
}
@ExceptionHandler(value = SQLDataException.class)
public ResponseInformation invalidClassException(SQLDataException sqlDataException) {
System.err.println(sqlDataException.getMessage());
for (StackTraceElement stackTraceElement : sqlDataException.getStackTrace()) {
System.err.println(stackTraceElement);
}
return ResponseInformation.ERROR("无法从某个字符串中确定类型");
}
@ExceptionHandler(value = Exception.class)
public ResponseInformation exception(Exception exception) {
System.err.println(exception.getMessage());
for (StackTraceElement stackTraceElement : exception.getStackTrace()) {
System.err.println(stackTraceElement);
}
return ResponseInformation.ERROR("系统繁忙");
}
}
【统一异常处理】可直接cv
最新推荐文章于 2024-07-19 17:37:28 发布