AsyncMysqlException::failed

Returns whether the type of failure that produced the exception was a general connection or query failure

public function failed(): bool;

An AsyncMysqlErrorResult can occur due to 'TimedOut', representing a timeout, or 'Failed', representing the server rejecting the connection or query.

Returns

  • bool - true if the type of failure was a general failure; false otherwise.

Examples

The following example shows how to use AsyncMysqlException::failed to determine if the connection failed in some other way than a timeout.

<?hh // partial

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\Except\Failed;

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;
  $ret = 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
    $ret = "Connection Exception";
    \var_dump($ex->failed());
  } catch (\AsyncMysqlQueryException $ex) { // implicitly constructed
    $ret = "Query Exception";
  } catch (\AsyncMysqlException $ex) {
    $ret = null;
  } finally {
    if ($conn) {
      $conn->close();
    }
  }
  return $ret;
}

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

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