Introduce a "DatabaseException" class for fatal exceptions (used in testmode to throw...
[friendica.git/.git] / src / Database / DatabaseException.php
1 <?php
2 declare(strict_types=1);
3
4 namespace Friendica\Database;
5
6 use Exception;
7 use Throwable;
8
9 /**
10  * A database fatal exception, which shouldn't occur
11  */
12 class DatabaseException extends Exception
13 {
14         protected $query;
15
16         /**
17          * Construct the exception. Note: The message is NOT binary safe.
18          *
19          * @link https://php.net/manual/en/exception.construct.php
20          *
21          * @param string    $message  The Database error message.
22          * @param int       $code     The Database error code.
23          * @param string    $query    The Database error query.
24          * @param Throwable $previous [optional] The previous throwable used for the exception chaining.
25          */
26         public function __construct(string $message, int $code, string $query, Throwable $previous = null)
27         {
28                 parent::__construct($message, $code, $previous);
29                 $this->query = $query;
30         }
31
32         /**
33          * {@inheritDoc}
34          */
35         public function __toString()
36         {
37                 return sprintf('Database error %d "%s" at "%s"', $this->message, $this->code, $this->query);
38         }
39 }