AsyncMysqlException::__construct

Explicitly construct an AsyncMysqlException

public function __construct(
  AsyncMysqlErrorResult $result,
);

Normally, you will catch an AsyncMysqlException, but if you want to explictly construct one and, for example, throw it for some given reason, then you pass it an AsyncMysqlErrorResult.

Parameters

  • AsyncMysqlErrorResult $result - An AsyncMysqlErrorResult that contains the error information for the failed operation.

Examples

The following example shows how to catch an AsyncMysqlException. Normally you would construct one implicitly via a try/catch block, like we did in this example.

The two current subclasses of AsyncMysqlException are AsyncMysqlConnectionException for connection problems and AsyncMysqlQueryException for querying issues.

Note that you can explicitly construct one by creating an object like new AsyncMysqlException(AsyncMysqlErrorResult $result). But this is not normally done.

require __DIR__.'/../../__includes/async_mysql_connect.inc.php';

use \Hack\UserDocumentation\API\Examples\AsyncMysql\ConnectionInfo as CI;

async function connect(
  \AsyncMysqlConnectionPool $pool,
): Awaitable<\AsyncMysqlConnection> {
  return await $pool->connect(
    CI::$host,
    CI::$port,
    CI::$db,
    CI::$user,
    "thisIsNotThePassword",
  );
}
async function simple_query(): Awaitable<?string> {
  $pool = new \AsyncMysqlConnectionPool(array());
  $conn = null;
  try {
    $conn = await connect($pool);
    $result = await $conn->query(
      'SELECT name FROM test_table WHERE userID = 1',
    );
    $conn->close();
    return $result->vectorRows()[0][0];
  } catch (\AsyncMysqlConnectException $ex) { // implicitly constructed
    return "Connection Exception";
  } catch (\AsyncMysqlQueryException $ex) { // implicitly constructed
    return "Query Exception";
  } catch (\AsyncMysqlException $ex) {
    return null;
  } finally {
    if ($conn) {
      $conn->close();
    }
  }
}

function run(): void {
  $r = \HH\Asio\join(simple_query());
  \var_dump($r);
}

run();
Output
string(20) "Connection Exception"