git.iko.soy/typeable/postgresql-simple
on master help sign in
Added proper error handling to finishQuery, and tightened up finishExecute
aa84100Joey Adams
src/Database/PostgreSQL/Simple.hs+9
@@ -428,6 +428,8 @@ finishQuery :: (FromRow r) => Connection -> Query -> PQ.Result -> IO [r]
428finishQuery conn q result = do
428finishQuery conn q result = do
429 status <- PQ.resultStatus result
429 status <- PQ.resultStatus result
430 case status of
430 case status of
431+ PQ.EmptyQuery ->
432+ throwIO $ QueryError "query: Empty query" q
431 PQ.CommandOk -> do
433 PQ.CommandOk -> do
432 throwIO $ QueryError "query resulted in a command response" q
434 throwIO $ QueryError "query resulted in a command response" q
433 PQ.TuplesOk -> do
435 PQ.TuplesOk -> do
@@ -455,6 +457,13 @@ finishQuery conn q result = do
455 Errors [] -> throwIO $ ConversionFailed "" "" "unknown error"
457 Errors [] -> throwIO $ ConversionFailed "" "" "unknown error"
456 Errors [x] -> throwIO x
458 Errors [x] -> throwIO x
457 Errors xs -> throwIO $ ManyErrors xs
459 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
458
467
459ellipsis :: ByteString -> ByteString
468ellipsis :: ByteString -> ByteString
460ellipsis bs
469ellipsis bs
src/Database/PostgreSQL/Simple/Internal.hs+17−10
@@ -224,6 +224,7 @@ finishExecute :: Connection -> Query -> PQ.Result -> IO Int64
224finishExecute _conn q result = do
224finishExecute _conn q result = do
225 status <- PQ.resultStatus result
225 status <- PQ.resultStatus result
226 case status of
226 case status of
227+ PQ.EmptyQuery -> throwIO $ QueryError "execute: Empty query" q
227 PQ.CommandOk -> do
228 PQ.CommandOk -> do
228 ncols <- PQ.nfields result
229 ncols <- PQ.nfields result
229 if ncols /= 0
230 if ncols /= 0
@@ -238,16 +239,13 @@ finishExecute _conn q result = do
238 ncols <- PQ.nfields result
239 ncols <- PQ.nfields result
239 throwIO $ QueryError ("execute resulted in " ++ show ncols ++
240 throwIO $ QueryError ("execute resulted in " ++ show ncols ++
240 "-column result") q
241 "-column result") q
241− PQ.CopyIn -> fail "FIXME: postgresql-simple does not currently support COPY IN"
242+ PQ.CopyOut ->
242− PQ.CopyOut -> fail "FIXME: postgresql-simple does not currently support COPY OUT"
243+ throwIO $ QueryError "execute: COPY TO is not supported" q
243− _ -> do
244+ PQ.CopyIn ->
244− errormsg <- maybe "" id <$> PQ.resultErrorMessage result
245+ throwIO $ QueryError "execute: COPY FROM is not supported" q
245− statusmsg <- PQ.resStatus status
246+ PQ.BadResponse -> throwResultError "execute" result status
246− state <- maybe "" id <$> PQ.resultErrorField result PQ.DiagSqlstate
247+ PQ.NonfatalError -> throwResultError "execute" result status
247− throwIO $ SqlError { sqlState = state
248+ PQ.FatalError -> throwResultError "execute" result status
248− , sqlNativeError = fromEnum status
249− , sqlErrorMsg = B.concat [ "execute: ", statusmsg
250− , ": ", errormsg ]}
251 where
249 where
252 toInteger str = B8.foldl' delta 0 str
250 toInteger str = B8.foldl' delta 0 str
253 where
251 where
@@ -256,6 +254,15 @@ finishExecute _conn q result = do
256 then 10 * acc + fromIntegral (ord c - ord '0')
254 then 10 * acc + fromIntegral (ord c - ord '0')
257 else error ("finishExecute: not an int: " ++ B8.unpack str)
255 else error ("finishExecute: not an int: " ++ B8.unpack str)
258
256
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 ]}
259
266
260disconnectedError :: SqlError
267disconnectedError :: SqlError
261disconnectedError = SqlError {
268disconnectedError = SqlError {