Refactor packages and add exception handler.
Renamed various classes to follow the "controlers" package structure for better organization and consistency. Added `ControllerExceptionHandler` to manage validation exceptions globally and improve error logging.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.alttd.forms.controlers;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ControllerAdvice
|
||||
public class ControllerExceptionHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);
|
||||
|
||||
public ControllerExceptionHandler() {
|
||||
logger.info("ControllerExceptionHandler initialized"); // Ensure this gets logged
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex) {
|
||||
logger.debug("invalid request", 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);
|
||||
});
|
||||
if (logger.isDebugEnabled()) {
|
||||
try {
|
||||
String errorsAsString = new ObjectMapper().writeValueAsString(errors);
|
||||
logger.debug("Invalid parameters\n{}", errorsAsString);
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.warn("Failed to parse errors to json", e);
|
||||
}
|
||||
}
|
||||
return ResponseEntity.badRequest().body(errors);
|
||||
}
|
||||
}
|
||||
+2
-5
@@ -1,4 +1,4 @@
|
||||
package com.alttd.forms.apply;
|
||||
package com.alttd.forms.controlers.apply;
|
||||
|
||||
import com.alttd.forms.form.StoreFormQuery;
|
||||
import com.alttd.forms.mail.verification.VerificationResult;
|
||||
@@ -8,10 +8,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alttd.forms.apply;
|
||||
package com.alttd.forms.controlers.apply;
|
||||
|
||||
import com.alttd.forms.form.Form;
|
||||
import jakarta.validation.constraints.*;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alttd.forms.contact;
|
||||
package com.alttd.forms.controlers.contact;
|
||||
import com.alttd.forms.form.StoreFormQuery;
|
||||
import com.alttd.forms.mail.verification.VerificationResult;
|
||||
import com.alttd.forms.mail.verification.Verify;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alttd.forms.contact;
|
||||
package com.alttd.forms.controlers.contact;
|
||||
|
||||
import com.alttd.forms.form.Form;
|
||||
import jakarta.validation.constraints.Email;
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.alttd.forms.verify_mail;
|
||||
|
||||
import com.alttd.forms.contact.ContactFormData;
|
||||
import com.alttd.forms.controlers.contact.ContactFormData;
|
||||
import com.alttd.forms.database.DatabaseConnection;
|
||||
import com.alttd.forms.form.Form;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import com.alttd.forms.contact.ContactFormData;
|
||||
import com.alttd.forms.controlers.contact.ContactFormData;
|
||||
import com.alttd.forms.form.StoreFormQuery;
|
||||
import com.alttd.forms.database.DatabaseConnection;
|
||||
import com.alttd.forms.form.Form;
|
||||
|
||||
Reference in New Issue
Block a user