Error Handling

hl7types.utils.error.err_from_pydantic_error(error, version)

Convert a single Pydantic error dict into a version-appropriate ERR segment.

Parameters:
  • error (dict) – A single error entry from ValidationError.errors().

  • version (str) – The HL7 version string, e.g. "2.5.1" or "2.8.2". Determines the structure of the returned ERR segment.

Returns:

An ERR segment instance for the given version.

Return type:

HL7Model

Raises:

ValueError – If no ERR segment class is found for the given version.

hl7types.utils.error.errs_from_exception(exc, version)

Convert an exception into a list of version-appropriate ERR segments.

Only pydantic.ValidationError is handled. Any other exception type returns an empty list.

Parameters:
  • exc (Exception) – The exception to convert. Typically a pydantic.ValidationError raised by decode_er7 or direct model construction.

  • version (str) – The HL7 version string, e.g. "2.5.1" or "2.8.2". Determines the structure of each returned ERR segment.

Returns:

One ERR segment per violated field, or an empty list if exc is not a ValidationError.

Return type:

list[HL7Model]