Search in sources :

Example 1 with BaseServerResponseException

use of ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException in project gpconnect-demonstrator by nhsconnect.

the class FhirRequestGenericIntercepter method preProcessOutgoingException.

/**
 * Listens for any exceptions thrown. In the case of invalid parameters, we
 * need to catch this and throw it as a UnprocessableEntityException.
 *
 * @param theRequestDetails
 * @param theException
 * @param theServletRequest
 * @return UnprocessableEntityException if a InvalidRequestException was
 * thrown.
 * @throws javax.servlet.ServletException
 */
@Override
public BaseServerResponseException preProcessOutgoingException(RequestDetails theRequestDetails, Throwable theException, HttpServletRequest theServletRequest) throws ServletException {
    LOG.info("Response Exception");
    LOG.info(theException.getMessage());
    LOG.info("stackTrace: ", theException);
    // how else to pick up on just the relevant exceptions!
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("Invalid attribute value")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("Unknown resource in URI")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new ResourceNotFoundException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("Can not have multiple date range parameters for the same param ")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
    if (theException instanceof DataFormatException) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
    }
    if (theException instanceof MethodNotAllowedException && theException.getMessage().contains("request must use HTTP GET")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().startsWith("Failed to parse request body as JSON resource. Error was: ")) {
        // #250 422 INVALID_RESOURCE not 400 BAD_REQUEST
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_RESOURCE, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().startsWith("Invalid request: ")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("non-repeatable parameter")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("header blank")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("InvalidResourceType")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.INVALID_RESOURCE, IssueType.INVALID);
    }
    if (theException instanceof InvalidRequestException && theException.getMessage().contains("Can not create resource with ID")) {
        return OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    if (theException instanceof ResourceNotFoundException && theException.getMessage().contains("Unknown resource type")) {
        return OperationOutcomeFactory.buildOperationOutcomeException((ResourceNotFoundException) theException, SystemCode.BAD_REQUEST, IssueType.INVALID);
    }
    // }
    if (theException instanceof ResourceVersionConflictException && theException.getMessage().contains("Slot is already in use.")) {
        ResourceVersionConflictException exception = (ResourceVersionConflictException) theException;
        return OperationOutcomeFactory.buildOperationOutcomeException(exception, SystemCode.DUPLICATE_REJECTED, IssueType.CONFLICT);
    }
    if (theException instanceof ResourceVersionConflictException) {
        ResourceVersionConflictException exception = (ResourceVersionConflictException) theException;
        return OperationOutcomeFactory.buildOperationOutcomeException(exception, SystemCode.FHIR_CONSTRAINT_VIOLATION, IssueType.CONFLICT);
    }
    if (theException instanceof BaseServerResponseException) {
        BaseServerResponseException baseServerResponseException = (BaseServerResponseException) theException;
        // If the OperationalOutcome is already set, just return it.
        return null == baseServerResponseException.getOperationOutcome() ? OperationOutcomeFactory.buildOperationOutcomeException(baseServerResponseException, SystemCode.BAD_REQUEST, IssueType.INVALID) : baseServerResponseException;
    }
    // Default catch all.
    return OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(theException.getMessage()), SystemCode.BAD_REQUEST, IssueType.INVALID);
}
Also used : UnprocessableEntityException(ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) MethodNotAllowedException(ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException) InvalidRequestException(ca.uhn.fhir.rest.server.exceptions.InvalidRequestException) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) ResourceVersionConflictException(ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException) BaseServerResponseException(ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)

Aggregations

DataFormatException (ca.uhn.fhir.parser.DataFormatException)1 BaseServerResponseException (ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)1 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)1 MethodNotAllowedException (ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException)1 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)1 ResourceVersionConflictException (ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException)1 UnprocessableEntityException (ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException)1