ADD more error handling

This commit is contained in:
2025-06-05 19:41:03 +02:00
parent e2eb9b365a
commit 7f829a70e1
2 changed files with 24 additions and 1 deletions

View File

@@ -2,12 +2,32 @@ package com.ddf.vodsystem.controllers;
import com.ddf.vodsystem.exceptions.JobNotFinished; import com.ddf.vodsystem.exceptions.JobNotFinished;
import com.ddf.vodsystem.exceptions.JobNotFound; import com.ddf.vodsystem.exceptions.JobNotFound;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
@ControllerAdvice @ControllerAdvice
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
@ExceptionHandler({ MultipartException.class })
public ResponseEntity<String> handleMultipartException(MultipartException ex) {
return ResponseEntity.badRequest().body("Request is not multipart/form-data.");
}
@ExceptionHandler({ MissingServletRequestPartException.class })
public ResponseEntity<String> handleMissingPart(MissingServletRequestPartException ex) {
return ResponseEntity.badRequest().body("Required file part is missing.");
}
@ExceptionHandler({ HttpMediaTypeNotSupportedException.class })
public ResponseEntity<String> handleUnsupportedMediaType(HttpMediaTypeNotSupportedException ex) {
return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
.body("Unsupported media type: expected multipart/form-data.");
}
@ExceptionHandler(IllegalArgumentException.class) @ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> handleIllegalArgument(IllegalArgumentException ex) { public ResponseEntity<String> handleIllegalArgument(IllegalArgumentException ex) {
return ResponseEntity.status(400).body(ex.getMessage()); return ResponseEntity.status(400).body(ex.getMessage());

View File

@@ -20,8 +20,11 @@ public class UploadController {
@PostMapping() @PostMapping()
public ResponseEntity<String> uploadVideo(@RequestParam("file") MultipartFile file) { public ResponseEntity<String> uploadVideo(@RequestParam("file") MultipartFile file) {
String uuid = uploadService.upload(file); if (file == null || file.isEmpty()) {
throw new IllegalArgumentException("Invalid file");
}
String uuid = uploadService.upload(file);
return new ResponseEntity<>(uuid, HttpStatus.OK); return new ResponseEntity<>(uuid, HttpStatus.OK);
} }
} }