UPDATE backend API to return JSON-like objects
This commit is contained in:
@@ -1,45 +1,63 @@
|
||||
package com.ddf.vodsystem.controllers;
|
||||
|
||||
import com.ddf.vodsystem.entities.APIResponse;
|
||||
import com.ddf.vodsystem.exceptions.JobNotFinished;
|
||||
import com.ddf.vodsystem.exceptions.JobNotFound;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
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;
|
||||
|
||||
@ControllerAdvice
|
||||
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<String> handleMultipartException(MultipartException ex) {
|
||||
return ResponseEntity.badRequest().body("Request is not multipart/form-data.");
|
||||
public ResponseEntity<APIResponse<Void>> handleMultipartException(MultipartException ex) {
|
||||
logger.error("MultipartException: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, "Multipart request error: " + ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ MissingServletRequestPartException.class })
|
||||
public ResponseEntity<String> handleMissingPart(MissingServletRequestPartException ex) {
|
||||
return ResponseEntity.badRequest().body("Required file part is missing.");
|
||||
public ResponseEntity<APIResponse<Void>> handleMissingPart(MissingServletRequestPartException ex) {
|
||||
logger.error("MissingServletRequestPartException: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, "Required file part is missing.", null);
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ HttpMediaTypeNotSupportedException.class })
|
||||
public ResponseEntity<String> handleUnsupportedMediaType(HttpMediaTypeNotSupportedException ex) {
|
||||
return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
|
||||
.body("Unsupported media type: expected multipart/form-data.");
|
||||
public ResponseEntity<APIResponse<Void>> handleUnsupportedMediaType(HttpMediaTypeNotSupportedException ex) {
|
||||
logger.error("HttpMediaTypeNotSupportedException: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> 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<String> handleIllegalArgument(IllegalArgumentException ex) {
|
||||
return ResponseEntity.status(400).body(ex.getMessage());
|
||||
public ResponseEntity<APIResponse<Void>> handleIllegalArgument(IllegalArgumentException ex) {
|
||||
logger.error("IllegalArgumentException: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(JobNotFound.class)
|
||||
public ResponseEntity<String> handleFileNotFound(JobNotFound ex) {
|
||||
return ResponseEntity.status(404).body(ex.getMessage());
|
||||
public ResponseEntity<APIResponse<Void>> handleFileNotFound(JobNotFound ex) {
|
||||
logger.error("JobNotFound: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(JobNotFinished.class)
|
||||
public ResponseEntity<String> handleJobNotFinished(JobNotFinished ex) {
|
||||
return ResponseEntity.status(202).body(ex.getMessage());
|
||||
public ResponseEntity<APIResponse<Void>> handleJobNotFinished(JobNotFinished ex) {
|
||||
logger.error("JobNotFinished: {}", ex.getMessage(), ex);
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user