-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\Commons\Misc; | ||
|
||
|
||
use ErrorException; | ||
|
||
class ErrorHandlerHelper | ||
{ | ||
public static function registerDefaultErrorHandler() | ||
{ | ||
self::enableErrorReporting(E_ALL); | ||
self::hideDisplayErrors(); | ||
self::registerErrorToExceptionHandler(E_ALL); | ||
} | ||
|
||
public static function enableErrorReporting($errorTypes) | ||
{ | ||
ini_set('error_reporting', $errorTypes); | ||
} | ||
|
||
public static function hideDisplayErrors() | ||
{ | ||
ini_set('display_errors', "Off"); | ||
} | ||
|
||
public static function registerErrorToExceptionHandler($errorTypes = E_ALL) | ||
{ | ||
$handler = function ($errno, $errstr, $errfile, $errline) { | ||
throw new ErrorException($errstr, 0, $errno, $errfile, $errline); | ||
}; | ||
set_error_handler($handler, $errorTypes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\Misc; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class ErrorHandlerHelperTest extends TestCase | ||
{ | ||
|
||
public function testConvertingErrorToException() | ||
{ | ||
ErrorHandlerHelper::registerDefaultErrorHandler(); | ||
$this->expectException(\ErrorException::class); | ||
$this->expectExceptionMessage("Undefined variable: undefined"); | ||
echo $undefined; | ||
} | ||
} |