From ce4f3a50b9fe4f847041c127045788c318acbf1a Mon Sep 17 00:00:00 2001
From: Steven Loria <sloria1@gmail.com>
Date: Fri, 24 Jan 2020 16:34:41 -0500
Subject: [PATCH 1/2] Add hook to handle unexpected errors on routing messages

---
 hl7apy/mllp.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/hl7apy/mllp.py b/hl7apy/mllp.py
index ad54da0..996a680 100644
--- a/hl7apy/mllp.py
+++ b/hl7apy/mllp.py
@@ -95,7 +95,8 @@ def handle(self):
         if message is not None:
             try:
                 response = self._route_message(message)
-            except Exception:
+            except Exception as exc:
+                self._handle_routing_error(exc)
                 self.request.close()
             else:
                 # encode the response
@@ -138,6 +139,9 @@ def _create_handler(self, handler_class, msg, args):
     def _create_error_handler(self, handler_class, exc, msg, args):
         return handler_class(exc, msg, *args)
 
+    def _handle_routing_error(self, exc):
+        pass
+
 
 class MLLPServer(ThreadingTCPServer):
     """

From eac8ce12054c321be120933d85a37d6ebee51f2f Mon Sep 17 00:00:00 2001
From: Steven Loria <sloria1@gmail.com>
Date: Tue, 18 Feb 2020 14:55:21 -0500
Subject: [PATCH 2/2] Pass message to _handle_routing_error

---
 hl7apy/mllp.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/hl7apy/mllp.py b/hl7apy/mllp.py
index 996a680..7117423 100644
--- a/hl7apy/mllp.py
+++ b/hl7apy/mllp.py
@@ -96,7 +96,7 @@ def handle(self):
             try:
                 response = self._route_message(message)
             except Exception as exc:
-                self._handle_routing_error(exc)
+                self._handle_routing_error(exc, message)
                 self.request.close()
             else:
                 # encode the response
@@ -139,7 +139,7 @@ def _create_handler(self, handler_class, msg, args):
     def _create_error_handler(self, handler_class, exc, msg, args):
         return handler_class(exc, msg, *args)
 
-    def _handle_routing_error(self, exc):
+    def _handle_routing_error(self, exc, msg):
         pass