diff --git a/src/LastErrorException/LastErrorException.php b/src/LastErrorException/LastErrorException.php new file mode 100644 index 0000000..03f17e7 --- /dev/null +++ b/src/LastErrorException/LastErrorException.php @@ -0,0 +1,44 @@ + 0, + 'message' => '', + 'file' => __FILE__, + 'line' => __LINE__, + ]; + } + parent::__construct($lastError['message'], $code, $lastError['type'], $lastError['file'], $lastError['line']); + } + + /** + * Create the exception if there is a last error + * + * @param bool $checkErrorReporting Check if last error type match {@see https://www.php.net/manual/function.error-reporting error_reporting()} + * + * @return static|null The exception or Null if no last error + */ + public static function createFromLastError (bool $checkErrorReporting = true): ?static { + if (($lastError = error_get_last()) === null) { + return null; + } + if ($checkErrorReporting && !($lastError['type'] & error_reporting())) { + return null; + } + return new static(); + } +} \ No newline at end of file diff --git a/tests/test.php b/tests/test.php new file mode 100644 index 0000000..98909ff --- /dev/null +++ b/tests/test.php @@ -0,0 +1,19 @@ +getMessage() + . PHP_EOL . 'in ' . $e->getFile() . '(' . $e->getLine() . ')' + . PHP_EOL . $e->getTraceAsString() + ); +} \ No newline at end of file