Search in sources :

Example 6 with ResourceNotFoundException

use of ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException 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)

Example 7 with ResourceNotFoundException

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

the class LocationResourceProvider method getLocationById.

@Read(version = true)
public Location getLocationById(@IdParam IdType locationId) {
    LocationDetails locationDetails = locationSearch.findLocationById(locationId.getIdPart());
    if (locationDetails == null) {
        throw OperationOutcomeFactory.buildOperationOutcomeException(new ResourceNotFoundException("No location details found for location ID: " + locationId.getIdPart()), SystemCode.REFERENCE_NOT_FOUND, IssueType.INCOMPLETE);
    }
    Location location = locationDetailsToLocation(locationDetails);
    return location;
}
Also used : LocationDetails(uk.gov.hscic.model.location.LocationDetails) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) Location(org.hl7.fhir.dstu3.model.Location) Read(ca.uhn.fhir.rest.annotation.Read)

Aggregations

ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)7 OrganizationDetails (uk.gov.hscic.model.organization.OrganizationDetails)3 Read (ca.uhn.fhir.rest.annotation.Read)2 InvalidRequestException (ca.uhn.fhir.rest.server.exceptions.InvalidRequestException)2 UnprocessableEntityException (ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException)2 Location (org.hl7.fhir.dstu3.model.Location)2 LocationDetails (uk.gov.hscic.model.location.LocationDetails)2 PatientDetails (uk.gov.hscic.model.patient.PatientDetails)2 IdDt (ca.uhn.fhir.model.primitive.IdDt)1 DataFormatException (ca.uhn.fhir.parser.DataFormatException)1 ResourceBinding (ca.uhn.fhir.rest.server.ResourceBinding)1 BaseServerResponseException (ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)1 ForbiddenOperationException (ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException)1 MethodNotAllowedException (ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException)1 ResourceVersionConflictException (ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException)1 IParameter (ca.uhn.fhir.rest.server.method.IParameter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 BundleEntryComponent (org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent)1 CodeableConcept (org.hl7.fhir.dstu3.model.CodeableConcept)1