Commit 2f5528af authored by Giridhari Sahoo's avatar Giridhari Sahoo

implement validation logic

parent 38f70dba
package com.nisum.Employeeinfo.advice;
import com.nisum.Employeeinfo.errorcode.ApiError;
import com.nisum.Employeeinfo.errorcode.ApiErrorCode;
import com.nisum.Employeeinfo.exception.EmployeeNotFoundException;
import com.nisum.Employeeinfo.exception.ExceptionResponse;
import com.nisum.Employeeinfo.exception.InvalidEmployeeDataException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class EmployeeServiceException {
@ExceptionHandler(EmployeeNotFoundException.class)
public ResponseEntity<Object> handleEmployeeNotFoundException(EmployeeNotFoundException ex, ServerHttpRequest serverHttpRequest){
ApiError apiError= new ApiError(
ex.getErrorCode(),
ex.getMessage(),
serverHttpRequest.getURI().getPath()
);
ExceptionResponse response=new ExceptionResponse(apiError);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(InvalidEmployeeDataException.class)
public ResponseEntity<Object> handleInvalidEmployeeData(InvalidEmployeeDataException ex, ServerHttpRequest serverHttpRequest){
ApiError apiError= new ApiError(
ex.getErrorCode(),
ex.getMessage(),
serverHttpRequest.getURI().getPath()
);
ExceptionResponse response=new ExceptionResponse(apiError);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleGeneralException(Exception ex, ServerHttpRequest serverHttpRequest){
ApiError apiError= new ApiError(
ApiErrorCode.GENERAL_ERROR.getString(),
ex.getMessage(),
serverHttpRequest.getURI().getPath()
);
ExceptionResponse response=new ExceptionResponse(apiError);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
package com.nisum.Employeeinfo.errorcode;
import com.fasterxml.jackson.annotation.JsonProperty;
public class ApiError {
@JsonProperty("errorCode")
private String errorCode;
@JsonProperty("errorMessage")
private String errorMessage;
@JsonProperty("uri")
private String uri;
public ApiError(String errorCode, String errorMessage, String uri) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
this.uri = uri;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
}
package com.nisum.Employeeinfo.errorcode;
import com.fasterxml.jackson.annotation.JsonValue;
public enum ApiErrorCode {
EMPLOYEE_NOT_FOUND("EMPLOYEE_404"),
INVALID_EMPLOYEE_DATA("EMPLOYEE_400"),
EMPLOYEE_CREATION_FAILED("EMPLOYEE_500"),
EMPLOYEE_UPDATE_FAILED("EMPLOYEE_500"),
EMPLOYEE_DELETION_FAILED("EMPLOYEE_500"),
GENERAL_ERROR("GENERAL_500");
private final String errorCode;
// Constructor
private ApiErrorCode(String errorCode) {
this.errorCode = errorCode;
}
// Method to return the error code as a string
@JsonValue
public String getString() {
return this.errorCode;
}
}
package com.nisum.Employeeinfo.exception;
import com.nisum.Employeeinfo.errorcode.ApiErrorCode;
public class EmployeeNotFoundException extends RuntimeException {
private static final long serialVersionUID = -4918194282229127629L;
private final String errorCode;
// Constructor that accepts ApiErrorCode and message
public EmployeeNotFoundException(ApiErrorCode apiErrorCode, String message) {
super(message); // Pass message to RuntimeException constructor
this.errorCode = apiErrorCode.getString(); // Store the error code from ApiErrorCode
}
// Getter for errorCode
public String getErrorCode() {
return errorCode;
}
}
package com.nisum.Employeeinfo.exception;
import com.nisum.Employeeinfo.errorcode.ApiError;
public class ExceptionResponse {
private ApiError apiError;
public ExceptionResponse(ApiError error) {
apiError=new ApiError(
error.getErrorCode(),
error.getErrorMessage(),
error.getUri()
);
}
public ApiError getApiError() {
return apiError;
}
public void setApiError(ApiError apiError) {
this.apiError = apiError;
}
}
package com.nisum.Employeeinfo.exception;
import com.nisum.Employeeinfo.errorcode.ApiErrorCode;
public class InvalidEmployeeDataException extends RuntimeException{
private final String errorCode;
public InvalidEmployeeDataException(ApiErrorCode errorCode, String message){
super(message);
this.errorCode=errorCode.getString();
}
public String getErrorCode() {
return errorCode;
}
}
package com.nisum.Employeeinfo.validator;
public class CommonServiceValidator {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment