8000 Throwing Incorrect Usage Exceptions · postgres-haskell/postgres-wire@321aea4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 321aea4

Browse files
Throwing Incorrect Usage Exceptions
1 parent 57ef66e commit 321aea4

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/Database/PostgreSQL/Driver/Error.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import Database.PostgreSQL.Protocol.Types (ErrorDesc)
1313
-- ProtocolException
1414
-- IncorrectUsage.
1515

16-
data IncorrectUsage = IncorrectUsage
16+
newtype IncorrectUsage = IncorrectUsage ByteString
17+
deriving (Show)
18+
19+
instance Exception IncorrectUsage where
20+
displayException (IncorrectUsage msg) =
21+
"Incorrect usage: " ++ BS.unpack msg
1722

1823
newtype ProtocolException = ProtocolException ByteString
1924
deriving (Show)
@@ -22,6 +27,9 @@ instance Exception ProtocolException where
2227
displayException (ProtocolException msg) =
2328
"Exception in protocol, " ++ BS.unpack msg
2429

30+
throwIncorrectUsage :: ByteString -> IO a
31+
throwIncorrectUsage = throwIO . IncorrectUsage
32+
2533
throwProtocolEx :: ByteString -> IO a
2634
throwProtocolEx = throwIO . ProtocolException
2735

src/Database/PostgreSQL/Driver/Query.hs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ sendSync conn = sendEncode conn $ encodeClientMessage Sync
4040
sendSimpleQuery :: ConnectionCommon -> B.ByteString -> IO (Either Error ())
4141
sendSimpleQuery conn q = do
4242
sendMessage (connRawConnection conn) $ SimpleQuery (StatementSQL q)
43-
checkErrors <$> collectUntilReadyForQuery conn
43+
(checkErrors =<<) <$> collectUntilReadyForQuery conn
4444
where
45-
checkErrors = either
46-
(Left . ReceiverError)
47-
(maybe (Right ()) (Left . PostgresError) . findFirstError)
45+
checkErrors =
46+
maybe (Right ()) (Left . PostgresError) . findFirstError
4847

4948
waitReadyForQuery :: Connection -> IO (Either Error ())
5049
waitReadyForQuery conn =
@@ -56,8 +55,8 @@ waitReadyForQuery conn =
5655
-- We should wait for ReadyForQuery anyway.
5756
waitReadyForQuery conn
5857
pure . Left $ PostgresError e
59-
-- TODO
60-
(DataMessage _) -> error "incorrect usage waitReadyForQuery"
58+
(DataMessage _) -> throwIncorrectUsage
59+
"Expected ReadyForQuery, but got DataRow message"
6160
DataReady -> pure $ Right ()
6261

6362
-- | Public
@@ -69,8 +68,8 @@ readNextData conn =
6968
handleDataMessage msg = case msg of
7069
(DataError e) -> pure . Left $ PostgresError e
7170
(DataMessage rows) -> pure . Right $ rows
72-
-- TODO
73-
DataReady -> error "incorrect usage readNextData"
71+
DataReady -> throwIncorrectUsage
72+
"Expected DataRow message, but got ReadyForQuery"
7473

7574
-- Helper
7675
sendBatchEndBy :: ClientMessage -> Connection -> [Query] -> IO ()
@@ -115,7 +114,7 @@ describeStatement conn stmt = do
115114
encodeClientMessage (Parse sname (StatementSQL stmt) V.empty)
116115
<> encodeClientMessage (DescribeStatement sname)
117116
<> encodeClientMessage Sync
118-
msgs <- first ReceiverError <$> collectUntilReadyForQuery conn
117+
msgs <- collectUntilReadyForQuery conn
119118
either (pure . Left) parseMessages msgs
120119
where
121120
sname = StatementName ""
@@ -132,11 +131,11 @@ describeStatement conn stmt = do
132131
-- Collects all messages preceding `ReadyForQuery`
133132
collectUntilReadyForQuery
134133
:: ConnectionCommon
135-
-> IO (Either ReceiverException [ServerMessage])
134+
-> IO (Either Error [ServerMessage])
136135
collectUntilReadyForQuery conn = do
137136
msg <- readChan $ connOutChan conn
138137
case msg of
139-
Left e -> pure $ Left e
138+
Left e -> pure $ Left $ ReceiverError e
140139
Right ReadyForQuery{} -> pure $ Right []
141140
Right m -> fmap (m:) <$> collectUntilReadyForQuery conn
142141

tests/Protocol.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ testExtendedQueryNoData = withConnectionCommonAll $ \c -> do
124124

125125
-- | Assert that list contains element satisfies predicat.
126126
assertContains
127-
:: Either ReceiverException [ServerMessage]
127+
:: Either Error [ServerMessage]
128128
-> (ServerMessage -> Bool)
129129
-> String -> Assertion
130130
assertContains (Left e) _ _ = assertFailure $ "Got Error" ++ show e
131131
assertContains (Right msgs) f name =
132132
assertBool ("Does not contain" ++ name) $ any f msgs
133133

134134
-- | Assert there are on `ErrorResponse` in the list.
135-
assertNoErrorResponse :: Either ReceiverException [ServerMessage] -> Assertion
135+
assertNoErrorResponse :: Either Error [ServerMessage] -> Assertion
136136
assertNoErrorResponse (Left e) = assertFailure $ "Got Error" ++ show e
137137
assertNoErrorResponse (Right msgs) =
138138
assertBool "Occured ErrorResponse" $ all (not . isError) msgs

0 commit comments

Comments
 (0)
0