git.iko.soy/typeable/postgresql-simple
on master help sign in
Added proper error handling to finishQuery, and tightened up finishExecute
commitaa8410062b25a8ebd9e61b980fef3844ab691460 authorJoey Adams <joeyadams3.14159@gmail.com> dateTue 8 May 2012, 22:51 (14 years ago) parentc2e99ee
2 files changed+26−10
src/Database/PostgreSQL/Simple.hs+9 src/Database/PostgreSQL/Simple/Internal.hs+17−10
src/Database/PostgreSQL/Simple.hs+9
@@ -428,6 +428,8 @@ finishQuery :: (FromRow r) => Connection -> Query -> PQ.Result -> IO [r]
428428finishQuery conn q result = do
429429 status <- PQ.resultStatus result
430430 case status of
431+ PQ.EmptyQuery ->
432+ throwIO $ QueryError "query: Empty query" q
431433 PQ.CommandOk -> do
432434 throwIO $ QueryError "query resulted in a command response" q
433435 PQ.TuplesOk -> do
@@ -455,6 +457,13 @@ finishQuery conn q result = do
455457 Errors [] -> throwIO $ ConversionFailed "" "" "unknown error"
456458 Errors [x] -> throwIO x
457459 Errors xs -> throwIO $ ManyErrors xs
460+ PQ.CopyOut ->
461+ throwIO $ QueryError "query: COPY TO is not supported" q
462+ PQ.CopyIn ->
463+ throwIO $ QueryError "query: COPY FROM is not supported" q
464+ PQ.BadResponse -> throwResultError "query" result status
465+ PQ.NonfatalError -> throwResultError "query" result status
466+ PQ.FatalError -> throwResultError "query" result status
458467
459468ellipsis :: ByteString -> ByteString
460469ellipsis bs
src/Database/PostgreSQL/Simple/Internal.hs+17−10
@@ -224,6 +224,7 @@ finishExecute :: Connection -> Query -> PQ.Result -> IO Int64
224224finishExecute _conn q result = do
225225 status <- PQ.resultStatus result
226226 case status of
227+ PQ.EmptyQuery -> throwIO $ QueryError "execute: Empty query" q
227228 PQ.CommandOk -> do
228229 ncols <- PQ.nfields result
229230 if ncols /= 0
@@ -238,16 +239,13 @@ finishExecute _conn q result = do
238239 ncols <- PQ.nfields result
239240 throwIO $ QueryError ("execute resulted in " ++ show ncols ++
240241 "-column result") q
241− PQ.CopyIn -> fail "FIXME: postgresql-simple does not currently support COPY IN"
242− PQ.CopyOut -> fail "FIXME: postgresql-simple does not currently support COPY OUT"
243− _ -> do
244− errormsg <- maybe "" id <$> PQ.resultErrorMessage result
245− statusmsg <- PQ.resStatus status
246− state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
247− throwIO $ SqlError { sqlState = state
248− , sqlNativeError = fromEnum status
249− , sqlErrorMsg = B.concat [ "execute: ", statusmsg
250− , ": ", errormsg ]}
242+ PQ.CopyOut ->
243+ throwIO $ QueryError "execute: COPY TO is not supported" q
244+ PQ.CopyIn ->
245+ throwIO $ QueryError "execute: COPY FROM is not supported" q
246+ PQ.BadResponse -> throwResultError "execute" result status
247+ PQ.NonfatalError -> throwResultError "execute" result status
248+ PQ.FatalError -> throwResultError "execute" result status
251249 where
252250 toInteger str = B8.foldl' delta 0 str
253251 where
@@ -256,6 +254,15 @@ finishExecute _conn q result = do
256254 then 10 * acc + fromIntegral (ord c - ord '0')
257255 else error ("finishExecute: not an int: " ++ B8.unpack str)
258256
257+throwResultError :: ByteString -> PQ.Result -> PQ.ExecStatus -> IO a
258+throwResultError context result status = do
259+ errormsg <- maybe "" id <$> PQ.resultErrorMessage result
260+ statusmsg <- PQ.resStatus status
261+ state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
262+ throwIO $ SqlError { sqlState = state
263+ , sqlNativeError = fromEnum status
264+ , sqlErrorMsg = B.concat [ context, ": ", statusmsg
265+ , ": ", errormsg ]}
259266
260267disconnectedError :: SqlError
261268disconnectedError = SqlError {