fix PHP-7.3 compat
[mdref/mdref] / mdref / Exception.php
index 013d86106597524760c949ec2c72ed6fff305ee1..89dea42453bd1de7677670345b796f2aab7ac481 100644 (file)
@@ -2,17 +2,36 @@
 
 namespace mdref;
 
-use http\Response;
+use http\Env\Response;
+use http\Message\Body;
 
 class Exception extends \Exception
 {
-       function __construct($code, $message) {
+       /**
+        * Exception constructor with reversed arguments
+        *
+        * @param int $code HTTP code
+        * @param string $message reason message
+        */
+       function __construct(int $code, ?string $message = null) {
                parent::__construct($message, $code);
        }
 
-       function send(Response $res) {
+       /**
+        * Construct an Exception from error_get_last()'s message and code 500
+        * @return Exception
+        */
+       static function fromLastError() : Exception {
+               return new static(500, error_get_last()["message"]);
+       }
+
+       /**
+        * Send the error response
+        * @param Response $res
+        */
+       function send(Response $res) : void {
                $res->setResponseCode($this->code);
-               $res->setBody(new http\Message\Body);
+               $res->setBody(new Body);
                $res->getBody()->append($this->message);
        }
 }