2025-12-14 00:24:42 +08:00
|
|
|
|
package com.vibevault.exception;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
import org.springframework.web.server.ResponseStatusException;
|
|
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
2025-12-14 01:48:04 +08:00
|
|
|
|
import java.util.HashMap;
|
2025-12-14 00:24:42 +08:00
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 全局异常处理器
|
|
|
|
|
|
*
|
|
|
|
|
|
* 需要实现:
|
|
|
|
|
|
* - 捕获 ResourceNotFoundException 并返回 404 状态码
|
|
|
|
|
|
* - 捕获 UnauthorizedException 并返回 403 状态码
|
|
|
|
|
|
* - 捕获 ResponseStatusException 并返回对应状态码
|
|
|
|
|
|
* - [Advanced] 统一处理其他异常,返回合适的错误响应格式
|
|
|
|
|
|
*/
|
|
|
|
|
|
@RestControllerAdvice
|
|
|
|
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
|
2025-12-14 01:48:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 处理 ResourceNotFoundException 异常
|
|
|
|
|
|
* @param ex 异常对象
|
|
|
|
|
|
* @return 包含错误信息的 ResponseEntity
|
|
|
|
|
|
*/
|
|
|
|
|
|
@ExceptionHandler(ResourceNotFoundException.class)
|
|
|
|
|
|
public ResponseEntity<Map<String, Object>> handleResourceNotFoundException(ResourceNotFoundException ex) {
|
|
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
|
|
|
body.put("timestamp", LocalDateTime.now());
|
|
|
|
|
|
body.put("status", HttpStatus.NOT_FOUND.value());
|
|
|
|
|
|
body.put("error", "Not Found");
|
|
|
|
|
|
body.put("message", ex.getMessage());
|
|
|
|
|
|
|
|
|
|
|
|
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
|
|
|
|
|
|
}
|
2025-12-14 00:24:42 +08:00
|
|
|
|
|
2025-12-14 01:48:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 处理 UnauthorizedAccessException 异常
|
|
|
|
|
|
* @param ex 异常对象
|
|
|
|
|
|
* @return 包含错误信息的 ResponseEntity
|
|
|
|
|
|
*/
|
|
|
|
|
|
@ExceptionHandler(UnauthorizedAccessException.class)
|
|
|
|
|
|
public ResponseEntity<Map<String, Object>> handleUnauthorizedAccessException(UnauthorizedAccessException ex) {
|
|
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
|
|
|
body.put("timestamp", LocalDateTime.now());
|
|
|
|
|
|
body.put("status", HttpStatus.FORBIDDEN.value());
|
|
|
|
|
|
body.put("error", "Forbidden");
|
|
|
|
|
|
body.put("message", ex.getMessage());
|
|
|
|
|
|
|
|
|
|
|
|
return new ResponseEntity<>(body, HttpStatus.FORBIDDEN);
|
|
|
|
|
|
}
|
2025-12-14 00:24:42 +08:00
|
|
|
|
|
2025-12-14 01:48:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 处理 ResponseStatusException 异常
|
|
|
|
|
|
* @param ex 异常对象
|
|
|
|
|
|
* @return 包含错误信息的 ResponseEntity
|
|
|
|
|
|
*/
|
|
|
|
|
|
@ExceptionHandler(ResponseStatusException.class)
|
|
|
|
|
|
public ResponseEntity<Map<String, Object>> handleResponseStatusException(ResponseStatusException ex) {
|
|
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
|
|
|
body.put("timestamp", LocalDateTime.now());
|
|
|
|
|
|
body.put("status", ex.getStatusCode().value());
|
|
|
|
|
|
body.put("error", ex.getStatusCode().toString());
|
|
|
|
|
|
body.put("message", ex.getMessage());
|
|
|
|
|
|
|
|
|
|
|
|
return new ResponseEntity<>(body, ex.getStatusCode());
|
|
|
|
|
|
}
|
2025-12-14 00:24:42 +08:00
|
|
|
|
|
2025-12-14 01:48:04 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 处理所有其他未捕获的异常
|
|
|
|
|
|
* @param ex 异常对象
|
|
|
|
|
|
* @return 包含错误信息的 ResponseEntity
|
|
|
|
|
|
*/
|
|
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
|
|
|
public ResponseEntity<Map<String, Object>> handleGeneralException(Exception ex) {
|
|
|
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
|
|
|
body.put("timestamp", LocalDateTime.now());
|
|
|
|
|
|
body.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
|
|
|
|
body.put("error", "Internal Server Error");
|
|
|
|
|
|
body.put("message", "An unexpected error occurred");
|
|
|
|
|
|
|
|
|
|
|
|
// 在开发环境中可以添加异常堆栈信息
|
|
|
|
|
|
// body.put("stackTrace", Arrays.toString(ex.getStackTrace()));
|
|
|
|
|
|
|
|
|
|
|
|
return new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
}
|
2025-12-14 00:24:42 +08:00
|
|
|
|
}
|