Search in sources :

Example 1 with IssueDetails

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();
}
Also used : BaseExceptionResponse(gov.ca.cwds.rest.exception.BaseExceptionResponse) ConstraintViolation(javax.validation.ConstraintViolation) IssueDetails(gov.ca.cwds.rest.exception.IssueDetails) HashSet(java.util.HashSet)

Example 2 with IssueDetails

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;
}
Also used : IssueDetails(gov.ca.cwds.rest.exception.IssueDetails) IOException(java.io.IOException)

Example 3 with IssueDetails

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();
}
Also used : BaseExceptionResponse(gov.ca.cwds.rest.exception.BaseExceptionResponse) IssueDetails(gov.ca.cwds.rest.exception.IssueDetails) HashSet(java.util.HashSet)

Example 4 with IssueDetails

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();
}
Also used : Response(javax.ws.rs.core.Response) BaseExceptionResponse(gov.ca.cwds.rest.exception.BaseExceptionResponse) BaseExceptionResponse(gov.ca.cwds.rest.exception.BaseExceptionResponse) IssueDetails(gov.ca.cwds.rest.exception.IssueDetails) ErrorMessage(io.dropwizard.jersey.errors.ErrorMessage) HashSet(java.util.HashSet)

Example 5 with IssueDetails

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();
}
Also used : BaseExceptionResponse(gov.ca.cwds.rest.exception.BaseExceptionResponse) IssueDetails(gov.ca.cwds.rest.exception.IssueDetails) HashSet(java.util.HashSet)

Aggregations

IssueDetails (gov.ca.cwds.rest.exception.IssueDetails)30 BaseExceptionResponse (gov.ca.cwds.rest.exception.BaseExceptionResponse)22 Test (org.junit.Test)19 RFA1aFormDTO (gov.ca.cwds.cals.service.dto.rfa.RFA1aFormDTO)15 ApplicantDTO (gov.ca.cwds.cals.service.dto.rfa.ApplicantDTO)14 HashMap (java.util.HashMap)12 Set (java.util.Set)11 Map (java.util.Map)10 ClientErrorException (javax.ws.rs.ClientErrorException)10 Response (javax.ws.rs.core.Response)10 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)4 REQUIRED_APPLICANT_DATE_OF_BIRTH (gov.ca.cwds.cals.Constants.Validation.Business.Code.REQUIRED_APPLICANT_DATE_OF_BIRTH)4 REQUIRED_APPLICANT_DRIVER_LICENSE_NUMBER (gov.ca.cwds.cals.Constants.Validation.Business.Code.REQUIRED_APPLICANT_DRIVER_LICENSE_NUMBER)4 REQUIRED_APPLICANT_DRIVER_LICENSE_STATE (gov.ca.cwds.cals.Constants.Validation.Business.Code.REQUIRED_APPLICANT_DRIVER_LICENSE_STATE)4 REQUIRED_APPLICANT_OTHER_NAMES_FIRST_NAME (gov.ca.cwds.cals.Constants.Validation.Business.Code.REQUIRED_APPLICANT_OTHER_NAMES_FIRST_NAME)4 REQUIRED_APPLICANT_OTHER_NAMES_LAST_NAME (gov.ca.cwds.cals.Constants.Validation.Business.Code.REQUIRED_APPLICANT_OTHER_NAMES_LAST_NAME)4 REQUIRED_APPLICANT_PHONE_NUMBER (gov.ca.cwds.cals.Constants.Validation.Business.Code.REQUIRED_APPLICANT_PHONE_NUMBER)4 REQUIRED_BODY_OF_WATER_EXIST_FIELD (gov.ca.cwds.cals.Constants.Validation.Business.Code.REQUIRED_BODY_OF_WATER_EXIST_FIELD)4 REQUIRED_HOME_LANGUAGES (gov.ca.cwds.cals.Constants.Validation.Business.Code.REQUIRED_HOME_LANGUAGES)4 REQUIRED_MINOR_CHILD_ADOPTED (gov.ca.cwds.cals.Constants.Validation.Business.Code.REQUIRED_MINOR_CHILD_ADOPTED)4