module Hasql.Pool.SessionErrorDestructors where

import Hasql.Errors qualified as Errors
import Hasql.Pool.Prelude

reset :: (Text -> x) -> x -> Errors.SessionError -> x
reset :: forall x. (Text -> x) -> x -> SessionError -> x
reset Text -> x
onReset x
onNoReset = \case
  Errors.ConnectionSessionError Text
details -> Text -> x
onReset Text
details
  SessionError
_ -> x
onNoReset

requiresConnectionDiscard :: Errors.SessionError -> Bool
requiresConnectionDiscard :: SessionError -> Bool
requiresConnectionDiscard = \case
  Errors.ConnectionSessionError {} -> Bool
True
  Errors.MissingTypesSessionError {} -> Bool
True
  Errors.ScriptSessionError Text
_ ServerError
serverError -> ServerError -> Bool
isStaleServerError ServerError
serverError
  Errors.StatementSessionError Int
_ Int
_ Text
_ [Text]
_ Bool
_ StatementError
statementError -> StatementError -> Bool
statementRequiresConnectionDiscard StatementError
statementError
  Errors.DriverSessionError {} -> Bool
False

discardDetails :: Errors.SessionError -> Maybe Text
discardDetails :: SessionError -> Maybe Text
discardDetails SessionError
err =
  if SessionError -> Bool
requiresConnectionDiscard SessionError
err
    then Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ SessionError -> Text
forall a. IsError a => a -> Text
Errors.toMessage SessionError
err
    else Maybe Text
forall a. Maybe a
Nothing

statementRequiresConnectionDiscard :: Errors.StatementError -> Bool
statementRequiresConnectionDiscard :: StatementError -> Bool
statementRequiresConnectionDiscard = \case
  Errors.ServerStatementError ServerError
serverError -> ServerError -> Bool
isStaleServerError ServerError
serverError
  Errors.UnexpectedColumnTypeStatementError {} -> Bool
True
  StatementError
_ -> Bool
False

isStaleServerError :: Errors.ServerError -> Bool
isStaleServerError :: ServerError -> Bool
isStaleServerError (Errors.ServerError Text
code Text
_ Maybe Text
_ Maybe Text
_ Maybe Int
_) =
  Text
code Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"0A000" Bool -> Bool -> Bool
|| Text
code Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"XX000"