@@ -76,7 +76,7 @@ import Data.Foldable
7676import Data.Function ((&))
7777import Data.Kind
7878import Data.Monoid
79−import Data.Text (unpack)
79+import Data.Text (pack, Text)
8080import Data.Traversable
8181import Generics.SOP
8282import PostgreSQL.Binary.Encoding (encodingBytes)
@@ -273,7 +273,7 @@ instance IndexedMonadTransPQ PQ where
273273 define (UnsafeDefinition q) = PQ $ \ (K conn) -> do
274274 resultMaybe <- liftBase $ LibPQ.exec conn q
275275 case resultMaybe of
276− Nothing -> error
276+ Nothing -> throw $ ResultException
277277 "define: LibPQ.exec returned no results"
278278 Just result -> return $ K (K result)
279279
@@ -403,7 +403,7 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
403403 q' = q <> ";"
404404 resultMaybe <- liftBase $ LibPQ.execParams conn q' params' LibPQ.Binary
405405 case resultMaybe of
406− Nothing -> error
406+ Nothing -> throw $ ResultException
407407 "manipulateParams: LibPQ.execParams returned no results"
408408 Just result -> do
409409 tryResult result
@@ -415,7 +415,7 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
415415 let temp = "temporary_statement"
416416 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
417417 case prepResultMaybe of
418− Nothing -> error
418+ Nothing -> throw $ ResultException
419419 "traversePrepared: LibPQ.prepare returned no results"
420420 Just prepResult -> tryResult prepResult
421421 results <- for list $ \ params -> do
@@ -424,14 +424,14 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
424424 params' = fmap (fmap toParam') (hcollapse (toParams @x @xs params))
425425 resultMaybe <- LibPQ.execPrepared conn temp params' LibPQ.Binary
426426 case resultMaybe of
427− Nothing -> error
427+ Nothing -> throw $ ResultException
428428 "traversePrepared: LibPQ.execParams returned no results"
429429 Just result -> do
430430 tryResult result
431431 return $ K result
432432 deallocResultMaybe <- LibPQ.exec conn ("DEALLOCATE " <> temp <> ";")
433433 case deallocResultMaybe of
434− Nothing -> error
434+ Nothing -> throw $ ResultException
435435 "traversePrepared: LibPQ.exec DEALLOCATE returned no results"
436436 Just deallocResult -> tryResult deallocResult
437437 return (K results)
@@ -442,7 +442,7 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
442442 let temp = "temporary_statement"
443443 prepResultMaybe <- LibPQ.prepare conn temp q Nothing
444444 case prepResultMaybe of
445− Nothing -> error
445+ Nothing -> throw $ ResultException
446446 "traversePrepared_: LibPQ.prepare returned no results"
447447 Just prepResult -> tryResult prepResult
448448 for_ list $ \ params -> do
@@ -451,12 +451,12 @@ instance (MonadBase IO io, schema0 ~ schema, schema1 ~ schema)
451451 params' = fmap (fmap toParam') (hcollapse (toParams @x @xs params))
452452 resultMaybe <- LibPQ.execPrepared conn temp params' LibPQ.Binary
453453 case resultMaybe of
454− Nothing -> error
454+ Nothing -> throw $ ResultException
455455 "traversePrepared_: LibPQ.execParams returned no results"
456456 Just result -> tryResult result
457457 deallocResultMaybe <- LibPQ.exec conn ("DEALLOCATE " <> temp <> ";")
458458 case deallocResultMaybe of
459− Nothing -> error
459+ Nothing -> throw $ ResultException
460460 "traversePrepared: LibPQ.exec DEALLOCATE returned no results"
461461 Just deallocResult -> tryResult deallocResult
462462 return (K ())
@@ -531,15 +531,15 @@ getRow
531531 -> io y
532532getRow r (K result :: K LibPQ.Result columns) = liftBase $ do
533533 numRows <- LibPQ.ntuples result
534− when (numRows < r) $ error $
535− "getRow: expected at least " <> show r <> "rows but only saw "
536− <> show numRows
534+ when (numRows < r) $ throw $ ResultException $
535+ "getRow: expected at least " <> pack (show r) <> "rows but only saw "
536+ <> pack (show numRows)
537537 let len = fromIntegral (lengthSList (Proxy @columns))
538538 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
539539 case fromList row' of
540− Nothing -> error "getRow: found unexpected length"
540+ Nothing -> throw $ ResultException "getRow: found unexpected length"
541541 Just row -> case fromRow @columns row of
542− Left parseError -> error $ "fromRow: " <> unpack parseError
542+ Left parseError -> throw $ ParseException $ "getRow: " <> parseError
543543 Right y -> return y
544544
545545-- | Intended to be used for unfolding in streaming libraries, `nextRow`
@@ -557,9 +557,9 @@ nextRow total (K result :: K LibPQ.Result columns) r
557557 let len = fromIntegral (lengthSList (Proxy @columns))
558558 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
559559 case fromList row' of
560− Nothing -> error "nextRow: found unexpected length"
560+ Nothing -> throw $ ResultException "nextRow: found unexpected length"
561561 Just row -> case fromRow @columns row of
562− Left parseError -> error $ "nextRow: " <> unpack parseError
562+ Left parseError -> throw $ ParseException $ "nextRow: " <> parseError
563563 Right y -> return $ Just (r+1, y)
564564
565565-- | Get all rows from a `LibPQ.Result`.
@@ -573,9 +573,9 @@ getRows (K result :: K LibPQ.Result columns) = liftBase $ do
573573 for [0 .. numRows - 1] $ \ r -> do
574574 row' <- traverse (LibPQ.getvalue result r) [0 .. len - 1]
575575 case fromList row' of
576− Nothing -> error "getRows: found unexpected length"
576+ Nothing -> throw $ ResultException "getRows: found unexpected length"
577577 Just row -> case fromRow @columns row of
578− Left parseError -> error $ "getRows: " <> unpack parseError
578+ Left parseError -> throw $ ParseException $ "getRows: " <> parseError
579579 Right y -> return y
580580
581581-- | Get the first row if possible from a `LibPQ.Result`.
@@ -589,9 +589,9 @@ firstRow (K result :: K LibPQ.Result columns) = liftBase $ do
589589 let len = fromIntegral (lengthSList (Proxy @columns))
590590 row' <- traverse (LibPQ.getvalue result 0) [0 .. len - 1]
591591 case fromList row' of
592− Nothing -> error "firstRow: found unexpected length"
592+ Nothing -> throw $ ResultException "firstRow: found unexpected length"
593593 Just row -> case fromRow @columns row of
594− Left parseError -> error $ "firstRow: " <> unpack parseError
594+ Left parseError -> throw $ ParseException $ "firstRow: " <> parseError
595595 Right y -> return $ Just y
596596
597597-- | Lifts actions on results from @LibPQ@.
@@ -626,12 +626,15 @@ resultErrorCode
626626resultErrorCode = liftResult (flip LibPQ.resultErrorField LibPQ.DiagSqlstate)
627627
628628data SquealException
629− = SquealException
629+ = PQException
630630 { sqlExecStatus :: LibPQ.ExecStatus
631631 , sqlStateCode :: Maybe ByteString
632632 -- ^ https://www.postgresql.org/docs/current/static/errcodes-appendix.html
633633 , sqlErrorMessage :: Maybe ByteString
634− } deriving (Eq, Show)
634+ }
635+ | ResultException Text
636+ | ParseException Text
637+ deriving (Eq, Show)
635638instance Exception SquealException
636639
637640tryResult
@@ -646,4 +649,4 @@ tryResult result = liftBase $ do
646649 _ -> do
647650 stateCode <- LibPQ.resultErrorField result LibPQ.DiagSqlstate
648651 msg <- LibPQ.resultErrorMessage result
649− throw $ SquealException status stateCode msg
652+ throw $ PQException status stateCode msg