jcomellas / ex_hl7

HL7 Parser for Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing Segment parser crash

pozhega opened this issue · comments

While trying to parse ADT-A04 example message, parser breaks on this several headers:

  • NK1
  • AL1
  • ROL
  • GT1
  • IN2

Parser break because with this error message:

** (UndefinedFunctionError) function HL7.Segment.NK1.new/0 is undefined (module HL7.Segment.NK1 is not available)

Is this desired behaviour (the crash) or it should be handled in some way, and partial result returned (with some warning messages). Also this behaviour could be encapsulated in separate function which would return partial result instead of crash. I'm planning to fork this repo and handle this exception in order to have parses running because this fields are not important to me, and I'm wondering is this something that you would like to see in the PR?

I took deeper dive into the code and resolved the issue without forking.
I've added custom segment_creator which handles unknown segments and creates unknown segment.

defmodule Project.HL7.UnknownSegment do
  import HL7.Segment.Def

  segment "UNKNOWN_SEGMENT" do
  end
end

defp tolerant_segment_creator(segment_id) do
    module = HL7.Segment.module(segment_id)

    try do
      {module, module.new()}
    rescue
      UndefinedFunctionError ->
        {UnknownSegment, UnknownSegment.new()}
    end
 end