fix user api validation
This commit is contained in:
@@ -167,7 +167,7 @@
|
|||||||
],
|
],
|
||||||
"body": {
|
"body": {
|
||||||
"mode": "raw",
|
"mode": "raw",
|
||||||
"raw": "{\n \"username\": \"newcustomer{{$timestamp}}\",\n \"password\": \"password123\",\n \"email\": \"new{{$timestamp}}@example.com\",\n \"fullName\": \"New Customer\"\n}"
|
"raw": "{\n \"username\": \"newcustomer{{$timestamp}}\",\n \"password\": \"password123\",\n \"email\": \"new{{$timestamp}}@example.com\",\n \"phone\": \"+1-555-01{{$randomInt}}\",\n \"fullName\": \"New Customer\"\n}"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"event": [
|
"event": [
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ public class EmployeeRequest {
|
|||||||
@Email(message = "Invalid email format")
|
@Email(message = "Invalid email format")
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
@NotBlank(message = "Phone is required")
|
||||||
@Size(max = 20, message = "Phone must not exceed 20 characters")
|
@Size(max = 20, message = "Phone must not exceed 20 characters")
|
||||||
private String phone;
|
private String phone;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package com.petshop.backend.exception;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
@@ -82,6 +84,27 @@ public class GlobalExceptionHandler {
|
|||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||||
|
public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) {
|
||||||
|
ErrorResponse error = new ErrorResponse(
|
||||||
|
HttpStatus.BAD_REQUEST.value(),
|
||||||
|
"Invalid value for parameter: " + ex.getName(),
|
||||||
|
LocalDateTime.now()
|
||||||
|
);
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(ResponseStatusException.class)
|
||||||
|
public ResponseEntity<ErrorResponse> handleResponseStatusException(ResponseStatusException ex) {
|
||||||
|
String message = ex.getReason() != null ? ex.getReason() : ex.getMessage();
|
||||||
|
ErrorResponse error = new ErrorResponse(
|
||||||
|
ex.getStatusCode().value(),
|
||||||
|
message,
|
||||||
|
LocalDateTime.now()
|
||||||
|
);
|
||||||
|
return ResponseEntity.status(ex.getStatusCode()).body(error);
|
||||||
|
}
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
|
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
|
||||||
ErrorResponse error = new ErrorResponse(
|
ErrorResponse error = new ErrorResponse(
|
||||||
|
|||||||
Reference in New Issue
Block a user