@@ -56,15 +56,14 @@ data Message a = Response UUID a
5656 | AsyncRequest UUID a
5757 | ResponseExpectedRequest UUID (Maybe Timeout) a
5858 | TimedOutResponse UUID
59− -- | Services
60− | ExceptionResponse String
59+ | ExceptionResponse UUID String
6160 deriving (Generic, Show)
6261 deriving Serialise via WineryVariant (Message a)
6362
6463-- | The response type
6564data HandlerResponse a = NoResponse
6665 | HandlerResponse a
67− | HandlerException a
66+ | HandlerException String
6867 deriving Generic
6968 deriving Serialise via WineryVariant (HandlerResponse a)
7069
@@ -89,6 +88,7 @@ instance Serialise UUID where
8988
9089data ConnectionError = CodecError String -- show of WineryException from exception initiator which cannot otherwise be transmitted over a line due to dependencies on TypeReps
9190 | TimeoutError
91+ | ExceptionError String
9292 deriving (Generic, Show, Eq)
9393 deriving Serialise via WineryVariant ConnectionError
9494
@@ -130,7 +130,6 @@ serve connhandler userMsgHandler hostaddr port mSockLock = do
130130 Left exc -> error (show exc)
131131 Right dec -> dec
132132 handleSock sock = do
133− -- allow the server to send an async welcome message to the new client, if necessary
134133 mResp <- connhandler
135134 case mResp of
136135 Nothing -> pure ()
@@ -157,13 +156,20 @@ serverMessageHandler sock requestMessageHandler msg = do
157156 case msg of
158157 Response{} -> putStrLn "client sent response"
159158 ResponseExpectedRequest requestID mTimeout val -> do
160− resp <- runTimeout mTimeout (requestMessageHandler val)
161− case resp of
162− Just (HandlerResponse responseVal) -> sendMessage (Response requestID responseVal) sock
163− Just NoResponse -> error "attempt to return non-response to expected response message"
164− Just (HandlerException _) -> error "TODO HandlerException"
165− Nothing ->
166− sendMessage (TimedOutResponse @(Message resp) requestID) sock
159+ let normalResponder = do
160+ resp <- runTimeout mTimeout (requestMessageHandler val)
161+ case resp of
162+ Just (HandlerResponse responseVal) -> sendMessage (Response requestID responseVal) sock
163+ Just NoResponse -> error "attempt to return non-response to expected response message"
164+ Just (HandlerException exc) -> sendMessage (ExceptionResponse @(Message resp) requestID exc) sock
165+ Nothing ->
166+ sendMessage (TimedOutResponse @(Message resp) requestID) sock
167+ excHandler :: SomeException -> IO ()
168+ excHandler e = do
169+ --send exception to client
170+ sendMessage (ExceptionResponse @(Message resp) requestID (show e)) sock
171+ throwIO e
172+ catch normalResponder excHandler
167173 AsyncRequest _ val -> do
168174 void $ requestMessageHandler val
169175 --no response necessary