From ef742dab67bbe4379864868ff195efc9c3d77c34 Mon Sep 17 00:00:00 2001 From: ThisBirchWood Date: Tue, 13 May 2025 21:41:57 +0200 Subject: [PATCH] ADD improved error handling --- .../controllers/DownloadController.java | 10 -------- .../vodsystem/controllers/EditController.java | 5 ---- .../controllers/GlobalExceptionHandler.java | 25 +++++++++++++++++++ .../com/ddf/vodsystem/entities/JobStatus.java | 3 ++- .../vodsystem/exceptions/FFMPEGException.java | 7 ++++++ .../services/CompressionService.java | 7 +++++- .../ddf/vodsystem/services/EditService.java | 6 +++++ 7 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/ddf/vodsystem/controllers/GlobalExceptionHandler.java create mode 100644 src/main/java/com/ddf/vodsystem/exceptions/FFMPEGException.java diff --git a/src/main/java/com/ddf/vodsystem/controllers/DownloadController.java b/src/main/java/com/ddf/vodsystem/controllers/DownloadController.java index 50e604e..937e432 100644 --- a/src/main/java/com/ddf/vodsystem/controllers/DownloadController.java +++ b/src/main/java/com/ddf/vodsystem/controllers/DownloadController.java @@ -35,14 +35,4 @@ public class DownloadController { .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); } - - @ExceptionHandler(JobNotFound.class) - public ResponseEntity handleFileNotFound(JobNotFound ex) { - return ResponseEntity.status(404).body(ex.getMessage()); - } - - @ExceptionHandler(JobNotFinished.class) - public ResponseEntity handleJobNotFinished(JobNotFinished ex) { - return ResponseEntity.status(404).body(ex.getMessage()); - } } diff --git a/src/main/java/com/ddf/vodsystem/controllers/EditController.java b/src/main/java/com/ddf/vodsystem/controllers/EditController.java index 1e4be78..e2c05f7 100644 --- a/src/main/java/com/ddf/vodsystem/controllers/EditController.java +++ b/src/main/java/com/ddf/vodsystem/controllers/EditController.java @@ -33,9 +33,4 @@ public class EditController { return new ResponseEntity<>(editService.getProgress(uuid), HttpStatus.OK); } - @ExceptionHandler(IllegalArgumentException.class) - public ResponseEntity handleIllegalArgument(IllegalArgumentException ex) { - return ResponseEntity.status(404).body(ex.getMessage()); - } - } diff --git a/src/main/java/com/ddf/vodsystem/controllers/GlobalExceptionHandler.java b/src/main/java/com/ddf/vodsystem/controllers/GlobalExceptionHandler.java new file mode 100644 index 0000000..3d31742 --- /dev/null +++ b/src/main/java/com/ddf/vodsystem/controllers/GlobalExceptionHandler.java @@ -0,0 +1,25 @@ +package com.ddf.vodsystem.controllers; + +import com.ddf.vodsystem.exceptions.JobNotFinished; +import com.ddf.vodsystem.exceptions.JobNotFound; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleIllegalArgument(IllegalArgumentException ex) { + return ResponseEntity.status(400).body(ex.getMessage()); + } + + @ExceptionHandler(JobNotFound.class) + public ResponseEntity handleFileNotFound(JobNotFound ex) { + return ResponseEntity.status(404).body(ex.getMessage()); + } + + @ExceptionHandler(JobNotFinished.class) + public ResponseEntity handleJobNotFinished(JobNotFinished ex) { + return ResponseEntity.status(202).body(ex.getMessage()); + } +} diff --git a/src/main/java/com/ddf/vodsystem/entities/JobStatus.java b/src/main/java/com/ddf/vodsystem/entities/JobStatus.java index f831933..3b6a04a 100644 --- a/src/main/java/com/ddf/vodsystem/entities/JobStatus.java +++ b/src/main/java/com/ddf/vodsystem/entities/JobStatus.java @@ -4,5 +4,6 @@ public enum JobStatus { NOT_READY, PENDING, RUNNING, - FINISHED + FINISHED, + FAILED } diff --git a/src/main/java/com/ddf/vodsystem/exceptions/FFMPEGException.java b/src/main/java/com/ddf/vodsystem/exceptions/FFMPEGException.java new file mode 100644 index 0000000..a0c01e7 --- /dev/null +++ b/src/main/java/com/ddf/vodsystem/exceptions/FFMPEGException.java @@ -0,0 +1,7 @@ +package com.ddf.vodsystem.exceptions; + +public class FFMPEGException extends RuntimeException { + public FFMPEGException(String message) { + super(message); + } +} diff --git a/src/main/java/com/ddf/vodsystem/services/CompressionService.java b/src/main/java/com/ddf/vodsystem/services/CompressionService.java index 1474d22..ad9663e 100644 --- a/src/main/java/com/ddf/vodsystem/services/CompressionService.java +++ b/src/main/java/com/ddf/vodsystem/services/CompressionService.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.ddf.vodsystem.exceptions.FFMPEGException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -109,7 +110,6 @@ public class CompressionService { logger.info("FFMPEG starting..."); ProcessBuilder pb = buildCommand(job.getInputFile(), job.getOutputFile(), job.getClipConfig()); - //pb.redirectErrorStream(true); Process process = pb.start(); job.setStatus(JobStatus.RUNNING); @@ -127,6 +127,11 @@ public class CompressionService { } } + if (process.waitFor() != 0) { + job.setStatus(JobStatus.FAILED); + throw new FFMPEGException("FFMPEG process failed"); + } + job.setStatus(JobStatus.FINISHED); logger.info("FFMPEG finished"); } diff --git a/src/main/java/com/ddf/vodsystem/services/EditService.java b/src/main/java/com/ddf/vodsystem/services/EditService.java index 1b19c44..3f77e35 100644 --- a/src/main/java/com/ddf/vodsystem/services/EditService.java +++ b/src/main/java/com/ddf/vodsystem/services/EditService.java @@ -23,6 +23,12 @@ public class EditService { } } + if (clipConfig.getFileSize() != null) { + if (clipConfig.getFileSize() < 100) { + throw new IllegalArgumentException("File size cannot be less than 100kb"); + } + } + job.setClipConfig(clipConfig); }