Skip to main content

exceptions

EHR-related exceptions.

Module​

Functions​

is_forbidden​

def is_forbidden(error: BaseException) ‑> bool:

Whether error, or an exception it was raised from, is a refusal.

A backend's readers wrap its refusal in their own errors, so the chain is followed: __cause__ always, and __context__ only when it was not suppressed (raise ... from None).

Arguments

  • error: The exception caught.

Returns True when an EHRForbiddenError is in the chain.

Classes​

DataError​

class DataError(*args, **kwargs):

Exception for unexpected data encountered unable to be processed.

Subclasses​

EHRAuthenticationError​

class EHRAuthenticationError(*args, **kwargs):

The EHR rejected a request as unauthenticated.

Where the client can refresh its token, this is after a forced token refresh.

Raised for one request. Whether the run carries on is the auth budget's decision, which it reports as EHRConnectionError.

EHRFetchUnavailableError​

class EHRFetchUnavailableError(*args, **kwargs):

A fetch failed this time, and may succeed if asked again later.

The transient half of "no data", beside EHRFetchUnsupportedError's structural half: a search the server stopped answering, or a server error. Not a GetPatientInfoError, so every handler that catches one treats these as it treats any other failure.

EHRFetchUnsupportedError​

class EHRFetchUnsupportedError(*args, **kwargs):

This backend cannot serve this fetch at all.

The structural half of "no data": not "the fetch failed this time" but "this backend has no way to answer it", which is identical on every run for every patient. Consumers deciding whether a blank is worth retrying — or whether the write guard should count it (steps/ehr_query/functions._every_supported_group_failed) — branch on this type rather than on a capability lookup performed after the fact.

Raised by the backend that knows, at the point it knows: the FHIR R4 client on an unsupported-type OperationOutcome (and the querier when its CapabilityStatement already rules the type out), and the NextGen querier from the endpoints it does not implement. Whether the gap is permanent or merely unimplemented is not encoded here; both are fixed properties of the backend, which is all any consumer needs.

Subclasses GetPatientInfoError so that every handler written for a failed fetch keeps working — the type adds information rather than demanding new handling. A handler that means "could not retrieve this time" has to catch this first and re-raise; the danger is silent, since swallowing it reports a structural gap as a transient one and keeps the guard counting the group for as long as the gap persists.

EHRForbiddenError​

class EHRForbiddenError(*args, **kwargs):

The EHR refused a request it understood (HTTP 403).

The credentials lack a scope or permission for what was asked. A backend's own refusal subclasses it; is_forbidden finds one anywhere in an error's chain.

EHRRequestError​

class EHRRequestError(*args, **kwargs):

A request to the EHR failed, and the backend reported why.

The server answered with an error status or an error it reported itself, or with an answer the backend could not read (on FHIR R4, one that is not a Bundle or not JSON, or a resource that is not valid). A backend's own request errors subclass it, so a caller catches a failed request without naming the backend: FHIR R4's FHIRR4APIError does, and NextGen raises none. Some subclasses are also lookup outcomes (no match, several, no ID) or GetPatientInfoErrors; a handler that treats those differently catches them first. Which failures arrive as one is set out in service/protocols.py.

Subclasses​

EHRTokenUnavailableError​

class EHRTokenUnavailableError(*args, **kwargs):

No token could be obtained to send, so the request was never made.

The client's token source failed: for a desktop pod, the app or the pod did not supply an EHR token. The EHR did not reject anything, which is what sets this apart from its parent in logs and telemetry. For the run it is the same failure. Every handler that lets an authentication error through also lets this through, and a patient whose lookup raises it is recorded as AUTH_REJECTED, not as a generic fetch failure.

The auth budget counts it like a rejection. The token source has already retried before giving up, so a run of these means it is down. Asking it again for every remaining patient would fail each one within milliseconds and achieve nothing. The pass ends instead, and the patients it did not reach are retried by a later run.

EHRUnavailableStandDown​

class EHRUnavailableStandDown(message: str, *, run_outcome: str, records_stored: int):

A pass over EHR patients could not reach the EHR, so the run stands down.

Raised by ehr_criteria_query in patient_id_cache mode, after it has stored whatever it fetched. It is raised when the EHR was unreachable before the pass began, when the pass was cut short (EHRConnectionError and the token errors), and when every patient it asked went unanswered.

This does not mark the step or its wave as failed. The background executor stops the run here, the way it does for a busy step resource. The wave is neither banked nor marked failed, so the wave ledger keeps its patients outstanding. A later run (a wave continuation, or the next periodic rerun) asks for them again. Banking the wave would leave the patients it never reached looking done until the next rerun horizon. Failing it would count an outage against them, and background_dag_wave_retry_limit would drop them from the plan.

Arguments

  • message: What happened, for the log.
  • run_outcome: The step's EHRRunOutcome value, for telemetry.
  • records_stored: Rows the step stored before standing down.

Ancestors​

GetPatientInfoError​

class GetPatientInfoError(*args, **kwargs):

Could not retrieve patient info.

IdentityMismatchError​

class IdentityMismatchError(*args, **kwargs):

A fallback search found a patient whose identity does not match the scan.

Raised by the first-given-name and MRN fallbacks, on FHIR R4 and NextGen alike, when the EHR record differs from the scan in date of birth, family name or given names, or lacks one of them. Treated as no match: a first-given-name search matches other people's names (by prefix on FHIR R4, by substring on NextGen), and an MRN may belong to a different patient or to a different identifier system.

NoMatchingPatientError​

class NoMatchingPatientError(*args, **kwargs):

Exception raised when no patient matching filters is found.

NoPatientIDError​

class NoPatientIDError(*args, **kwargs):

Exception raised when patient ID could not be extracted.

NonSpecificPatientError​

class NonSpecificPatientError(*args, **kwargs):

Exception raised when patient could not be narrowed to a single person.

QuotaExceeded​

class QuotaExceeded(*args, **kwargs):

Exception raised when we have exceeded our API call quota limit.