use of gov.ca.cwds.rest.exception.IssueDetails in project api-core by ca-cwds.
the class CustomJerseyViolationExceptionMapper method toResponse.
@Override
public Response toResponse(final JerseyViolationException exception) {
final Set<IssueDetails> validationDetailsList = new HashSet<>();
final Set<ConstraintViolation<?>> transgressions = exception.getConstraintViolations();
for (ConstraintViolation<?> v : transgressions) {
String message = CustomConstraintMessage.getMessage(v, exception.getInvocable()).trim();
IssueDetails details = unmarshallData(message);
if (details != null) {
details.setType(IssueType.BUSINESS_VALIDATION);
} else {
details = IssueDetailsCreator.create(v, loggingContext.getUniqueId());
}
details.setIncidentId(loggingContext.getUniqueId());
validationDetailsList.add(details);
}
BaseExceptionResponse constraintViolationsResponse = new BaseExceptionResponse();
constraintViolationsResponse.setIssueDetails(validationDetailsList);
int status = CustomConstraintMessage.determineStatus(exception.getConstraintViolations(), exception.getInvocable());
return Response.status(status).type(MediaType.APPLICATION_JSON_TYPE).entity(constraintViolationsResponse).build();
}
use of gov.ca.cwds.rest.exception.IssueDetails in project api-core by ca-cwds.
the class CustomJerseyViolationExceptionMapper method unmarshallData.
/**
* hibernate validation framework updates user message with prefix that should be removed.
*
* @param data constraint violation message
* @return validation details in case of business validation message or null in case of constraint
* validation message
* @see {@link CustomConstraintMessage#calculateMessage(ConstraintViolation, Invocable)}
*/
private IssueDetails unmarshallData(String data) {
String marshalledDetails = StringUtils.removeStart(data, "The request body");
IssueDetails details = null;
try {
details = (IssueDetails) JsonUtils.from(marshalledDetails, IssueDetails.class);
} catch (IOException e) {
LOG.debug("Cannot unmarshall validation details", e);
}
return details;
}
use of gov.ca.cwds.rest.exception.IssueDetails in project api-core by ca-cwds.
the class ExpectedExceptionMapperImpl method toResponse.
@Override
public Response toResponse(ExpectedException exception) {
IssueDetails details = new IssueDetails();
details.setType(IssueType.EXPECTED_EXCEPTION);
details.setIncidentId(loggingContext.getUniqueId());
details.setTechnicalMessage(exception.getMessage());
details.setUserMessage(BASE_MESSAGE);
Set<IssueDetails> detailsList = new HashSet<>();
detailsList.add(details);
BaseExceptionResponse expectedExceptionResponse = new BaseExceptionResponse();
expectedExceptionResponse.setIssueDetails(detailsList);
return Response.status(exception.getResponseStatus()).entity(expectedExceptionResponse).type(MediaType.APPLICATION_JSON).build();
}
use of gov.ca.cwds.rest.exception.IssueDetails in project api-core by ca-cwds.
the class CustomJsonProcessingExceptionMapper method toResponse.
@Override
public Response toResponse(JsonProcessingException exception) {
Response response = super.toResponse(exception);
if (!(response.getEntity() instanceof ErrorMessage)) {
throw new IllegalStateException("ErrorMessage class is expected here");
}
ErrorMessage errorMessage = (ErrorMessage) response.getEntity();
IssueDetails details = new IssueDetails();
details.setType(IssueType.JSON_PROCESSING_EXCEPTION);
details.setIncidentId(loggingContext.getUniqueId());
details.setUserMessage(BASE_MESSAGE);
details.setTechnicalMessage(StringUtils.join(new Object[] { errorMessage.getMessage(), errorMessage.getDetails() }, ". "));
Set<IssueDetails> detailsList = new HashSet<>();
detailsList.add(details);
BaseExceptionResponse jsonProcessingExceptionResponse = new BaseExceptionResponse();
jsonProcessingExceptionResponse.setIssueDetails(detailsList);
return Response.status(response.getStatus()).type(response.getMediaType()).entity(jsonProcessingExceptionResponse).build();
}
use of gov.ca.cwds.rest.exception.IssueDetails in project api-core by ca-cwds.
the class ExceptionMapperUtils method createGenericResponse.
/**
* Create a generic exception response
*
* @param ex The exception
* @param issueType Issue type
* @param status Response status
* @param incidentId Incident ID
* @return A generic Response created from given exception
*/
public static Response createGenericResponse(Exception ex, IssueType issueType, int status, String incidentId) {
IssueDetails issueDetails = new IssueDetails();
issueDetails.setType(issueType);
issueDetails.setIncidentId(incidentId);
issueDetails.setUserMessage(BASE_MESSAGE);
issueDetails.setTechnicalMessage(ex.getMessage());
if (ex.getCause() != null) {
issueDetails.setTechnicalMessage(ex.getCause().getMessage());
issueDetails.setCauseStackTrace(StringEscapeUtils.escapeJson(ExceptionUtils.getStackTrace(ex.getCause())));
}
issueDetails.setStackTrace(StringEscapeUtils.escapeJson(ExceptionUtils.getStackTrace(ex)));
final Set<IssueDetails> detailsList = new HashSet<>();
detailsList.add(issueDetails);
final BaseExceptionResponse response = new BaseExceptionResponse();
response.setIssueDetails(detailsList);
return Response.status(status).entity(response).type(MediaType.APPLICATION_JSON).build();
}
Aggregations