package com.ddf.vodsystem.controllers; import com.ddf.vodsystem.dto.APIResponse; import com.ddf.vodsystem.exceptions.AlreadyStreaming; import com.ddf.vodsystem.exceptions.FFMPEGException; import com.ddf.vodsystem.exceptions.JobNotFinished; import com.ddf.vodsystem.exceptions.JobNotFound; import com.ddf.vodsystem.exceptions.KeyNotFound; import com.ddf.vodsystem.exceptions.NotAuthenticated; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.HttpMediaTypeNotSupportedException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.support.MissingServletRequestPartException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @RestControllerAdvice public class GlobalExceptionHandler { private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); private static final String ERROR = "error"; @ExceptionHandler({ MultipartException.class }) public ResponseEntity> handleMultipartException(MultipartException ex) { logger.error("MultipartException: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, "Multipart request error: " + ex.getMessage(), null); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); } @ExceptionHandler({ MissingServletRequestPartException.class }) public ResponseEntity> handleMissingPart(MissingServletRequestPartException ex) { logger.error("MissingServletRequestPartException: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, "Required file part is missing.", null); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); } @ExceptionHandler({ HttpMediaTypeNotSupportedException.class }) public ResponseEntity> handleUnsupportedMediaType(HttpMediaTypeNotSupportedException ex) { logger.error("HttpMediaTypeNotSupportedException: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, "Unsupported media type: expected multipart/form-data.", null); return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE).body(response); } @ExceptionHandler(IllegalArgumentException.class) public ResponseEntity> handleIllegalArgument(IllegalArgumentException ex) { logger.error("IllegalArgumentException: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, ex.getMessage(), null); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); } @ExceptionHandler(JobNotFound.class) public ResponseEntity> handleFileNotFound(JobNotFound ex) { logger.error("JobNotFound: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, ex.getMessage(), null); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response); } @ExceptionHandler(JobNotFinished.class) public ResponseEntity> handleJobNotFinished(JobNotFinished ex) { logger.error("JobNotFinished: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, ex.getMessage(), null); return ResponseEntity.status(HttpStatus.ACCEPTED).body(response); } @ExceptionHandler(FFMPEGException.class) public ResponseEntity> handleFFMPEGException(FFMPEGException ex) { logger.error("FFMPEGException: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, "FFMPEG Error: Please upload a valid file", null); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } @ExceptionHandler(NotAuthenticated.class) public ResponseEntity> handleNotAuthenticated(NotAuthenticated ex) { logger.error("NotAuthenticated: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, ex.getMessage(), null); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response); } @ExceptionHandler(KeyNotFound.class) public ResponseEntity> handleKeyNotFound(KeyNotFound ex) { logger.error("KeyNotFound: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, ex.getMessage(), null); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response); } @ExceptionHandler(AlreadyStreaming.class) public ResponseEntity> handleAlreadyStreaming(AlreadyStreaming ex) { logger.error("AlreadyStreaming: {}", ex.getMessage()); APIResponse response = new APIResponse<>(ERROR, ex.getMessage(), null); return ResponseEntity.status(HttpStatus.CONFLICT).body(response); } }