Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
/*
* ApiError.java
* Copyright (c) 2017 Veterans Affairs.
*/
package gov.va.oneconsult.seoc.api.exceptions;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
/**
* @author AbleVets
*/
@JsonSerialize
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.CUSTOM, property = "error", visible = true)
@JsonTypeIdResolver(LowerCaseClassNameResolver.class)
class ApiError
{
public static final Logger logger = LoggerFactory.getLogger(ApiError.class);
/**
* HttpStatus of the error
*/
private HttpStatus status;
/**
* Time stamp of error generated
*/
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
private LocalDateTime timestamp;
/**
* Detailed error message
*/
private String message;
/**
* Debug error message
*/
private String debugMessage;
/**
* List of sub-errors when applicable
*/
private List<ApiSubError> subErrors;
/**
* Default Constructor for ApiError with current time stamp
*/
private ApiError()
{
timestamp = LocalDateTime.now();
}
/**
* Constructor with HttpStatus code
*
* @param status
*/
ApiError(HttpStatus status)
{
this();
this.status = status;
}
/**
* Constructor with HttpStatus code, and exception thrown
*
* @param status
* @param ex
*/
ApiError(HttpStatus status, Throwable ex)
{
this();
this.status = status;
this.message = "Unexpected error";
//this.debugMessage = ex.getLocalizedMessage();
}
/**
* Constructor with HttpStatus, Message from error and Exception
*
* @param status
* @param message
* @param ex
*/
ApiError(HttpStatus status, String message, Throwable ex)
{
this();
this.status = status;
this.message = message;
//this.debugMessage = ex.getLocalizedMessage();
}
/**
* Utility method to add Sub errors to the main ApiError
*
* @param subError {@link ApiSubError}
*/
private void addSubError(ApiSubError subError)
{
if (subErrors == null)
{
subErrors = new ArrayList<>();
}
subErrors.add(subError);
}
/**
* @param object
* @param field
* @param rejectedValue
* @param message
*/
private void addValidationError(String object, String field, Object rejectedValue,
String message)
{
addSubError(new ApiValidationError(object, field, rejectedValue, message));
}
/**
* @param object
* @param message
*/
private void addValidationError(String object, String message)
{
addSubError(new ApiValidationError(object, message));
}
/**
* @param fieldError
*/
private void addValidationError(FieldError fieldError)
{
this.addValidationError(fieldError.getObjectName(), fieldError.getField(),
fieldError.getRejectedValue(), fieldError.getDefaultMessage());
}
/**
* @param fieldErrors
*/
void addValidationErrors(List<FieldError> fieldErrors)
{
fieldErrors.forEach(this::addValidationError);
}
/**
* @param objectError
*/
private void addValidationError(ObjectError objectError)
{
this.addValidationError(objectError.getObjectName(), objectError.getDefaultMessage());
}
/**
* @param globalErrors
*/
void addValidationError(List<ObjectError> globalErrors)
{
globalErrors.forEach(this::addValidationError);
}
/**
* Utility method for adding error of ConstraintViolation. Usually when
* a @Validated validation fails.
*
* @param cv ConstraintViolation
*/
private void addValidationError(ConstraintViolation<?> cv)
{
this.addValidationError(cv.getRootBeanClass().getSimpleName(),
((PathImpl) cv.getPropertyPath()).getLeafNode().asString(), cv.getInvalidValue(),
cv.getMessage());
}
/**
* @param constraintViolations
*/
void addValidationErrors(Set<ConstraintViolation<?>> constraintViolations)
{
constraintViolations.forEach(this::addValidationError);
}
/**
* @param ConstraintViolation
*/
private void addSeocValidationError(ConstraintViolation<?> cv)
{
this.addValidationError(cv.getRootBeanClass().getSimpleName(),
((PathImpl) cv.getPropertyPath()).asString(), cv.getExecutableReturnValue(),
cv.getMessage());
}
/**
* @param seocConstraintViolations
*/
void addSeocValidationErrors(Set<ConstraintViolation<?>> seocConstraintViolations)
{
seocConstraintViolations.forEach(this::addSeocValidationError);
}
public HttpStatus getStatus()
{
return status;
}
public void setStatus(HttpStatus status)
{
this.status = status;
}
public LocalDateTime getTimestamp()
{
return timestamp;
}
public void setTimestamp(LocalDateTime timestamp)
{
this.timestamp = timestamp;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public String getDebugMessage()
{
return debugMessage;
}
public void setDebugMessage(String debugMessage)
{
this.debugMessage = debugMessage;
}
public List<ApiSubError> getSubErrors()
{
if (subErrors != null && !subErrors.isEmpty())
{
subErrors.forEach(se ->
{
logger.info("Inside SubError : " + se.toString());
});
}
return subErrors;
}
public void setSubErrors(List<ApiSubError> subErrors)
{
this.subErrors = subErrors;
}
/**
* ApiSubError abstract class
*
* @author AbleVets
*/
abstract class ApiSubError
{
}
/**
* ApiValidationError Implementation class of {@link ApiSubError}
*
* @author AbleVets
*/
@JsonSerialize
@SuppressWarnings("unused")
class ApiValidationError extends ApiSubError
{
private String object;
private String field;
private Object rejectedValue;
private String message;
ApiValidationError(String object, String message)
{
this.object = object;
this.message = message;
}
public ApiValidationError(String object2, String field2, Object rejectedValue2,
String message2)
{
this.object = object2;
this.field = field2;
this.rejectedValue = rejectedValue2;
this.message = message2;
}
public String getObject()
{
return object;
}
public String getField()
{
return field;
}
public Object getRejectedValue()
{
return rejectedValue;
}
public String getMessage()
{
return message;
}
@Override
public String toString()
{
return "ApiValidationError [object=" + object + ", field=" + field + ", rejectedValue="
+ rejectedValue + ", message=" + message + "]";
}
}
}