generator: use a proper destination
[mdref/mdref] / mdref / Exception.php
1 <?php
2
3 namespace mdref;
4
5 use http\Env\Response;
6 use http\Message\Body;
7
8 class Exception extends \Exception
9 {
10 /**
11 * Exception constructor with reversed arguments
12 *
13 * @param int $code HTTP code
14 * @param string $message reason message
15 */
16 function __construct(int $code, ?string $message = null) {
17 parent::__construct($message, $code);
18 }
19
20 /**
21 * Construct an Exception from error_get_last()'s message and code 500
22 * @return Exception
23 */
24 static function fromLastError() : Exception {
25 return new static(500, error_get_last()["message"]);
26 }
27
28 /**
29 * Send the error response
30 * @param Response $res
31 */
32 function send(Response $res) : void {
33 $res->setResponseCode($this->code);
34 $res->setBody(new Body);
35 $res->getBody()->append($this->message);
36 }
37 }