Skip to content

exceptions

Alexey Borzov edited this page Jul 19, 2020 · 2 revisions

Exception classes

The package contains base exception interface \sad_spirit\pg_wrapper\Exception and several specialized exception classes that extend SPL Exception classes and implement this interface. Therefore all exceptions thrown in pg_wrapper can be caught with

try {
    // Do some database-related stuff
} catch (\sad_spirit\pg_wrapper\Exception $e) {
    // Handle exception
}

All exception classes belong to sad_spirit\pg_wrapper\exceptions namespace:

  • BadMethodCallException extends \BadMethodCallException - Namespaced version of SPL's BadMethodCallException
  • InvalidArgumentException extends \InvalidArgumentException - Namespaced version of SPL's InvalidArgumentException.
  • OutOfBoundsException extends \OutOfBoundsException - Namespaced version of SPL's OutOfBoundsException.
  • RuntimeException extends \RuntimeException - Namespaced version of SPL's RuntimeException.
    • ConnectionException extends RuntimeException - Thrown when database connection fails.
    • ServerException extends RuntimeException - Exception thrown when query fails. The class defines getSqlState() method returning SQLSTATE error code if one is available. It also contains class constants for all such codes so that ServerException::DIVISION_BY_ZERO can be checked rather than '22012'. Several specialized subclasses of ServerException belong to sad_spirit\pg_wrapper\exceptions\server namespace:
      • ConstraintViolationException - Thrown when database integrity constraint is violated. It defines an additional getConstraintName() method returning name of that constraint if one is available.
      • DataException - Thrown when there are problems with processed data, like division by zero or numeric value out of range.
      • FeatureNotSupportedException - Thrown when an attempt to use functionality not supported by Postgres is made.
      • InsufficientPrivilegeException - Thrown when an action fails due to insufficient permissions.
      • InternalErrorException - Thrown when a database encounters some internal error: e.g. transaction state is invalid.
      • OperationalException - Thrown for errors related to database's operation that are not necessarily under the control of programmer.
      • ProgrammingException - Thrown for programming errors: invalid SQL syntax, undefined or ambiguous objects, etc.
      • QueryCanceledException - Thrown when query is canceled, either due to statement_timeout setting or user request.
      • TransactionRollbackException - Thrown when transaction is rolled back due to deadlock or serialization failure.
  • TypeConversionException extends \DomainException - Exception thrown when conversion of value from/to database representation fails.

Generally, if query fails:

  • ConnectionException is thrown when connection to server failed.
  • Subclass of ServerException is thrown based on SQLSTATE error code when said code is available, generic ServerException otherwise.