-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/add input validation #6
Open
arunabahmukherjee-zz
wants to merge
5
commits into
dev
Choose a base branch
from
feature/add_input_validation
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1f1d4c4
Added input validation to Business Controller
arunabah-mukherjee 125e915
Added validation for Owner Controller
arunabah-mukherjee 8dd0fca
Added validation for ShoppingCartController
arunabah-mukherjee 59d9a7d
Fixed test errors
arunabah-mukherjee 948eb24
Added validation to input for StriptePaymentController
arunabah-mukherjee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,26 @@ | |
import com.google.maps.errors.ApiError; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.core.convert.ConversionFailedException; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.validation.ObjectError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.ConstraintViolationException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@ControllerAdvice | ||
public class ApiExceptionControllerAdvice extends ResponseEntityExceptionHandler { | ||
|
@@ -28,7 +39,10 @@ public class ApiExceptionControllerAdvice extends ResponseEntityExceptionHandler | |
* @return a {@code ResponseEntity} instance | ||
*/ | ||
@Override | ||
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { | ||
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, | ||
HttpHeaders headers, | ||
HttpStatus status, | ||
WebRequest request) { | ||
ApiError error = new ApiError(); | ||
error.code = 400; | ||
error.status = HttpStatus.BAD_REQUEST.toString(); | ||
|
@@ -37,22 +51,22 @@ protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotRead | |
} | ||
|
||
@ExceptionHandler(InternalException.class) | ||
protected ResponseEntity<Object> handleInternalExceptions(InternalException exp) { | ||
protected ResponseEntity<Object> handleInternalException(InternalException exp) { | ||
ApiError error = new ApiError(); | ||
error.code = 500; | ||
error.status = HttpStatus.INTERNAL_SERVER_ERROR.toString(); | ||
error.message = "Uh oh! something went wrong processing your request. Please try again or advise the administrator."; | ||
error.message = exp.getMessage(); | ||
|
||
|
||
return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
@ExceptionHandler(BusinessAlreadyClaimedException.class) | ||
protected ResponseEntity<Object> handleBusinessAlreadyClaimedExceptions(BusinessAlreadyClaimedException exp) { | ||
protected ResponseEntity<Object> handleBusinessAlreadyClaimedException(BusinessAlreadyClaimedException exp) { | ||
ApiError error = new ApiError(); | ||
error.code = 409; | ||
error.status = HttpStatus.BAD_REQUEST.toString(); | ||
error.message = "Business has already been claimed. Please contact administrator for assistance."; | ||
error.status = HttpStatus.CONFLICT.toString(); | ||
error.message = exp.getMessage(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment lets not show user internal message. |
||
|
||
return new ResponseEntity<>(error, HttpStatus.CONFLICT); | ||
} | ||
|
@@ -68,7 +82,7 @@ protected ResponseEntity<Object> handleBusinessClaimExceptions(BusinessClaimExce | |
} | ||
|
||
@ExceptionHandler(BusinessNotFoundException.class) | ||
protected ResponseEntity<Object> handleBusinessNotFoundExceptionExceptions(BusinessNotFoundException exp) { | ||
protected ResponseEntity<Object> handleBusinessNotFoundException(BusinessNotFoundException exp) { | ||
ApiError error = new ApiError(); | ||
error.code = 404; | ||
error.status = HttpStatus.NOT_FOUND.toString(); | ||
|
@@ -78,7 +92,7 @@ protected ResponseEntity<Object> handleBusinessNotFoundExceptionExceptions(Busin | |
} | ||
|
||
@ExceptionHandler(PaymentServiceException.class) | ||
protected ResponseEntity<Object> handlePaymentServiceExceptions(PaymentServiceException exp) { | ||
protected ResponseEntity<Object> handlePaymentServiceException(PaymentServiceException exp) { | ||
ApiError error = new ApiError(); | ||
error.code = 500; | ||
error.status = HttpStatus.INTERNAL_SERVER_ERROR.toString(); | ||
|
@@ -88,7 +102,7 @@ protected ResponseEntity<Object> handlePaymentServiceExceptions(PaymentServiceEx | |
} | ||
|
||
@ExceptionHandler(PaymentAccountNotSetupException.class) | ||
protected ResponseEntity<Object> handlePaymentAccountNotSetupExceptionExceptions(PaymentAccountNotSetupException exp) { | ||
protected ResponseEntity<Object> handlePaymentAccountNotSetupException(PaymentAccountNotSetupException exp) { | ||
ApiError error = new ApiError(); | ||
error.code = 400; | ||
error.status = HttpStatus.BAD_REQUEST.toString(); | ||
|
@@ -97,4 +111,50 @@ protected ResponseEntity<Object> handlePaymentAccountNotSetupExceptionExceptions | |
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
} | ||
/** | ||
* Customize the response for MethodArgumentNotValidException. | ||
* <p>This method delegates to {@link #handleExceptionInternal}. | ||
* | ||
* @param exp the exception | ||
* @param headers the headers to be written to the response | ||
* @param status the selected response status | ||
* @param request the current request | ||
* @return a {@code ResponseEntity} instance | ||
*/ | ||
@Override | ||
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException exp, | ||
HttpHeaders headers, HttpStatus status, WebRequest request) { | ||
Map<String, String> fieldErrorMap = new HashMap<>(); | ||
List<ObjectError> fieldErrors = exp.getBindingResult().getAllErrors(); | ||
fieldErrors.forEach((error) -> { | ||
String errMessage = error.getDefaultMessage(); | ||
String fieldId = ((FieldError) error).getField(); | ||
fieldErrorMap.put(fieldId, errMessage); | ||
}); | ||
|
||
return new ResponseEntity<>(fieldErrorMap, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(ConstraintViolationException.class) | ||
protected ResponseEntity<Object> handleConstraintViolationException(ConstraintViolationException exp) { | ||
Map<String, String> constraintErrors = new HashMap<>(); | ||
Set<ConstraintViolation<?>> constraintViolations = exp.getConstraintViolations(); | ||
constraintViolations.forEach((constraintViolation) -> { | ||
String errMessage = constraintViolation.getMessage(); | ||
String constraintPath = constraintViolation.getPropertyPath().toString(); | ||
constraintErrors.put(constraintPath, errMessage); | ||
}); | ||
|
||
return new ResponseEntity<>(constraintErrors, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(ConversionFailedException.class) | ||
protected ResponseEntity<Object> handleConversionFailedException(ConversionFailedException exp) { | ||
ApiError error = new ApiError(); | ||
error.code = 400; | ||
error.status = HttpStatus.BAD_REQUEST.toString(); | ||
error.message = "Invalid value provided."; | ||
|
||
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 9 additions & 1 deletion
10
src/main/java/com/coronacarecard/model/BusinessRegistrationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
package com.coronacarecard.model; | ||
|
||
import javax.validation.constraints.Email; | ||
import javax.validation.constraints.NotEmpty; | ||
import javax.validation.constraints.NotNull; | ||
|
||
@lombok.Builder | ||
@lombok.NoArgsConstructor | ||
@lombok.AllArgsConstructor | ||
@lombok.Getter | ||
public class BusinessRegistrationRequest { | ||
@NotNull | ||
@NotEmpty | ||
private String businessId; | ||
@NotNull | ||
@NotEmpty | ||
private String email; | ||
private String phone; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we validate phone number? |
||
private String description; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think we should show the customer intneral message. The previous approach was better. Also i added some code to log this exception since we are overriding it