generated from Java-2025Fall/final-vibevault-template
完成作业
This commit is contained in:
parent
a8adb74b53
commit
9a9f5d4eb2
@ -7,25 +7,74 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 全局异常处理器
|
||||
*
|
||||
* 需要实现:
|
||||
* 已实现:
|
||||
* - 捕获 ResourceNotFoundException 并返回 404 状态码
|
||||
* - 捕获 UnauthorizedException 并返回 403 状态码
|
||||
* - 捕获 ResponseStatusException 并返回对应状态码
|
||||
* - [Advanced] 统一处理其他异常,返回合适的错误响应格式
|
||||
* - 统一处理其他异常,返回合适的错误响应格式
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
// TODO: 实现 ResourceNotFoundException 处理器 (返回 404)
|
||||
/**
|
||||
* 处理 ResourceNotFoundException,返回 404 状态码
|
||||
*/
|
||||
@ExceptionHandler(ResourceNotFoundException.class)
|
||||
public ResponseEntity<Map<String, Object>> handleResourceNotFoundException(ResourceNotFoundException ex) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("timestamp", LocalDateTime.now());
|
||||
response.put("status", HttpStatus.NOT_FOUND.value());
|
||||
response.put("error", "Not Found");
|
||||
response.put("message", ex.getMessage());
|
||||
|
||||
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
// TODO: 实现 UnauthorizedException 处理器 (返回 403)
|
||||
/**
|
||||
* 处理 UnauthorizedException,返回 403 状态码
|
||||
*/
|
||||
@ExceptionHandler(UnauthorizedException.class)
|
||||
public ResponseEntity<Map<String, Object>> handleUnauthorizedException(UnauthorizedException ex) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("timestamp", LocalDateTime.now());
|
||||
response.put("status", HttpStatus.FORBIDDEN.value());
|
||||
response.put("error", "Forbidden");
|
||||
response.put("message", ex.getMessage());
|
||||
|
||||
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
// TODO: 实现 ResponseStatusException 处理器
|
||||
/**
|
||||
* 处理 ResponseStatusException,返回对应状态码
|
||||
*/
|
||||
@ExceptionHandler(ResponseStatusException.class)
|
||||
public ResponseEntity<Map<String, Object>> handleResponseStatusException(ResponseStatusException ex) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("timestamp", LocalDateTime.now());
|
||||
response.put("status", ex.getStatusCode().value());
|
||||
response.put("error", "HTTP Error " + ex.getStatusCode().value());
|
||||
response.put("message", ex.getReason());
|
||||
|
||||
return new ResponseEntity<>(response, ex.getStatusCode());
|
||||
}
|
||||
|
||||
// TODO [Advanced]: 实现通用异常处理器
|
||||
/**
|
||||
* 处理其他所有异常,返回 500 状态码
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Map<String, Object>> handleGenericException(Exception ex) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("timestamp", LocalDateTime.now());
|
||||
response.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
|
||||
response.put("error", "Internal Server Error");
|
||||
response.put("message", "An unexpected error occurred");
|
||||
|
||||
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user