| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\Database\Exceptions; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class DatabaseQueryException |
| 9 |
* |
| 10 |
* An exception for when errors occurred within the database while performing a query, which stores the SQL errors the |
| 11 |
* database returned |
| 12 |
* |
| 13 |
* @since 2.9.2 |
| 14 |
*/ |
| 15 |
class DatabaseQueryException extends Exception { |
| 16 |
/** |
| 17 |
* @var string[] |
| 18 |
*/ |
| 19 |
private $queryErrors; |
| 20 |
|
| 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(); |
| 33 |
|
| 34 |
$error->message = $message ?: 'Query failed in database'; |
| 35 |
$error->queryErrors = (array) $queryErrors; |
| 36 |
|
| 37 |
return $error; |
| 38 |
} |
| 39 |
|
| 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 |
} |
| 50 |
|
| 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 |
); |
| 65 |
|
| 66 |
return " |
| 67 |
Code: {$this->getCode()}\n |
| 68 |
Message: {$this->getMessage()}\n |
| 69 |
DB Errors: \n |
| 70 |
{$queryErrors} |
| 71 |
"; |
| 72 |
} |
| 73 |
} |
| 74 |
|