| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Exceptions; |
| 6 |
|
| 7 |
/** |
| 8 |
* Database Exception |
| 9 |
* |
| 10 |
* Thrown when database operations fail |
| 11 |
*/ |
| 12 |
class DatabaseException extends YatraException |
| 13 |
{ |
| 14 |
protected string $errorCode = 'database_error'; |
| 15 |
|
| 16 |
public function __construct(string $message = 'Database operation failed', string $query = '', ?\Exception $previous = null) |
| 17 |
{ |
| 18 |
$context = []; |
| 19 |
if (!empty($query)) { |
| 20 |
$context['query'] = $query; |
| 21 |
} |
| 22 |
|
| 23 |
parent::__construct($message, 500, $previous, $context); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Create from wpdb error |
| 28 |
*/ |
| 29 |
public static function fromWpdbError(string $query = '', string $error = ''): self |
| 30 |
{ |
| 31 |
$message = 'Database query failed'; |
| 32 |
if (!empty($error)) { |
| 33 |
$message .= ': ' . $error; |
| 34 |
} |
| 35 |
|
| 36 |
return new self($message, $query); |
| 37 |
} |
| 38 |
} |
| 39 |
|