generated from Java-2025Fall/final-vibevault-template
更新 src/main/java/com/vibevault/exception/GlobalExceptionHandler.java
This commit is contained in:
parent
633d89272b
commit
3830a3b051
@ -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)
|
||||||
// TODO: 实现 UnauthorizedException 处理器 (返回 403)
|
public ResponseEntity<Map<String, Object>> handleResourceNotFound(ResourceNotFoundException ex) {
|
||||||
|
Map<String, Object> error = buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND);
|
||||||
// TODO: 实现 ResponseStatusException 处理器
|
return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
// TODO [Advanced]: 实现通用异常处理器
|
|
||||||
|
// 处理 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
// [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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user