← All changes
|
src/Framework/Database/Exceptions/DatabaseQueryException.php
+53
-54
4.16.0
→
2.17.0
View file →
| @@ -1,10 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Give\Framework\Database\Exceptions; |
| 4 | 4 | |
| 5 | -use Give\Framework\Exceptions\Primitives\Exception; | |
| 6 | -use Throwable; | |
| 5 | +use Exception; | |
| 7 | 6 | |
| 8 | 7 | /** |
| 9 | 8 | * Class DatabaseQueryException |
| 10 | 9 | * |
| @@ -10,65 +9,65 @@ | ||
| 10 | 9 | * |
| 11 | 10 | * An exception for when errors occurred within the database while performing a query, which stores the SQL errors the |
| 12 | 11 | * database returned |
| 13 | 12 | * |
| 14 | - * @since 2.21.0 Use the GiveWP exception class | |
| 15 | 13 | * @since 2.9.2 |
| 16 | 14 | */ |
| 17 | -class DatabaseQueryException extends Exception | |
| 18 | -{ | |
| 19 | - /** | |
| 20 | - * @var string[] | |
| 21 | - */ | |
| 22 | - private $queryErrors; | |
| 15 | +class DatabaseQueryException extends Exception { | |
| 16 | + /** | |
| 17 | + * @var string[] | |
| 18 | + */ | |
| 19 | + private $queryErrors; | |
| 23 | 20 | |
| 24 | - /** | |
| 25 | - * @var string | |
| 26 | - */ | |
| 27 | - private $query; | |
| 21 | + /** | |
| 22 | + * Creates a new instance wih the query errors | |
| 23 | + * | |
| 24 | + * @since 2.9.2 | |
| 25 | + * | |
| 26 | + * @param string|string[] $queryErrors | |
| 27 | + * @param string|null $message | |
| 28 | + * | |
| 29 | + * @return DatabaseQueryException | |
| 30 | + */ | |
| 31 | + public static function create( $queryErrors, $message = null ) { | |
| 32 | + $error = new self(); | |
| 28 | 33 | |
| 29 | - /** | |
| 30 | - * @since 2.21.0 include query and query errors, and make auto-logging compatible | |
| 31 | - * @since 2.9.2 | |
| 32 | - */ | |
| 33 | - public function __construct( | |
| 34 | - string $query, | |
| 35 | - array $queryErrors, | |
| 36 | - string $message = 'Database Query', | |
| 37 | - $code = 0, | |
| 38 | - Throwable $previous = null | |
| 39 | - ) { | |
| 40 | - $this->query = $query; | |
| 41 | - $this->queryErrors = $queryErrors; | |
| 34 | + $error->message = $message ?: 'Query failed in database'; | |
| 35 | + $error->queryErrors = (array) $queryErrors; | |
| 42 | 36 | |
| 43 | - parent::__construct($message, $code, $previous); | |
| 44 | - } | |
| 37 | + return $error; | |
| 38 | + } | |
| 45 | 39 | |
| 46 | - /** | |
| 47 | - * Returns the query errors | |
| 48 | - * | |
| 49 | - * @since 2.9.2 | |
| 50 | - * | |
| 51 | - * @return string[] | |
| 52 | - */ | |
| 53 | - public function getQueryErrors(): array | |
| 54 | - { | |
| 55 | - return $this->queryErrors; | |
| 56 | - } | |
| 40 | + /** | |
| 41 | + * Returns the query errors | |
| 42 | + * | |
| 43 | + * @since 2.9.2 | |
| 44 | + * | |
| 45 | + * @return string[] | |
| 46 | + */ | |
| 47 | + public function getQueryErrors() { | |
| 48 | + return $this->queryErrors; | |
| 49 | + } | |
| 57 | 50 | |
| 58 | - public function getQuery(): string | |
| 59 | - { | |
| 60 | - return $this->query; | |
| 61 | - } | |
| 51 | + /** | |
| 52 | + * Returns a human readable form of the exception for logging | |
| 53 | + * | |
| 54 | + * @since 2.9.2 | |
| 55 | + * | |
| 56 | + * @return string | |
| 57 | + */ | |
| 58 | + public function getLogOutput() { | |
| 59 | + $queryErrors = array_map( | |
| 60 | + function ( $error ) { | |
| 61 | + return " - {$error}"; | |
| 62 | + }, | |
| 63 | + $this->queryErrors | |
| 64 | + ); | |
| 62 | 65 | |
| 63 | - /** | |
| 64 | - * @inheritDoc | |
| 65 | - */ | |
| 66 | - public function getLogContext(): array | |
| 67 | - { | |
| 68 | - return [ | |
| 69 | - 'category' => 'Uncaught database exception', | |
| 70 | - 'Query' => $this->query, | |
| 71 | - 'Query Errors' => $this->queryErrors, | |
| 72 | - ]; | |
| 73 | - } | |
| 66 | + return " | |
| 67 | + Code: {$this->getCode()}\n | |
| 68 | + Message: {$this->getMessage()}\n | |
| 69 | + DB Errors: \n | |
| 70 | + {$queryErrors} | |
| 71 | + "; | |
| 72 | + } | |
| 74 | 73 | } |