更新 src/main/java/com/vibevault/exception/GlobalExceptionHandler.java
Some checks are pending
autograde-final-vibevault / check-trigger (push) Waiting to run
autograde-final-vibevault / grade (push) Blocked by required conditions

This commit is contained in:
张雪尔 2025-12-22 04:08:26 +08:00
parent 633d89272b
commit 3830a3b051

View File

@ -1,12 +1,13 @@
package com.vibevault.exception; package com.vibevault.exception;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
@ -21,11 +22,44 @@ import java.util.Map;
@RestControllerAdvice @RestControllerAdvice
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
// TODO: 实现 ResourceNotFoundException 处理器 (返回 404) // 处理 ResourceNotFoundException返回404
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Map<String, Object>> handleResourceNotFound(ResourceNotFoundException ex) {
Map<String, Object> error = buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND);
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}
// TODO: 实现 UnauthorizedException 处理器 (返回 403) // 处理 UnauthorizedException返回403
@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<Map<String, Object>> handleUnauthorized(UnauthorizedException ex) {
Map<String, Object> error = buildErrorResponse(ex.getMessage(), HttpStatus.FORBIDDEN);
return new ResponseEntity<>(error, HttpStatus.FORBIDDEN);
}
// TODO: 实现 ResponseStatusException 处理器 // 处理 ResponseStatusException返回对应状态码
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<Map<String, Object>> handleResponseStatus(ResponseStatusException ex) {
Map<String, Object> error = buildErrorResponse(ex.getReason(), ex.getStatusCode());
return new ResponseEntity<>(error, ex.getStatusCode());
}
// TODO [Advanced]: 实现通用异常处理器 // [Advanced] 统一处理其他异常返回500
@ExceptionHandler(Exception.class)
public ResponseEntity<Map<String, Object>> handleGenericException(Exception ex) {
Map<String, Object> error = buildErrorResponse(
"An unexpected error occurred: " + ex.getMessage(),
HttpStatus.INTERNAL_SERVER_ERROR
);
return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
// 构建统一的错误响应格式
private Map<String, Object> buildErrorResponse(String message, HttpStatusCode status) {
Map<String, Object> error = new HashMap<>();
error.put("timestamp", java.time.LocalDateTime.now().toString());
error.put("status", status.value());
error.put("error", status instanceof HttpStatus ? ((HttpStatus) status).getReasonPhrase() : "Unknown Error");
error.put("message", message);
return error;
}
} }