initial commit

This commit is contained in:
pratik
2025-10-20 11:07:35 -05:00
commit 0dae94011c
14 changed files with 1136 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
package com.cfdeployer.exception;
import com.cfdeployer.model.CfDeployResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<CfDeployResponse> handleValidationExceptions(MethodArgumentNotValidException ex) {
log.error("Validation error occurred", 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);
});
String errorMessage = errors.entrySet().stream()
.map(entry -> entry.getKey() + ": " + entry.getValue())
.collect(Collectors.joining(", "));
CfDeployResponse response = CfDeployResponse.failure(
"Validation failed: " + errorMessage,
ex.getMessage()
);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<CfDeployResponse> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException ex) {
log.error("File size exceeded maximum allowed size", ex);
CfDeployResponse response = CfDeployResponse.failure(
"File size exceeded maximum allowed size. Please ensure your files are within the 500MB limit.",
ex.getMessage()
);
return ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE).body(response);
}
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<CfDeployResponse> handleIllegalArgumentException(IllegalArgumentException ex) {
log.error("Invalid argument provided", ex);
CfDeployResponse response = CfDeployResponse.failure(
ex.getMessage(),
ex.toString()
);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<CfDeployResponse> handleGenericException(Exception ex) {
log.error("Unexpected error occurred", ex);
CfDeployResponse response = CfDeployResponse.failure(
"An unexpected error occurred: " + ex.getMessage(),
ex.toString()
);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}