Skip to content
This repository has been archived by the owner on Dec 25, 2020. It is now read-only.

Error Handling

Kerem Güneş edited this page Jan 11, 2017 · 9 revisions

Oppa tries to keep it secure the development process throwing each error and gives options to handle errors on production. So, it is recommended to do everything in try/catch blocks but you can also define your exception handler and catch any thrown error.

try {
    // if things go wrong
} catch (\Throwable $e) {
    print $e->getMessage();
}

You can also define your query error handler in configuration that --used for only query errors-- instead of throwing them instantly if any fail occurs.

$cfg.query_error_handler => function($e, $query, $queryParams) {
    print $e->getMessage();
    // throw $e;
}

**List of Exceptions

Oppa\Exception\Error
Oppa\Exception\SqlException // (contains getSqlState() method)
   Oppa\Exception\ConnectionException
   Oppa\Exception\QueryException
Oppa\Exception\InvalidConfigException
Oppa\Exception\InvalidKeyException
Oppa\Exception\InvalidValueException
Oppa\Exception\InvalidQueryException
Oppa\Exception\InvalidResourceException

// catch en error while querying mysql
try {
    $agent->query("select * from noneexists");
} catch (Oppa\Exception\QueryException $e) {
    print $e->getMessage();
    if ($e->getCode() == Oppa\SqlState\MysqlServerError::NO_SUCH_TABLE) {
        print 'Nö table!';
    }
    if ($e->getSqlState() == Oppa\SqlState\Mysql::NO_SUCH_TABLE) {
        print 'Nö table!';
    }
}

**List of SQL States

Oppa\SqlState\SqlState         // Oppa specific states
Oppa\SqlState\Mysql            // MySQL states
Oppa\SqlState\Pgsql            // PostgreSQL states
Oppa\SqlState\MysqlServerError // MySQL server error codes
Oppa\SqlState\MysqlClientError // MySQL client error codes

**SQL States Notes

  • For Oppa\Exception\ConnectionException's use Oppa\SqlState\SqlState constants.
  • For MySQL: $e->getCode() == Oppa\SqlState\Mysql...Error::xxx more reliable than $e->getSqlState() == xxx.
  • For PostgreSQL: $e->getCode() doesn't work 'bcos it has no error codes, use $e->getSqlState() == Oppa\SqlState\Pgsql::xxx instead.
Clone this wiki locally