/
Chebarik
/
PackageManagement
Обзор
Документация
Войти
/
Chebarik
/
PackageManagement
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/com/packagemanagement/presentation/exception/GlobalExceptionHandler.java
142 строки
6 KB
Николай Мехоношин
Implemented non functional requirements
14 апр 2026, 18:37
14 апр 2026, 18:37
2e86838
Код
Авторство
О чём код?
package com.packagemanagement.presentation.exception; import com.packagemanagement.domain.exception.InvalidPackageFormatException; import com.packagemanagement.domain.exception.PackageAlreadyExistsException; import com.packagemanagement.domain.exception.PackageNotFoundException; import com.packagemanagement.domain.exception.RateLimitExceededException; import com.packagemanagement.domain.exception.VersionNotFoundException; import com.packagemanagement.presentation.response.ApiResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.servlet.resource.NoResourceFoundException; import java.util.HashMap; import java.util.Map; @RestControllerAdvice public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); @ExceptionHandler(PackageNotFoundException.class) public ResponseEntity<ApiResponse<Void>> handlePackageNotFound(PackageNotFoundException ex) { log.warn("Package not found: {}", ex.getPackageId()); return ResponseEntity .status(HttpStatus.NOT_FOUND) .body(ApiResponse.error(ex.getMessage())); } @ExceptionHandler(VersionNotFoundException.class) public ResponseEntity<ApiResponse<Void>> handleVersionNotFound(VersionNotFoundException ex) { log.warn("Version not found: {}:{}", ex.getPackageId(), ex.getVersionNumber()); return ResponseEntity .status(HttpStatus.NOT_FOUND) .body(ApiResponse.error(ex.getMessage())); } @ExceptionHandler(PackageAlreadyExistsException.class) public ResponseEntity<ApiResponse<Void>> handlePackageAlreadyExists(PackageAlreadyExistsException ex) { log.warn("Package already exists: {}", ex.getPackageName()); return ResponseEntity .status(HttpStatus.CONFLICT) .body(ApiResponse.error(ex.getMessage())); } @ExceptionHandler(InvalidPackageFormatException.class) public ResponseEntity<ApiResponse<Void>> handleInvalidPackageFormat(InvalidPackageFormatException ex) { log.warn("Invalid package format: {}", ex.getExpectedFormat()); return ResponseEntity .status(HttpStatus.BAD_REQUEST) .body(ApiResponse.error(ex.getMessage())); } @ExceptionHandler(IllegalArgumentException.class) public ResponseEntity<ApiResponse<Void>> handleIllegalArgument(IllegalArgumentException ex) { log.warn("Invalid argument: {}", ex.getMessage()); return ResponseEntity .status(HttpStatus.BAD_REQUEST) .body(ApiResponse.error(ex.getMessage())); } @ExceptionHandler(IllegalStateException.class) public ResponseEntity<ApiResponse<Void>> handleIllegalState(IllegalStateException ex) { log.warn("Invalid state: {}", ex.getMessage()); return ResponseEntity .status(HttpStatus.CONFLICT) .body(ApiResponse.error(ex.getMessage())); } @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<ApiResponse<Map<String, String>>> handleValidationErrors( MethodArgumentNotValidException ex) { Map<String, String> errors = new HashMap<>(); ex.getBindingResult().getAllErrors().forEach(error -> { String fieldName = ((FieldError) error).getField(); String errorMessage = error.getDefaultMessage(); errors.put(fieldName, errorMessage); }); log.warn("Validation errors: {}", errors); return ResponseEntity .status(HttpStatus.BAD_REQUEST) .body(new ApiResponse<>(false, errors, "Validation failed", java.time.Instant.now().toString())); } @ExceptionHandler(MaxUploadSizeExceededException.class) public ResponseEntity<ApiResponse<Void>> handleMaxUploadSize(MaxUploadSizeExceededException ex) { log.warn("File upload too large: {}", ex.getMessage()); return ResponseEntity .status(HttpStatus.PAYLOAD_TOO_LARGE) .body(ApiResponse.error("File size exceeds maximum allowed size")); } @ExceptionHandler(RateLimitExceededException.class) public ResponseEntity<ApiResponse<Void>> handleRateLimitExceeded(RateLimitExceededException ex) { log.warn("Rate limit exceeded: {}", ex.getClientKey()); return ResponseEntity .status(HttpStatus.TOO_MANY_REQUESTS) .header("Retry-After", String.valueOf(ex.getRetryAfterSeconds())) .body(ApiResponse.error(ex.getMessage())); } @ExceptionHandler(BadCredentialsException.class) public ResponseEntity<ApiResponse<Void>> handleBadCredentials(BadCredentialsException ex) { log.warn("Authentication failed: {}", ex.getMessage()); return ResponseEntity .status(HttpStatus.UNAUTHORIZED) .body(ApiResponse.error(ex.getMessage())); } @ExceptionHandler(AccessDeniedException.class) public ResponseEntity<ApiResponse<Void>> handleAccessDenied(AccessDeniedException ex) { log.warn("Access denied: {}", ex.getMessage()); return ResponseEntity .status(HttpStatus.FORBIDDEN) .body(ApiResponse.error("Access denied. Insufficient permissions.")); } @ExceptionHandler(NoResourceFoundException.class) public ResponseEntity<ApiResponse<Void>> handleNoResourceFound(NoResourceFoundException ex) { return ResponseEntity .status(HttpStatus.NOT_FOUND) .body(ApiResponse.error("Resource not found")); } @ExceptionHandler(Exception.class) public ResponseEntity<ApiResponse<Void>> handleGenericException(Exception ex) { log.error("Unexpected error", ex); return ResponseEntity .status(HttpStatus.INTERNAL_SERVER_ERROR) .body(ApiResponse.error("An unexpected error occurred")); } }