hasql-1.10.3: Fast PostgreSQL driver with a flexible mapping API
Safe HaskellNone
LanguageHaskell2010

Hasql.Errors

Description

Explicit error types for all Hasql operations.

This module provides access to all error types used throughout Hasql:

The module follows Hasql's philosophy of explicit error handling, where all errors are represented as values rather than exceptions.

Synopsis

Error class

class IsError a where Source #

A class for types that can be treated as errors.

Methods

toMessage :: a -> Text Source #

Convert the error to a human-readable message with no dynamic details.

toDetails :: a -> [(Text, Text)] Source #

Convert the error to a list of key-value pairs of dynamic details.

isTransient :: a -> Bool Source #

Whether the error is transient and the operation causing it can be retried.

toDetailedText :: IsError e => e -> Text Source #

Convert the error to a multiline detailed human-readable text representation containing all details.

Connection errors

data ConnectionError Source #

Error that occurs when attempting to establish a database connection.

These errors can occur when calling acquire. Connection errors are categorized into several types to help with error handling and logging.

Constructors

NetworkingConnectionError

Network-level error while connecting to the database server.

This typically indicates issues like:

  • Server is not reachable (wrong host/port or server is down)
  • Network connectivity problems
  • Firewall blocking the connection
  • Connection timeout

These errors are transient and the operation can be retried.

Fields

  • Text

    Human readable details intended for logging.

AuthenticationConnectionError

Authentication failed when connecting to the database.

This typically indicates issues like:

  • Invalid username or password
  • User does not have permission to access the database
  • Authentication method mismatch (e.g., server requires SSL but client doesn't use it)

These errors are not transient and require fixing the credentials or permissions.

Fields

  • Text

    Human readable details intended for logging.

CompatibilityConnectionError

Compatibility issue between client and server.

This typically indicates issues like:

  • Server version is too old or too new
  • Required PostgreSQL features are not available
  • Protocol version mismatch

These errors are not transient and require upgrading/downgrading the server or client.

Fields

  • Text

    Human readable details intended for logging.

OtherConnectionError

Uncategorized error coming from libpq.

This is a catch-all for connection errors that don't fit into other categories. The error message may be empty if libpq doesn't provide details.

These errors are not transient by default.

Fields

  • Text

    Human readable details intended for logging. May be empty.

Session errors

data SessionError Source #

Error that occurs during session execution.

A session is a batch of actions executed in a database connection context. Session errors can occur due to statement failures, connection issues, missing types, or internal driver errors.

Session errors provide detailed context to help diagnose problems, including SQL text, parameters, and the location of the error within a pipeline of statements.

Constructors

StatementSessionError

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.

Fields

  • Int

    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.

  • Text

    SQL template of the failing statement.

  • [Text]

    Parameter values as text (for logging purposes).

  • Bool

    Whether the statement was executed as a prepared one.

  • StatementError

    The underlying statement error.

ScriptSessionError

An error occurred while executing a script.

Scripts are multi-statement SQL texts executed via script. Unlike regular statements, scripts don't support parameters or result decoding, and errors are limited to server-reported issues.

Fields

ConnectionSessionError

A connection-level error occurred during session execution.

This indicates that the connection to the database was lost or became unusable during the session. These errors are transient and the operation can be retried with a new connection.

Note: As of version 1.10, connections recover from async exceptions without resetting, preserving connection-local state.

Fields

  • Text

    Human-readable details about the connection error.

MissingTypesSessionError

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

Fields

  • (HashSet (Maybe Text, Text))

    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.

DriverSessionError

An internal driver error occurred.

This indicates either:

  • A bug in Hasql
  • The PostgreSQL server misbehaving
  • An unexpected response from the server

If you encounter this error, please report it as a bug.

Fields

  • Text

    Human-readable details about what went wrong.

data StatementError Source #

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).

Constructors

ServerStatementError ServerError

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.

UnexpectedRowCountStatementError

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.

Fields

  • Int

    Expected minimum number of rows.

  • Int

    Expected maximum number of rows.

  • Int

    Actual number of rows returned.

UnexpectedColumnCountStatementError

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

Fields

  • Int

    Expected number of columns.

  • Int

    Actual number of columns returned.

UnexpectedColumnTypeStatementError

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.

Fields

  • Int

    0-based column index where the type mismatch occurred.

  • Word32

    Expected PostgreSQL type OID.

  • Word32

    Actual PostgreSQL type OID of the column.

RowStatementError

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.

Fields

  • Int

    0-based index of the row that failed to decode.

  • RowError

    The underlying row-level error.

UnexpectedResultStatementError

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

Fields

  • Text

    Human-readable details about what went wrong.

data RowError Source #

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.

Constructors

CellRowError

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.

Fields

  • Int

    0-based index of the column where the error occurred.

  • Word32

    PostgreSQL type OID of the column, as reported by the server.

  • CellError

    The underlying cell-level error.

RefinementRowError

A refinement or validation error when processing the row.

This occurs when using refinement functions in row decoders (e.g., with refineRow) to validate or transform decoded values. The refinement function rejected the row data.

Fields

  • Text

    Human-readable details about why the refinement failed.

Instances

Instances details
Show RowError Source # 
Instance details

Defined in Hasql.Engine.Errors

Eq RowError Source # 
Instance details

Defined in Hasql.Engine.Errors

IsError RowError Source # 
Instance details

Defined in Hasql.Errors

data CellError Source #

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.

Constructors

UnexpectedNullCellError

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.

DeserializationCellError

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

Fields

  • Text

    Human-readable error message describing what went wrong.

Instances

Instances details
Show CellError Source # 
Instance details

Defined in Hasql.Engine.Errors

Eq CellError Source # 
Instance details

Defined in Hasql.Engine.Errors

IsError CellError Source # 
Instance details

Defined in Hasql.Errors

data ServerError Source #

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

Constructors

ServerError 

Fields

  • Text

    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.

  • (Maybe 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 Int)

    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.

Instances

Instances details
Show ServerError Source # 
Instance details

Defined in Hasql.Engine.Errors

Eq ServerError Source # 
Instance details

Defined in Hasql.Engine.Errors

IsError ServerError Source # 
Instance details

Defined in Hasql.Errors