module Hasql.Engine.Errors where

import Hasql.Comms.Recv qualified
import Hasql.Comms.ResultDecoder qualified
import Hasql.Comms.Roundtrip qualified
import Hasql.Comms.RowReader qualified
import Hasql.Platform.Prelude
import TextBuilder qualified

-- * Error types

-- |
-- Error that occurs when attempting to establish a database connection.
--
-- These errors can occur when calling 'Hasql.Connection.acquire', which runs
-- three stages in sequence: it establishes a connection, checks the server
-- version, and initializes session settings. A constructor is named for its
-- stage, then its case: a bare stage name means nothing further is known at
-- that stage, and a stage with only one case needs no prefix. Reaching a
-- stage is therefore a claim about everything before it - a failure at
-- initialization means the connection was established and the server
-- version was accepted.
--
-- Every constructor here publishes only what the driver actually observed.
-- @libpq@ produces no structured signal for a failure while connecting - no
-- 'ServerError' is available - so 'ConnectionAcquireError' and
-- 'ConnectionPasswordRequiredAcquireError' carry prose. A failure during
-- session initialization can go either way, hence the split between
-- 'InitializationConnectionLossAcquireError' and
-- 'InitializationServerErrorAcquireError'.
data AcquireError
  = -- | The connection could not be established, and no structured signal
    -- exists to say why.
    --
    -- This is the residual case at the connection stage: DNS failure,
    -- connection refused, TLS negotiation failure, a rejected password, a
    -- missing database, and any other rejection @libpq@ does not surface a
    -- flag for, all land here alike.
    ConnectionAcquireError
      -- | Human readable details from @libpq@, intended for logging. May be
      -- empty.
      Text
  | -- | The server demanded a password and none was available.
    --
    -- Reported from @PQconnectionNeedsPassword@, a flag @libpq@ sets from
    -- the authentication exchange rather than from message text. A wrong
    -- password, by contrast, reports 'False' on this flag and is
    -- indistinguishable from any other connection refusal, so it lands in
    -- 'ConnectionAcquireError' instead.
    ConnectionPasswordRequiredAcquireError
      -- | Human readable details from @libpq@, intended for logging.
      Text
  | -- | The server's version is below the minimum this driver supports.
    --
    -- The three fields are the server's major, minor and patch version, in
    -- that order. The minimum required version is not carried alongside
    -- them: it is a constant of this library, currently @9.0.0@.
    VersionTooOldAcquireError
      -- | Major version.
      Int
      -- | Minor version.
      Int
      -- | Patch version.
      Int
  | -- | Session initialization failed and the connection died: there is
    -- prose and nothing else.
    --
    -- This covers both @PQexec@ returning no result at all and @PQexec@
    -- returning a result @libpq@ fabricated client-side with no diagnostic
    -- fields on it - the same event either way, distinguished from
    -- 'InitializationServerErrorAcquireError' by whether the result carries
    -- a SQLSTATE, not by whether a result exists.
    InitializationConnectionLossAcquireError
      -- | Human readable details from @libpq@, intended for logging. May be
      -- empty.
      Text
  | -- | Session initialization failed and the server said why.
    --
    -- The server rejected the initialization statement and returned a real
    -- error report, readable through @PQresultErrorField@. This is the one
    -- 'AcquireError' for which 'Hasql.Errors.toSqlState' can return 'Just'.
    InitializationServerErrorAcquireError
      ServerError
  deriving stock (Int -> AcquireError -> ShowS
[AcquireError] -> ShowS
AcquireError -> String
(Int -> AcquireError -> ShowS)
-> (AcquireError -> String)
-> ([AcquireError] -> ShowS)
-> Show AcquireError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AcquireError -> ShowS
showsPrec :: Int -> AcquireError -> ShowS
$cshow :: AcquireError -> String
show :: AcquireError -> String
$cshowList :: [AcquireError] -> ShowS
showList :: [AcquireError] -> ShowS
Show, AcquireError -> AcquireError -> Bool
(AcquireError -> AcquireError -> Bool)
-> (AcquireError -> AcquireError -> Bool) -> Eq AcquireError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AcquireError -> AcquireError -> Bool
== :: AcquireError -> AcquireError -> Bool
$c/= :: AcquireError -> AcquireError -> Bool
/= :: AcquireError -> AcquireError -> Bool
Eq)

-- |
-- Error that occurs during session execution and leaves the connection
-- usable.
--
-- A session is a batch of actions executed in a database connection context.
-- Every constructor here means the connection is still fit to be handed
-- back for reuse - that is the axis 'SessionError' is narrowed on, not
-- provenance. A failure that leaves the connection gone is not a
-- 'SessionError' at all; it is one of the other two constructors of
-- 'UseError'.
--
-- Session errors provide detailed context to help diagnose problems,
-- including SQL text, parameters, and the location of the error within
-- a pipeline of statements.
data SessionError
  = -- | An error occurred while executing a statement in the session.
    --
    -- This wraps statement-level errors and provides additional context about:
    --
    -- * Which statement in the pipeline failed (when multiple statements are batched)
    -- * The SQL text and parameters of the failing statement
    -- * Whether the statement was prepared or unprepared
    --
    -- The error message includes formatted output showing all this context,
    -- making it easier to diagnose issues in production.
    StatementSessionError
      -- | Total number of statements in the running pipeline. 1 if it's executed alone.
      Int
      -- | 0-based index of the statement that failed. 0 if it's executed alone.
      Int
      -- | SQL template of the failing statement.
      Text
      -- | Parameter values as text (for logging purposes).
      [Text]
      -- | Whether the statement was executed as a prepared one.
      Bool
      -- | The underlying statement error.
      StatementError
  | -- | An error occurred while executing a script.
    --
    -- Scripts are multi-statement SQL texts executed via 'Hasql.Session.script'.
    -- Unlike regular statements, scripts don't support parameters or result decoding,
    -- and errors are limited to server-reported issues.
    ScriptSessionError
      -- | The SQL text of the script.
      Text
      -- | The server error that occurred.
      ServerError
  | -- | One or more types referenced in the statement could not be found in the database.
    --
    -- This occurs when using custom types (enums, composite types, domains) that
    -- are resolved by name at runtime, but the types don't exist in the database.
    --
    -- To fix this error:
    --
    -- * Ensure the types are defined in the database
    -- * Check that schema search paths are configured correctly
    -- * Verify that the type names in your code match those in the database
    MissingTypesSessionError
      -- | Set of (schema name, type name) pairs that could not be found.
      --
      -- Schema name is 'Nothing' when the type was looked up without a schema qualifier.
      (HashSet (Maybe Text, Text))
  deriving stock (Int -> SessionError -> ShowS
[SessionError] -> ShowS
SessionError -> String
(Int -> SessionError -> ShowS)
-> (SessionError -> String)
-> ([SessionError] -> ShowS)
-> Show SessionError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SessionError -> ShowS
showsPrec :: Int -> SessionError -> ShowS
$cshow :: SessionError -> String
show :: SessionError -> String
$cshowList :: [SessionError] -> ShowS
showList :: [SessionError] -> ShowS
Show, SessionError -> SessionError -> Bool
(SessionError -> SessionError -> Bool)
-> (SessionError -> SessionError -> Bool) -> Eq SessionError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SessionError -> SessionError -> Bool
== :: SessionError -> SessionError -> Bool
$c/= :: SessionError -> SessionError -> Bool
/= :: SessionError -> SessionError -> Bool
Eq)

-- |
-- Error that 'Hasql.Connection.use' can return.
--
-- 'SessionUseError' means the connection is still usable and is the only
-- constructor 'Control.Monad.Except.catchError' on a 'Hasql.Session.Session'
-- can see. 'ConnectionUseError' means the connection is gone, and no handler
-- inside the session can intercept it - the split is structural, enforced by
-- the 'Session' 'Control.Monad.Monad' instance rather than by any check a
-- handler has to remember to make.
data UseError
  = -- | The session reported an ordinary failure and the connection is
    -- still usable.
    SessionUseError
      SessionError
  | -- | The connection is gone.
    --
    -- This covers every way 'Hasql.Connection.use' can lose the connection:
    -- a dropped socket, a request @libpq@ refused to send outright (e.g.
    -- more than 65535 parameters in one statement), an unexpected response
    -- from the server, or a bug in Hasql. The reason text says which.
    --
    -- 'Hasql.Connection.use' has finished the connection before returning
    -- this, so the 'Hasql.Connection.Connection' the session ran on is
    -- spent: further 'Hasql.Connection.use' on it fails with this same
    -- error, and 'Hasql.Connection.release' is a no-op. Pools must discard
    -- it rather than return it.
    --
    -- Whatever the cause, the driver cannot vouch for the protocol state of
    -- a connection a request failed to leave, and repairing one costs more
    -- than replacing it: the repair would have to push a Sync, which
    -- flushes and commits the very commands the failed session never got to
    -- complete. So this is reported uniformly rather than split by cause -
    -- retry policy belongs to the caller, informed by the reason text and,
    -- for server errors, 'Hasql.Errors.toSqlState'.
    ConnectionUseError
      -- | Human-readable details about what went wrong.
      Text
  deriving stock (Int -> UseError -> ShowS
[UseError] -> ShowS
UseError -> String
(Int -> UseError -> ShowS)
-> (UseError -> String) -> ([UseError] -> ShowS) -> Show UseError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UseError -> ShowS
showsPrec :: Int -> UseError -> ShowS
$cshow :: UseError -> String
show :: UseError -> String
$cshowList :: [UseError] -> ShowS
showList :: [UseError] -> ShowS
Show, UseError -> UseError -> Bool
(UseError -> UseError -> Bool)
-> (UseError -> UseError -> Bool) -> Eq UseError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UseError -> UseError -> Bool
== :: UseError -> UseError -> Bool
$c/= :: UseError -> UseError -> Bool
/= :: UseError -> UseError -> Bool
Eq)

-- |
-- Error that occurs when executing a single statement.
--
-- Statement errors can be caused by server-side issues (SQL errors, constraint violations)
-- or by mismatches between the decoder specification and the actual result structure
-- (wrong number of rows/columns, type mismatches, or cell-level decoding failures).
data StatementError
  = -- | The server rejected the statement and returned an error.
    --
    -- This includes SQL syntax errors, constraint violations, permission errors,
    -- and any other error reported by PostgreSQL during statement execution.
    ServerStatementError ServerError
  | -- | The statement returned a different number of rows than expected.
    --
    -- This occurs when using result decoders like @Decoders.singleRow@ or
    -- @Decoders.rowsAffectedAtLeast@ that have specific row count expectations.
    UnexpectedRowCountStatementError
      -- | Expected minimum number of rows.
      Int
      -- | Expected maximum number of rows.
      Int
      -- | Actual number of rows returned.
      Int
  | -- | The statement returned a different number of columns than expected.
    --
    -- This indicates a mismatch between the decoder specification and the
    -- actual result structure, possibly due to:
    --
    -- * Schema changes (columns added or removed)
    -- * Wrong query (selecting different columns than expected)
    -- * Decoder configuration error
    UnexpectedColumnCountStatementError
      -- | Expected number of columns.
      Int
      -- | Actual number of columns returned.
      Int
  | -- | A column has a different type than expected.
    --
    -- This occurs when the decoder expects a specific PostgreSQL type (by OID)
    -- but the actual column has a different type. This can happen due to:
    --
    -- * Schema changes (column type changed)
    -- * Wrong query (selecting wrong column or using a cast)
    -- * Decoder configuration error
    --
    -- Note: As of version 1.10, Hasql performs strict type checking and will
    -- report this error instead of attempting automatic type coercion.
    UnexpectedColumnTypeStatementError
      -- | 0-based column index where the type mismatch occurred.
      Int
      -- | Expected PostgreSQL type OID.
      Word32
      -- | Actual PostgreSQL type OID of the column.
      Word32
  | -- | An error occurred while decoding a specific row.
    --
    -- This wraps errors that occur at the row or cell level, providing
    -- context about which row failed.
    RowStatementError
      -- | 0-based index of the row that failed to decode.
      Int
      -- | The underlying row-level error.
      RowError
  | -- | The database returned an unexpected result structure.
    --
    -- This is a catch-all error that indicates either:
    --
    -- * An improper statement (e.g., executing a query that doesn't match expectations)
    -- * A schema mismatch between the code and database
    -- * A bug in Hasql or server misbehavior
    UnexpectedResultStatementError
      -- | Human-readable details about what went wrong.
      Text
  deriving stock (Int -> StatementError -> ShowS
[StatementError] -> ShowS
StatementError -> String
(Int -> StatementError -> ShowS)
-> (StatementError -> String)
-> ([StatementError] -> ShowS)
-> Show StatementError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StatementError -> ShowS
showsPrec :: Int -> StatementError -> ShowS
$cshow :: StatementError -> String
show :: StatementError -> String
$cshowList :: [StatementError] -> ShowS
showList :: [StatementError] -> ShowS
Show, StatementError -> StatementError -> Bool
(StatementError -> StatementError -> Bool)
-> (StatementError -> StatementError -> Bool) -> Eq StatementError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StatementError -> StatementError -> Bool
== :: StatementError -> StatementError -> Bool
$c/= :: StatementError -> StatementError -> Bool
/= :: StatementError -> StatementError -> Bool
Eq)

-- |
-- Error reported by the PostgreSQL server when executing a statement.
--
-- The server provides structured error information including error codes
-- (SQL state), messages, and optional context like hints and position information.
--
-- For a complete list of PostgreSQL error codes, see:
-- <https://www.postgresql.org/docs/current/errcodes-appendix.html>
data ServerError
  = ServerError
      -- | SQL State Code (SQLSTATE).
      --
      -- A five-character code that identifies the error class and condition.
      -- Examples: @\"23505\"@ (unique violation), @\"42P01\"@ (undefined table).
      Text
      -- | Primary error message.
      --
      -- A human-readable description of the error.
      Text
      -- | Optional detailed error information.
      --
      -- Additional context about the error, if available.
      (Maybe Text)
      -- | Optional hint for resolving the error.
      --
      -- A suggestion for how to fix the problem, if available.
      (Maybe Text)
      -- | Optional position in the SQL string where the error occurred.
      --
      -- A 1-based character index into the SQL string, if the error
      -- relates to a specific location in the query.
      (Maybe Int)
  deriving stock (Int -> ServerError -> ShowS
[ServerError] -> ShowS
ServerError -> String
(Int -> ServerError -> ShowS)
-> (ServerError -> String)
-> ([ServerError] -> ShowS)
-> Show ServerError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ServerError -> ShowS
showsPrec :: Int -> ServerError -> ShowS
$cshow :: ServerError -> String
show :: ServerError -> String
$cshowList :: [ServerError] -> ShowS
showList :: [ServerError] -> ShowS
Show, ServerError -> ServerError -> Bool
(ServerError -> ServerError -> Bool)
-> (ServerError -> ServerError -> Bool) -> Eq ServerError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ServerError -> ServerError -> Bool
== :: ServerError -> ServerError -> Bool
$c/= :: ServerError -> ServerError -> Bool
/= :: ServerError -> ServerError -> Bool
Eq)

-- |
-- Error that occurs when decoding a result row.
--
-- Row errors indicate problems when processing an individual row from the result set,
-- either at the cell level or during row refinement/validation.
data RowError
  = -- | An error occurred while decoding a specific cell in the row.
    --
    -- This wraps cell-level errors (null handling, deserialization failures)
    -- and provides context about which column failed and its type.
    CellRowError
      -- | 0-based index of the column where the error occurred.
      Int
      -- | PostgreSQL type OID of the column, as reported by the server.
      Word32
      -- | The underlying cell-level error.
      CellError
  | -- | A refinement or validation error when processing the row.
    --
    -- This occurs when using refinement functions in row decoders
    -- (e.g., with 'Decoders.refineRow') to validate or transform
    -- decoded values. The refinement function rejected the row data.
    RefinementRowError
      -- | Human-readable details about why the refinement failed.
      Text
  deriving stock (Int -> RowError -> ShowS
[RowError] -> ShowS
RowError -> String
(Int -> RowError -> ShowS)
-> (RowError -> String) -> ([RowError] -> ShowS) -> Show RowError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RowError -> ShowS
showsPrec :: Int -> RowError -> ShowS
$cshow :: RowError -> String
show :: RowError -> String
$cshowList :: [RowError] -> ShowS
showList :: [RowError] -> ShowS
Show, RowError -> RowError -> Bool
(RowError -> RowError -> Bool)
-> (RowError -> RowError -> Bool) -> Eq RowError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RowError -> RowError -> Bool
== :: RowError -> RowError -> Bool
$c/= :: RowError -> RowError -> Bool
/= :: RowError -> RowError -> Bool
Eq)

-- |
-- Error that occurs when decoding a single cell (column value) in a result row.
--
-- Cell errors indicate problems with individual values returned by the database,
-- such as unexpected nulls or failures in binary deserialization.
data CellError
  = -- | A NULL value was encountered when a non-NULL value was expected.
    --
    -- This occurs when using non-nullable decoders (e.g., @Decoders.nonNullable@)
    -- on a column that contains a NULL value.
    UnexpectedNullCellError
  | -- | Failed to deserialize the cell value from its binary representation.
    --
    -- This can occur when:
    --
    -- * The binary data is corrupted
    -- * The decoder doesn't match the actual data type
    -- * The data format is invalid for the expected type
    DeserializationCellError
      -- | Human-readable error message describing what went wrong.
      Text
  deriving stock (Int -> CellError -> ShowS
[CellError] -> ShowS
CellError -> String
(Int -> CellError -> ShowS)
-> (CellError -> String)
-> ([CellError] -> ShowS)
-> Show CellError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CellError -> ShowS
showsPrec :: Int -> CellError -> ShowS
$cshow :: CellError -> String
show :: CellError -> String
$cshowList :: [CellError] -> ShowS
showList :: [CellError] -> ShowS
Show, CellError -> CellError -> Bool
(CellError -> CellError -> Bool)
-> (CellError -> CellError -> Bool) -> Eq CellError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CellError -> CellError -> Bool
== :: CellError -> CellError -> Bool
$c/= :: CellError -> CellError -> Bool
/= :: CellError -> CellError -> Bool
Eq)

-- |
-- Whether a round trip error is a @Parse@ failing with SQLSTATE @42P05@
-- ("prepared statement \"...\" already exists").
--
-- Since prepared statement names are content-addressed (a SHA-256 digest of
-- the SQL text and the parameter OIDs), a collision on the name is - up to a
-- hash collision - a collision on the statement itself: whatever the server
-- already holds under that name is the very statement that was about to be
-- parsed. Callers that evict a cache entry on ordinary @Parse@ failures
-- should keep it in this one case, since the statement is in fact on the
-- server under the name they just tried to commit.
--
-- This is a fact about Hasql's own naming scheme, not something derivable
-- from the PostgreSQL error-code appendix: nothing about @42P05@ in general
-- says a collision is safe to treat as a cache hit, only Hasql's
-- content-addressing does.
isPrepareCollision :: Hasql.Comms.Roundtrip.Error tag -> Bool
isPrepareCollision :: forall tag. Error tag -> Bool
isPrepareCollision = \case
  Hasql.Comms.Roundtrip.ServerError (Hasql.Comms.Recv.ResultError tag
_ Int
_ (Hasql.Comms.ResultDecoder.ServerError ByteString
code ByteString
_ Maybe ByteString
_ Maybe ByteString
_ Maybe Int
_)) ->
    ByteString
code ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
"42P05"
  Error tag
_ -> Bool
False

-- | Turn a failed @libpq@ send into a 'UseError'.
--
-- A failed send always finishes the connection: either the socket is gone,
-- or @libpq@ refused the request with commands possibly queued behind it and
-- no way to tell what the server has seen. Neither is worth repairing - a
-- repair would have to push a Sync, which flushes and commits the very
-- commands the failed session never got to complete - so the connection is
-- finished instead.
fromSendError :: Text -> UseError
fromSendError :: Text -> UseError
fromSendError = Text -> UseError
ConnectionUseError

fromRoundtripError :: Hasql.Comms.Roundtrip.Error tag -> UseError
fromRoundtripError :: forall tag. Error tag -> UseError
fromRoundtripError = \case
  Hasql.Comms.Roundtrip.ClientError tag
_tag Text
details ->
    Text -> UseError
fromSendError Text
details
  Hasql.Comms.Roundtrip.ServerError Error tag
recvError ->
    Error (Maybe (Int, Int, ByteString, [Text], Bool)) -> UseError
fromRecvError (Maybe (Int, Int, ByteString, [Text], Bool)
forall a. Maybe a
Nothing Maybe (Int, Int, ByteString, [Text], Bool)
-> Error tag -> Error (Maybe (Int, Int, ByteString, [Text], Bool))
forall a b. a -> Error b -> Error a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Error tag
recvError)

fromRecvError :: Hasql.Comms.Recv.Error (Maybe (Int, Int, ByteString, [Text], Bool)) -> UseError
fromRecvError :: Error (Maybe (Int, Int, ByteString, [Text], Bool)) -> UseError
fromRecvError = \case
  Hasql.Comms.Recv.ResultError Maybe (Int, Int, ByteString, [Text], Bool)
tag Int
_resultOffset Error
resultError ->
    case Maybe (Int, Int, ByteString, [Text], Bool)
tag of
      Maybe (Int, Int, ByteString, [Text], Bool)
Nothing ->
        (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
          [ TextBuilder
"Unexpected error in an operation not associated with any statement. ",
            TextBuilder
"This indicates a bug in Hasql or the server misbehaving. ",
            TextBuilder
"Error: ",
            String -> TextBuilder
TextBuilder.string (Error -> String
forall a. Show a => a -> String
show Error
resultError)
          ]
      Just (Int
totalStatements, Int
statementIndex, ByteString
sql, [Text]
parameters, Bool
prepared) ->
        SessionError -> UseError
SessionUseError
          (SessionError -> UseError) -> SessionError -> UseError
forall a b. (a -> b) -> a -> b
$ Int
-> Int -> Text -> [Text] -> Bool -> StatementError -> SessionError
StatementSessionError
            Int
totalStatements
            Int
statementIndex
            (ByteString -> Text
decodeUtf8Lenient ByteString
sql)
            [Text]
parameters
            Bool
prepared
            (Error -> StatementError
fromStatementResultError Error
resultError)
  Hasql.Comms.Recv.NoResultsError Maybe (Int, Int, ByteString, [Text], Bool)
tag Bool
connectionLost Maybe ByteString
details ->
    if Bool
connectionLost
      then -- The results are not late, they are never coming: the socket is
      -- gone. Reporting this as a row-count mismatch would describe a
      -- dropped connection as a statement that returned nothing, and
      -- leave pools reusing a connection that no longer exists.
        Text -> UseError
ConnectionUseError (Text -> (ByteString -> Text) -> Maybe ByteString -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" ByteString -> Text
decodeUtf8Lenient Maybe ByteString
details)
      else case Maybe (Int, Int, ByteString, [Text], Bool)
tag of
        Maybe (Int, Int, ByteString, [Text], Bool)
Nothing ->
          (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
            [ TextBuilder
"Unexpectedly got no results in an operation not associated with any statement. ",
              TextBuilder
"This indicates a bug in Hasql or the server misbehaving. ",
              TextBuilder
"Details: ",
              String -> TextBuilder
TextBuilder.string (Maybe ByteString -> String
forall a. Show a => a -> String
show Maybe ByteString
details)
            ]
        Just (Int
totalStatements, Int
statementIndex, ByteString
sql, [Text]
parameters, Bool
prepared) ->
          SessionError -> UseError
SessionUseError
            (SessionError -> UseError) -> SessionError -> UseError
forall a b. (a -> b) -> a -> b
$ Int
-> Int -> Text -> [Text] -> Bool -> StatementError -> SessionError
StatementSessionError
              Int
totalStatements
              Int
statementIndex
              (ByteString -> Text
decodeUtf8Lenient ByteString
sql)
              [Text]
parameters
              Bool
prepared
              (Int -> Int -> Int -> StatementError
UnexpectedRowCountStatementError Int
1 Int
1 Int
0)
  Hasql.Comms.Recv.TooManyResultsError Maybe (Int, Int, ByteString, [Text], Bool)
tag Int
actual ->
    case Maybe (Int, Int, ByteString, [Text], Bool)
tag of
      Maybe (Int, Int, ByteString, [Text], Bool)
Nothing ->
        (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
          [ TextBuilder
"Unexpectedly got too many results in an operation not associated with any statement. ",
            TextBuilder
"This indicates a bug in Hasql or the server misbehaving. ",
            TextBuilder
"Amount: ",
            Int -> TextBuilder
forall a. Integral a => a -> TextBuilder
TextBuilder.decimal Int
actual
          ]
      Just (Int
totalStatements, Int
statementIndex, ByteString
sql, [Text]
parameters, Bool
prepared) ->
        SessionError -> UseError
SessionUseError
          (SessionError -> UseError) -> SessionError -> UseError
forall a b. (a -> b) -> a -> b
$ Int
-> Int -> Text -> [Text] -> Bool -> StatementError -> SessionError
StatementSessionError
            Int
totalStatements
            Int
statementIndex
            (ByteString -> Text
decodeUtf8Lenient ByteString
sql)
            [Text]
parameters
            Bool
prepared
            (Int -> Int -> Int -> StatementError
UnexpectedRowCountStatementError Int
1 Int
1 Int
actual)

fromStatementResultError :: Hasql.Comms.ResultDecoder.Error -> StatementError
fromStatementResultError :: Error -> StatementError
fromStatementResultError = \case
  Hasql.Comms.ResultDecoder.ServerError ByteString
code ByteString
message Maybe ByteString
detail Maybe ByteString
hint Maybe Int
position ->
    ServerError -> StatementError
ServerStatementError
      ( Text
-> Text -> Maybe Text -> Maybe Text -> Maybe Int -> ServerError
ServerError
          (ByteString -> Text
decodeUtf8Lenient ByteString
code)
          (ByteString -> Text
decodeUtf8Lenient ByteString
message)
          ((ByteString -> Text) -> Maybe ByteString -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
decodeUtf8Lenient Maybe ByteString
detail)
          ((ByteString -> Text) -> Maybe ByteString -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
decodeUtf8Lenient Maybe ByteString
hint)
          Maybe Int
position
      )
  Hasql.Comms.ResultDecoder.UnexpectedResult Text
msg ->
    Text -> StatementError
UnexpectedResultStatementError Text
msg
  Hasql.Comms.ResultDecoder.UnexpectedRowCount Int
actual ->
    Int -> Int -> Int -> StatementError
UnexpectedRowCountStatementError Int
1 Int
1 Int
actual
  Hasql.Comms.ResultDecoder.UnexpectedColumnCount Int
expected Int
actual ->
    Int -> Int -> StatementError
UnexpectedColumnCountStatementError Int
expected Int
actual
  Hasql.Comms.ResultDecoder.DecoderTypeMismatch Int
colIdx Word32
expectedOid Word32
actualOid ->
    Int -> Word32 -> Word32 -> StatementError
UnexpectedColumnTypeStatementError
      Int
colIdx
      Word32
expectedOid
      Word32
actualOid
  Hasql.Comms.ResultDecoder.RowError Int
rowIndex Error
rowError ->
    case Error
rowError of
      Hasql.Comms.RowReader.CellError Int
column Word32
oid CellError
cellErr ->
        Int -> RowError -> StatementError
RowStatementError
          Int
rowIndex
          ( Int -> Word32 -> CellError -> RowError
CellRowError
              Int
column
              Word32
oid
              ( case CellError
cellErr of
                  Hasql.Comms.RowReader.DecodingCellError Text
msg -> Text -> CellError
DeserializationCellError Text
msg
                  CellError
Hasql.Comms.RowReader.UnexpectedNullCellError -> CellError
UnexpectedNullCellError
              )
          )
      Hasql.Comms.RowReader.RefinementError Text
msg ->
        Int -> RowError -> StatementError
RowStatementError
          Int
rowIndex
          (Text -> RowError
RefinementRowError Text
msg)

fromRecvErrorInScript :: ByteString -> Hasql.Comms.Recv.Error (Maybe ByteString) -> UseError
fromRecvErrorInScript :: ByteString -> Error (Maybe ByteString) -> UseError
fromRecvErrorInScript ByteString
scriptSql = \case
  Hasql.Comms.Recv.ResultError Maybe ByteString
_ Int
_ Error
resultError ->
    case Error
resultError of
      Hasql.Comms.ResultDecoder.ServerError ByteString
code ByteString
message Maybe ByteString
detail Maybe ByteString
hint Maybe Int
position ->
        SessionError -> UseError
SessionUseError
          (SessionError -> UseError) -> SessionError -> UseError
forall a b. (a -> b) -> a -> b
$ Text -> ServerError -> SessionError
ScriptSessionError
            (ByteString -> Text
decodeUtf8Lenient ByteString
scriptSql)
            ( Text
-> Text -> Maybe Text -> Maybe Text -> Maybe Int -> ServerError
ServerError
                (ByteString -> Text
decodeUtf8Lenient ByteString
code)
                (ByteString -> Text
decodeUtf8Lenient ByteString
message)
                ((ByteString -> Text) -> Maybe ByteString -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
decodeUtf8Lenient Maybe ByteString
detail)
                ((ByteString -> Text) -> Maybe ByteString -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
decodeUtf8Lenient Maybe ByteString
hint)
                Maybe Int
position
            )
      Hasql.Comms.ResultDecoder.UnexpectedResult Text
msg ->
        (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
          [ TextBuilder
"Unexpected result in script: ",
            Text -> TextBuilder
TextBuilder.text Text
msg
          ]
      Hasql.Comms.ResultDecoder.UnexpectedRowCount Int
actual ->
        (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
          [ TextBuilder
"Unexpected amount of rows in script: ",
            Int -> TextBuilder
forall a. Integral a => a -> TextBuilder
TextBuilder.decimal Int
actual
          ]
      Hasql.Comms.ResultDecoder.UnexpectedColumnCount Int
expected Int
actual ->
        (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
          [ TextBuilder
"Unexpected amount of columns in script: expected ",
            Int -> TextBuilder
forall a. Integral a => a -> TextBuilder
TextBuilder.decimal Int
expected,
            TextBuilder
", got ",
            Int -> TextBuilder
forall a. Integral a => a -> TextBuilder
TextBuilder.decimal Int
actual
          ]
      Hasql.Comms.ResultDecoder.DecoderTypeMismatch Int
colIdx Word32
expectedOid Word32
actualOid ->
        (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
          [ TextBuilder
"Decoder type mismatch in script: expected OID ",
            String -> TextBuilder
TextBuilder.string (Word32 -> String
forall a. Show a => a -> String
show Word32
expectedOid),
            TextBuilder
" at column ",
            Int -> TextBuilder
forall a. Integral a => a -> TextBuilder
TextBuilder.decimal Int
colIdx,
            TextBuilder
", got ",
            String -> TextBuilder
TextBuilder.string (Word32 -> String
forall a. Show a => a -> String
show Word32
actualOid)
          ]
      Hasql.Comms.ResultDecoder.RowError Int
rowIndex Error
rowError ->
        (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
          [ TextBuilder
"Row error in script at row ",
            Int -> TextBuilder
forall a. Integral a => a -> TextBuilder
TextBuilder.decimal Int
rowIndex,
            TextBuilder
": ",
            String -> TextBuilder
TextBuilder.string (Error -> String
forall a. Show a => a -> String
show Error
rowError)
          ]
  Hasql.Comms.Recv.NoResultsError Maybe ByteString
_ Bool
connectionLost Maybe ByteString
details ->
    if Bool
connectionLost
      then Text -> UseError
ConnectionUseError (Text -> (ByteString -> Text) -> Maybe ByteString -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" ByteString -> Text
decodeUtf8Lenient Maybe ByteString
details)
      else
        (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
          [ TextBuilder
"Got no results in script.",
            TextBuilder
" This indicates a bug in Hasql or the server misbehaving.",
            Maybe ByteString
details
              Maybe ByteString
-> (Maybe ByteString -> Maybe ByteString) -> Maybe ByteString
forall a b. a -> (a -> b) -> b
& (ByteString -> Bool) -> Maybe ByteString -> Maybe ByteString
forall a. (a -> Bool) -> Maybe a -> Maybe a
forall (f :: * -> *) a. Filterable f => (a -> Bool) -> f a -> f a
filter (ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= ByteString
"")
              Maybe ByteString
-> (Maybe ByteString -> TextBuilder) -> TextBuilder
forall a b. a -> (a -> b) -> b
& (ByteString -> TextBuilder) -> Maybe ByteString -> TextBuilder
forall m a. Monoid m => (a -> m) -> Maybe a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (TextBuilder -> TextBuilder -> TextBuilder
forall a. Monoid a => a -> a -> a
mappend TextBuilder
" Details: " (TextBuilder -> TextBuilder)
-> (ByteString -> TextBuilder) -> ByteString -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> TextBuilder
TextBuilder.text (Text -> TextBuilder)
-> (ByteString -> Text) -> ByteString -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Text
decodeUtf8Lenient)
          ]
  Hasql.Comms.Recv.TooManyResultsError Maybe ByteString
_ Int
actual ->
    (Text -> UseError
ConnectionUseError (Text -> UseError)
-> ([TextBuilder] -> Text) -> [TextBuilder] -> UseError
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TextBuilder -> Text
TextBuilder.toText (TextBuilder -> Text)
-> ([TextBuilder] -> TextBuilder) -> [TextBuilder] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat)
      [ TextBuilder
"Got too many results in script. ",
        TextBuilder
"This indicates a bug in Hasql or the server misbehaving. ",
        TextBuilder
"Amount: ",
        Int -> TextBuilder
forall a. Integral a => a -> TextBuilder
TextBuilder.decimal Int
actual
      ]