From 1c32e4244eb64d31bbd49f6db8f609862225bfda Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Wed, 20 Aug 2025 22:22:21 +0200 Subject: [PATCH] Add actual row number to row number errors --- lib/Database/PostgreSQL/Opium.hs | 4 ++-- lib/Database/PostgreSQL/Opium/Connection.hs | 3 +++ lib/Database/PostgreSQL/Opium/Error.hs | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/Database/PostgreSQL/Opium.hs b/lib/Database/PostgreSQL/Opium.hs index 70eaec1..4ac99d0 100644 --- a/lib/Database/PostgreSQL/Opium.hs +++ b/lib/Database/PostgreSQL/Opium.hs @@ -59,11 +59,11 @@ instance RowContainer Maybe where extract result nRows columnTable | nRows == 0 = pure Nothing | nRows == 1 = Just <$> ExceptT (fromRow result columnTable 0) - | otherwise = throwE ErrorMoreThanOneRow + | otherwise = throwE $ ErrorMoreThanOneRow $ fromEnum nRows instance RowContainer Identity where extract result nRows columnTable = do - unless (nRows == 1) $ throwE ErrorNotExactlyOneRow + unless (nRows == 1) $ throwE $ ErrorNotExactlyOneRow $ fromEnum nRows Identity <$> ExceptT (fromRow result columnTable 0) -- The order of the type parameters is important, because it is more common to use type applications for providing the row type and row container type. diff --git a/lib/Database/PostgreSQL/Opium/Connection.hs b/lib/Database/PostgreSQL/Opium/Connection.hs index b637d27..3427569 100644 --- a/lib/Database/PostgreSQL/Opium/Connection.hs +++ b/lib/Database/PostgreSQL/Opium/Connection.hs @@ -12,6 +12,7 @@ module Database.PostgreSQL.Opium.Connection ) where import Control.Concurrent.MVar (MVar, newMVar, modifyMVar_, withMVar) +import Control.Exception (Exception) import Data.Maybe (fromMaybe) import Data.Text (Text) import Database.PostgreSQL.LibPQ (ConnStatus (..)) @@ -27,6 +28,8 @@ newtype Connection = Connection newtype ConnectionError = ConnectionError Text deriving (Show) +instance Exception ConnectionError + withRawConnection :: HasCallStack => (Maybe LibPQ.Connection -> IO a) diff --git a/lib/Database/PostgreSQL/Opium/Error.hs b/lib/Database/PostgreSQL/Opium/Error.hs index 0485b97..f1fb890 100644 --- a/lib/Database/PostgreSQL/Opium/Error.hs +++ b/lib/Database/PostgreSQL/Opium/Error.hs @@ -17,8 +17,8 @@ data Error | ErrorInvalidOid Text Oid | ErrorUnexpectedNull ErrorPosition | ErrorInvalidField ErrorPosition Oid ByteString String - | ErrorNotExactlyOneRow - | ErrorMoreThanOneRow + | ErrorNotExactlyOneRow Int + | ErrorMoreThanOneRow Int | ErrorConnectionClosed deriving (Eq, Show)