PHP8
[m6w6/seekat] / lib / Exception / functions.php
index 08c8ddbca656d3cfc88d8f9065b9f6b6223a9fd6..0c7171a0176886845395e0c8056256317e8af36d 100644 (file)
@@ -2,17 +2,31 @@
 
 namespace seekat\Exception;
 
+/**
+ * @param string|\Throwable $message
+ * @return \Throwable
+ */
+function exception(&$message) : \Throwable {
+       if ($message instanceof \Throwable){
+               $exception = $message;
+               $message = $exception->getMessage();
+       } else {
+               $exception = new \Exception($message);
+       }
+       return $exception;
+}
+
 /**
  * Canonical error message from a string or Exception
- * @param string|Exception $error
+ * @param string|\Throwable $error
  * @return string
  */
-function message(&$error) : string {
+function message(&$error) : ?string {
        if ($error instanceof \Throwable) {
                $message = $error->getMessage();
        } else {
                $message = $error;
-               $error = new \Exception($error);
+               $error = new \Exception($message);
        }
        return $message;
 }