{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PatternSynonyms #-}
module Hmssql.Errors
( SqlServerError (..)
, sqlErrorNumber
, sqlErrorMessage
, LoginError (..)
, loginErrorNumber
, isTransientLoginError
, LoginRedirected (..)
, MarsNotSupported (..)
, ProtocolError (..)
, ConnectionClosed (ConnectionClosedBy, ConnectionClosed)
, CloseReason (..)
, connectionClosedReason
, ConnectionBroken (..)
, UnusableReason (..)
, ResultAbandoned (..)
, ReentrantCallback (..)
, ReentrantSource (..)
, ConnectionReleased (..)
, SevereError (..)
, severeErrorServerError
, DecodeError (..)
, EncodeError (..)
, UnexpectedResult (..)
) where
import Control.Exception (Exception (..), SomeException)
import Data.Int (Int32)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Word (Word16)
import Numeric (showHex)
import Hmssql.Internal.Derived
import Hmssql.Tds.Parser (DecodeError (..), ProtocolError (..), ResultAbandoned (..))
import Hmssql.Tds.Token (Done (..), ServerMessage (..), doneServerError)
data SqlServerError = SqlServerError
{ SqlServerError -> ServerMessage
sqlError :: !ServerMessage
, SqlServerError -> Maybe Text
sqlErrorStatement :: !(Maybe Text)
}
instance Show SqlServerError where
showsPrec :: Int -> SqlServerError -> ShowS
showsPrec Int
p (SqlServerError ServerMessage
a Maybe Text
b) =
Int -> Addr# -> [Field] -> ShowS
showRecord Int
p Addr#
"SqlServerError"# [Addr#
"sqlError"# Addr# -> ServerMessage -> Field
forall a. Show a => Addr# -> a -> Field
:= ServerMessage
a, Addr#
"sqlErrorStatement"# Addr# -> Maybe Text -> Field
forall a. Show a => Addr# -> a -> Field
:= Maybe Text
b]
{-# OPAQUE showsPrec #-}
instance Exception SqlServerError
sqlErrorNumber :: SqlServerError -> Int32
sqlErrorNumber :: SqlServerError -> Int32
sqlErrorNumber = ServerMessage -> Int32
msgNumber (ServerMessage -> Int32)
-> (SqlServerError -> ServerMessage) -> SqlServerError -> Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqlServerError -> ServerMessage
sqlError
sqlErrorMessage :: SqlServerError -> Text
sqlErrorMessage :: SqlServerError -> Text
sqlErrorMessage = ServerMessage -> Text
msgText (ServerMessage -> Text)
-> (SqlServerError -> ServerMessage) -> SqlServerError -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqlServerError -> ServerMessage
sqlError
newtype LoginError = LoginError ServerMessage
instance Show LoginError where
showsPrec :: Int -> LoginError -> ShowS
showsPrec Int
p (LoginError ServerMessage
a) = Int -> Addr# -> ServerMessage -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"LoginError"# ServerMessage
a
{-# OPAQUE showsPrec #-}
instance Exception LoginError
loginErrorNumber :: LoginError -> Int32
loginErrorNumber :: LoginError -> Int32
loginErrorNumber (LoginError ServerMessage
msg) = ServerMessage -> Int32
msgNumber ServerMessage
msg
isTransientLoginError :: LoginError -> Bool
isTransientLoginError :: LoginError -> Bool
isTransientLoginError LoginError
e = LoginError -> Int32
loginErrorNumber LoginError
e Int32 -> [Int32] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int32
6005, Int32
40613, Int32
40501, Int32
49918, Int32
49919, Int32
49920]
data LoginRedirected = LoginRedirected
{ LoginRedirected -> Text
redirectServer :: !Text
, LoginRedirected -> Word16
redirectPort :: !Word16
}
instance Show LoginRedirected where
showsPrec :: Int -> LoginRedirected -> ShowS
showsPrec Int
p (LoginRedirected Text
a Word16
b) =
Int -> Addr# -> [Field] -> ShowS
showRecord Int
p Addr#
"LoginRedirected"# [Addr#
"redirectServer"# Addr# -> Text -> Field
forall a. Show a => Addr# -> a -> Field
:= Text
a, Addr#
"redirectPort"# Addr# -> Word16 -> Field
forall a. Show a => Addr# -> a -> Field
:= Word16
b]
{-# OPAQUE showsPrec #-}
instance Eq LoginRedirected where
LoginRedirected Text
a Word16
b == :: LoginRedirected -> LoginRedirected -> Bool
== LoginRedirected Text
a' Word16
b' = Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
eq Text
a Text
a' Bool -> Bool -> Bool
&& Word16
b Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
== Word16
b'
{-# OPAQUE (==) #-}
instance Exception LoginRedirected where
displayException :: LoginRedirected -> String
displayException LoginRedirected{Word16
Text
redirectServer :: LoginRedirected -> Text
redirectPort :: LoginRedirected -> Word16
redirectServer :: Text
redirectPort :: Word16
..} =
String
"the login was redirected to " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
redirectServer String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", port " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word16 -> String
forall a. Show a => a -> String
show Word16
redirectPort
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
", and not followed: one redirect too many (hmssql follows ten), or one in plaintext after an encrypted login"
data MarsNotSupported = MarsNotSupported
deriving stock (MarsNotSupported -> MarsNotSupported -> Bool
(MarsNotSupported -> MarsNotSupported -> Bool)
-> (MarsNotSupported -> MarsNotSupported -> Bool)
-> Eq MarsNotSupported
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MarsNotSupported -> MarsNotSupported -> Bool
== :: MarsNotSupported -> MarsNotSupported -> Bool
$c/= :: MarsNotSupported -> MarsNotSupported -> Bool
/= :: MarsNotSupported -> MarsNotSupported -> Bool
Eq, Int -> MarsNotSupported -> ShowS
[MarsNotSupported] -> ShowS
MarsNotSupported -> String
(Int -> MarsNotSupported -> ShowS)
-> (MarsNotSupported -> String)
-> ([MarsNotSupported] -> ShowS)
-> Show MarsNotSupported
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MarsNotSupported -> ShowS
showsPrec :: Int -> MarsNotSupported -> ShowS
$cshow :: MarsNotSupported -> String
show :: MarsNotSupported -> String
$cshowList :: [MarsNotSupported] -> ShowS
showList :: [MarsNotSupported] -> ShowS
Show)
instance Exception MarsNotSupported where
displayException :: MarsNotSupported -> String
displayException MarsNotSupported
MarsNotSupported =
String
"connectMars is set, but the server does not support MARS (multiple active result sets)"
newtype ConnectionClosed = ConnectionClosedBy CloseReason
instance Show ConnectionClosed where
showsPrec :: Int -> ConnectionClosed -> ShowS
showsPrec Int
p (ConnectionClosedBy CloseReason
a) = Int -> Addr# -> CloseReason -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"ConnectionClosedBy"# CloseReason
a
{-# OPAQUE showsPrec #-}
data CloseReason
= ClosedByServer
| ClosedByClient
| TransportFailed SomeException
instance Show CloseReason where
showsPrec :: Int -> CloseReason -> ShowS
showsPrec Int
p = \case
CloseReason
ClosedByServer -> String -> ShowS
showString String
"ClosedByServer"
CloseReason
ClosedByClient -> String -> ShowS
showString String
"ClosedByClient"
TransportFailed SomeException
a -> Int -> Addr# -> SomeException -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"TransportFailed"# SomeException
a
{-# OPAQUE showsPrec #-}
pattern ConnectionClosed :: ConnectionClosed
pattern $bConnectionClosed :: ConnectionClosed
$mConnectionClosed :: forall {r}. ConnectionClosed -> ((# #) -> r) -> ((# #) -> r) -> r
ConnectionClosed <- ConnectionClosedBy _
where
ConnectionClosed = CloseReason -> ConnectionClosed
ConnectionClosedBy CloseReason
ClosedByServer
{-# COMPLETE ConnectionClosed #-}
connectionClosedReason :: ConnectionClosed -> CloseReason
connectionClosedReason :: ConnectionClosed -> CloseReason
connectionClosedReason (ConnectionClosedBy CloseReason
r) = CloseReason
r
instance Exception ConnectionClosed where
displayException :: ConnectionClosed -> String
displayException (ConnectionClosedBy CloseReason
r) = case CloseReason
r of
CloseReason
ClosedByServer -> String
"the server closed the connection"
CloseReason
ClosedByClient -> String
"the connection was closed by close"
TransportFailed SomeException
e -> String
"the connection failed: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SomeException -> String
forall e. Exception e => e -> String
displayException SomeException
e
newtype ConnectionBroken = ConnectionBroken UnusableReason
instance Show ConnectionBroken where
showsPrec :: Int -> ConnectionBroken -> ShowS
showsPrec Int
p (ConnectionBroken UnusableReason
a) = Int -> Addr# -> UnusableReason -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"ConnectionBroken"# UnusableReason
a
{-# OPAQUE showsPrec #-}
instance Exception ConnectionBroken
data UnusableReason
= Closed CloseReason
| ServerProtocolViolation String
| FatalServerError ServerMessage
| ServerFailedConnection Done
| SendFailed SomeException
| ReadFailed SomeException
| DrainFailed SomeException
| ResetRefused (Maybe ServerMessage)
| ResetFailed SomeException
| RollbackFailed SomeException
| ReaderThreadDied SomeException
instance Show UnusableReason where
showsPrec :: Int -> UnusableReason -> ShowS
showsPrec Int
p = \case
Closed CloseReason
a -> Int -> Addr# -> CloseReason -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"Closed"# CloseReason
a
ServerProtocolViolation String
a -> Int -> Addr# -> String -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"ServerProtocolViolation"# String
a
FatalServerError ServerMessage
a -> Int -> Addr# -> ServerMessage -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"FatalServerError"# ServerMessage
a
ServerFailedConnection Done
a -> Int -> Addr# -> Done -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"ServerFailedConnection"# Done
a
SendFailed SomeException
a -> Int -> Addr# -> SomeException -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"SendFailed"# SomeException
a
ReadFailed SomeException
a -> Int -> Addr# -> SomeException -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"ReadFailed"# SomeException
a
DrainFailed SomeException
a -> Int -> Addr# -> SomeException -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"DrainFailed"# SomeException
a
ResetRefused Maybe ServerMessage
a -> Int -> Addr# -> Maybe ServerMessage -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"ResetRefused"# Maybe ServerMessage
a
ResetFailed SomeException
a -> Int -> Addr# -> SomeException -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"ResetFailed"# SomeException
a
RollbackFailed SomeException
a -> Int -> Addr# -> SomeException -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"RollbackFailed"# SomeException
a
ReaderThreadDied SomeException
a -> Int -> Addr# -> SomeException -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"ReaderThreadDied"# SomeException
a
{-# OPAQUE showsPrec #-}
data ReentrantCallback = ReentrantCallback
deriving stock (Int -> ReentrantCallback -> ShowS
[ReentrantCallback] -> ShowS
ReentrantCallback -> String
(Int -> ReentrantCallback -> ShowS)
-> (ReentrantCallback -> String)
-> ([ReentrantCallback] -> ShowS)
-> Show ReentrantCallback
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReentrantCallback -> ShowS
showsPrec :: Int -> ReentrantCallback -> ShowS
$cshow :: ReentrantCallback -> String
show :: ReentrantCallback -> String
$cshowList :: [ReentrantCallback] -> ShowS
showList :: [ReentrantCallback] -> ShowS
Show)
instance Exception ReentrantCallback
data ReentrantSource = ReentrantSource deriving stock (Int -> ReentrantSource -> ShowS
[ReentrantSource] -> ShowS
ReentrantSource -> String
(Int -> ReentrantSource -> ShowS)
-> (ReentrantSource -> String)
-> ([ReentrantSource] -> ShowS)
-> Show ReentrantSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReentrantSource -> ShowS
showsPrec :: Int -> ReentrantSource -> ShowS
$cshow :: ReentrantSource -> String
show :: ReentrantSource -> String
$cshowList :: [ReentrantSource] -> ShowS
showList :: [ReentrantSource] -> ShowS
Show)
instance Exception ReentrantSource
data ConnectionReleased = ConnectionReleased
deriving stock (Int -> ConnectionReleased -> ShowS
[ConnectionReleased] -> ShowS
ConnectionReleased -> String
(Int -> ConnectionReleased -> ShowS)
-> (ConnectionReleased -> String)
-> ([ConnectionReleased] -> ShowS)
-> Show ConnectionReleased
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConnectionReleased -> ShowS
showsPrec :: Int -> ConnectionReleased -> ShowS
$cshow :: ConnectionReleased -> String
show :: ConnectionReleased -> String
$cshowList :: [ConnectionReleased] -> ShowS
showList :: [ConnectionReleased] -> ShowS
Show)
instance Exception ConnectionReleased
data SevereError = SevereError
{ SevereError -> Done
severeErrorDone :: !Done
, SevereError -> Maybe Text
severeErrorStatement :: !(Maybe Text)
}
instance Show SevereError where
showsPrec :: Int -> SevereError -> ShowS
showsPrec Int
p (SevereError Done
a Maybe Text
b) =
Int -> Addr# -> [Field] -> ShowS
showRecord Int
p Addr#
"SevereError"# [Addr#
"severeErrorDone"# Addr# -> Done -> Field
forall a. Show a => Addr# -> a -> Field
:= Done
a, Addr#
"severeErrorStatement"# Addr# -> Maybe Text -> Field
forall a. Show a => Addr# -> a -> Field
:= Maybe Text
b]
{-# OPAQUE showsPrec #-}
instance Exception SevereError where
displayException :: SevereError -> String
displayException SevereError
e =
String
"the server reported a failure without an error message (DONE status 0x"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word16 -> ShowS
forall a. Integral a => a -> ShowS
showHex (Done -> Word16
doneStatus (SevereError -> Done
severeErrorDone SevereError
e)) String
""
String -> ShowS
forall a. [a] -> [a] -> [a]
++ (if SevereError -> Bool
severeErrorServerError SevereError
e then String
", a server error: the connection is closed" else String
"")
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
severeErrorServerError :: SevereError -> Bool
severeErrorServerError :: SevereError -> Bool
severeErrorServerError = Done -> Bool
doneServerError (Done -> Bool) -> (SevereError -> Done) -> SevereError -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SevereError -> Done
severeErrorDone
newtype EncodeError = EncodeError String
deriving stock (EncodeError -> EncodeError -> Bool
(EncodeError -> EncodeError -> Bool)
-> (EncodeError -> EncodeError -> Bool) -> Eq EncodeError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EncodeError -> EncodeError -> Bool
== :: EncodeError -> EncodeError -> Bool
$c/= :: EncodeError -> EncodeError -> Bool
/= :: EncodeError -> EncodeError -> Bool
Eq)
instance Show EncodeError where
showsPrec :: Int -> EncodeError -> ShowS
showsPrec Int
p (EncodeError String
a) = Int -> Addr# -> String -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"EncodeError"# String
a
{-# OPAQUE showsPrec #-}
instance Exception EncodeError
newtype UnexpectedResult = UnexpectedResult String
instance Show UnexpectedResult where
showsPrec :: Int -> UnexpectedResult -> ShowS
showsPrec Int
p (UnexpectedResult String
a) = Int -> Addr# -> String -> ShowS
forall a. Show a => Int -> Addr# -> a -> ShowS
showCon1 Int
p Addr#
"UnexpectedResult"# String
a
{-# OPAQUE showsPrec #-}
instance Exception UnexpectedResult