PageRenderTime 41ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext/async_mysql/ext_async_mysql_exceptions.php

https://gitlab.com/iranjith4/hhvm
PHP | 139 lines | 30 code | 9 blank | 100 comment | 1 complexity | f0bf8ce0c1a591b319bcf8996459cd69 MD5 | raw file
  1. <?hh
  2. /*
  3. +----------------------------------------------------------------------+
  4. | HipHop for PHP |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.01 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_01.txt |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. */
  17. /**
  18. * The base exception class for any issues arising from the use of async
  19. * MySQL.
  20. *
  21. * In general, when trying to connect to a MySQL client (e.g., via
  22. * `AsyncMysqlConnectionPool::connect()`) or when making a query (e.g., via
  23. * `AsyncMysqlConnection::queryf()`), it is good practice to have your code
  24. * exception catchable somewhere in the code pipeline (via
  25. * [try/catch](http://php.net/manual/en/language.exceptions.php)).
  26. *
  27. * e.g.,
  28. *
  29. * ```
  30. * try {
  31. * // code here
  32. * } catch (AsyncMysqlException $ex) {
  33. * $error = $ex->mysqlErrorString();
  34. * }
  35. * ```
  36. *
  37. * @guide /hack/async/intro/
  38. * @guide /hack/async/extensions/
  39. */
  40. class AsyncMysqlException extends Exception {
  41. private AsyncMysqlErrorResult $result;
  42. /**
  43. * Explicitly construct an `AsyncMysqlException`.
  44. *
  45. * Normally, you will `catch` an `AsyncMysqlException`, but if you want to
  46. * explictly construct one and, for example, `throw` it for some given reason,
  47. * then you pass it an `AsyncMysqlErrorResult`.
  48. *
  49. * @param $result - An `AsyncMysqlErrorResult` that contains the error
  50. * information for the failed operation.
  51. */
  52. public function __construct(AsyncMysqlErrorResult $result) {
  53. $this->result = $result;
  54. $this->message =
  55. 'Error executing AsyncMysql operation: '.$result->failureType();
  56. if ($this->mysqlErrorCode() > 0) {
  57. $this->message = $this->message.' (mysql error: '.$this->mysqlErrorCode().
  58. ': '.$this->mysqlErrorString().')';
  59. }
  60. }
  61. /**
  62. * Returns the MySQL error number for that caused the current exception.
  63. *
  64. * See MySQL's
  65. * [mysql_errno()](http://dev.mysql.com/doc/refman/5.0/en/mysql-errno.html)
  66. * for information on the error numbers.
  67. *
  68. * @return - The error number as an `int`.
  69. */
  70. public function mysqlErrorCode(): int {
  71. return $this->result->mysql_errno();
  72. }
  73. /**
  74. * Returns a human-readable string for the error encountered in the current
  75. * exception.
  76. *
  77. * @return - The error string.
  78. */
  79. public function mysqlErrorString(): string {
  80. return $this->result->mysql_error();
  81. }
  82. /**
  83. * Returns whether the type of failure that produced the exception was a
  84. * timeout.
  85. *
  86. * An `AsyncMysqlErrorResult` can occur due to `'TimedOut'`, representing a
  87. * timeout, or `'Failed'`, representing the server rejecting the connection
  88. * or query.
  89. *
  90. * @return - `true` if the type of failure was a time out; `false` otherwise.
  91. */
  92. public function timedOut(): bool {
  93. return $this->result->failureType() === "TimedOut";
  94. }
  95. /**
  96. * Returns whether the type of failure that produced the exception was a
  97. * general connection or query failure.
  98. *
  99. * An `AsyncMysqlErrorResult` can occur due to `'TimedOut'`, representing a
  100. * timeout, or `'Failed'`, representing the server rejecting the connection
  101. * or query.
  102. *
  103. * @return - `true` if the type of failure was a general failure; `false`
  104. * otherwise.
  105. */
  106. public function failed(): bool {
  107. return $this->result->failureType() === "Failed";
  108. }
  109. /**
  110. * Returns the underlying `AsyncMysqlErrorResult` associated with the current
  111. * exception.
  112. *
  113. * @return - The `AsyncMysqlErrorResult` that underlies the current exception.
  114. */
  115. public function getResult(): AsyncMysqlErrorResult {
  116. return $this->result;
  117. }
  118. }
  119. /**
  120. * The exception associated with a MySQL connection failure.
  121. *
  122. * All methods are the same as the base `AsyncMysqlException`.
  123. */
  124. class AsyncMysqlConnectException extends AsyncMysqlException { }
  125. /**
  126. * The exception associated with a MySQL query failure.
  127. *
  128. * All methods are the same as the base `AsyncMysqlException`.
  129. */
  130. class AsyncMysqlQueryException extends AsyncMysqlException { }