package com.filesystem.config; 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 java.io.IOException; import java.util.Map; @RestControllerAdvice public class GlobalExceptionHandler { /** * 处理所有运行时异常 */ @ExceptionHandler(RuntimeException.class) public ResponseEntity handleRuntimeException(RuntimeException e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("message", e.getMessage() != null ? e.getMessage() : "服务器内部错误")); } /** * 处理 IO 异常(如 ZIP 压缩失败) */ @ExceptionHandler(IOException.class) public ResponseEntity handleIOException(IOException e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("message", "文件操作失败: " + (e.getMessage() != null ? e.getMessage() : "未知错误"))); } /** * 处理所有其他异常 */ @ExceptionHandler(Exception.class) public ResponseEntity handleException(Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("message", "服务器错误: " + (e.getMessage() != null ? e.getMessage() : "未知错误"))); } }