11{-# LANGUAGE CPP, BangPatterns, DoAndIfThenElse, RecordWildCards #-}
22{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
33{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+ {-# LANGUAGE ExistentialQuantification #-}
5+ {-# LANGUAGE InstanceSigs #-}
46
57------------------------------------------------------------------------------
68-- |
@@ -81,6 +83,25 @@ data Connection = Connection {
8183instance Eq Connection where
8284 x == y = connectionHandle x == connectionHandle y
8385
86+ -- | Superclass for postgresql exceptions
87+ data SomePostgreSqlException = forall e . Exception e => SomePostgreSqlException e
88+ deriving Typeable
89+
90+ postgresqlExceptionToException :: Exception e => e -> SomeException
91+ postgresqlExceptionToException = toException . SomePostgreSqlException
92+
93+ postgresqlExceptionFromException :: Exception e => SomeException -> Maybe e
94+ postgresqlExceptionFromException x = do
95+ SomePostgreSqlException a <- fromException x
96+ cast a
97+
98+ instance Show SomePostgreSqlException where
99+ showsPrec :: Int -> SomePostgreSqlException -> ShowS
100+ showsPrec p (SomePostgreSqlException e) = showsPrec p e
101+
102+ instance Exception SomePostgreSqlException where
103+ displayException (SomePostgreSqlException e) = displayException e
104+
84105data SqlError = SqlError {
85106 sqlState :: ByteString
86107 , sqlExecStatus :: ExecStatus
@@ -92,7 +113,10 @@ data SqlError = SqlError {
92113fatalError :: ByteString -> SqlError
93114fatalError msg = SqlError " " FatalError msg " " " "
94115
95- instance Exception SqlError
116+ instance Exception SqlError where
117+ toException = postgresqlExceptionToException
118+ fromException = postgresqlExceptionFromException
119+
96120
97121-- | Exception thrown if 'query' is used to perform an @INSERT@-like
98122-- operation, or 'execute' is used to perform a @SELECT@-like operation.
@@ -101,7 +125,9 @@ data QueryError = QueryError {
101125 , qeQuery :: Query
102126 } deriving (Eq , Show , Typeable )
103127
104- instance Exception QueryError
128+ instance Exception QueryError where
129+ toException = postgresqlExceptionToException
130+ fromException = postgresqlExceptionFromException
105131
106132-- | Exception thrown if a 'Query' could not be formatted correctly.
107133-- This may occur if the number of \'@?@\' characters in the query
@@ -112,7 +138,9 @@ data FormatError = FormatError {
112138 , fmtParams :: [ByteString ]
113139 } deriving (Eq , Show , Typeable )
114140
115- instance Exception FormatError
141+ instance Exception FormatError where
142+ toException = postgresqlExceptionToException
143+ fromException = postgresqlExceptionFromException
116144
117145data ConnectInfo = ConnectInfo {
118146 connectHost :: String
0 commit comments