package com.rongyichuang.common.exception;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.security.access.AccessDeniedException;
|
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.validation.BindException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.dao.DataIntegrityViolationException;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 全局异常处理器
|
*/
|
@RestControllerAdvice
|
public class GlobalExceptionHandler {
|
|
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
/**
|
* 业务异常处理
|
*/
|
@ExceptionHandler(BusinessException.class)
|
public ResponseEntity<Map<String, Object>> handleBusinessException(BusinessException e) {
|
logger.warn("业务异常: {}", e.getMessage());
|
Map<String, Object> response = new HashMap<>();
|
response.put("success", false);
|
response.put("message", e.getMessage());
|
response.put("code", e.getCode());
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
}
|
|
/**
|
* 参数校验异常处理
|
*/
|
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
|
public ResponseEntity<Map<String, Object>> handleValidationException(Exception e) {
|
logger.warn("参数校验异常: {}", e.getMessage());
|
Map<String, Object> response = new HashMap<>();
|
response.put("success", false);
|
response.put("message", "参数校验失败");
|
response.put("code", "VALIDATION_ERROR");
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
}
|
|
/**
|
* 认证异常处理
|
*/
|
@ExceptionHandler(BadCredentialsException.class)
|
public ResponseEntity<Map<String, Object>> handleBadCredentialsException(BadCredentialsException e) {
|
logger.warn("认证异常: {}", e.getMessage());
|
Map<String, Object> response = new HashMap<>();
|
response.put("success", false);
|
response.put("message", "用户名或密码错误");
|
response.put("code", "AUTHENTICATION_ERROR");
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
|
}
|
|
/**
|
* 权限异常处理
|
*/
|
@ExceptionHandler(AccessDeniedException.class)
|
public ResponseEntity<Map<String, Object>> handleAccessDeniedException(AccessDeniedException e) {
|
logger.warn("权限异常: {}", e.getMessage());
|
Map<String, Object> response = new HashMap<>();
|
response.put("success", false);
|
response.put("message", "权限不足");
|
response.put("code", "ACCESS_DENIED");
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(response);
|
}
|
|
/**
|
* 数据库约束违反异常处理
|
*/
|
@ExceptionHandler(DataIntegrityViolationException.class)
|
public ResponseEntity<Map<String, Object>> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
|
logger.warn("数据库约束违反异常: {}", e.getMessage());
|
Map<String, Object> response = new HashMap<>();
|
response.put("success", false);
|
|
String message = e.getMessage();
|
if (message != null && message.contains("phone")) {
|
response.put("message", "手机号码已存在,请使用其他手机号码");
|
response.put("code", "PHONE_DUPLICATE");
|
} else if (message != null && message.contains("Duplicate entry")) {
|
response.put("message", "数据重复,请检查输入信息");
|
response.put("code", "DUPLICATE_DATA");
|
} else {
|
response.put("message", "数据保存失败,请检查输入信息");
|
response.put("code", "DATA_CONSTRAINT_VIOLATION");
|
}
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
}
|
|
/**
|
* 系统异常处理
|
*/
|
@ExceptionHandler(Exception.class)
|
public ResponseEntity<Map<String, Object>> handleException(Exception e) {
|
logger.error("系统异常", e);
|
Map<String, Object> response = new HashMap<>();
|
response.put("success", false);
|
response.put("message", "系统内部错误");
|
response.put("code", "SYSTEM_ERROR");
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
|
}
|
}
|