-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #717 from mlocati/centralize-error-handling
Centralize error handling functions, fix #715
- Loading branch information
Showing
7 changed files
with
119 additions
and
67 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
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
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
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
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
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
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,93 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Imagine package. | ||
* | ||
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Imagine\Utils; | ||
|
||
use ErrorException; | ||
use Exception; | ||
use Imagine\Exception\RuntimeException; | ||
use Throwable; | ||
|
||
class ErrorHandling | ||
{ | ||
/** | ||
* Call a callback ignoring $flags warnings. | ||
* | ||
* @param int $flags The flags to be ignored (eg E_WARNING | E_NOTICE) | ||
* @param callable $callback The callable to be called | ||
* | ||
* @throws \Exception Throws an Exception if $callback throws an Exception | ||
* @throws \Throwable Throws an Throwable if $callback throws an Throwable | ||
* | ||
* @return mixed Returns the result of $callback | ||
*/ | ||
public static function ignoring($flags, $callback) | ||
{ | ||
set_error_handler( | ||
function () { | ||
}, | ||
$flags | ||
); | ||
try { | ||
$result = $callback(); | ||
$exception = null; | ||
} catch (Exception $x) { | ||
$exception = $x; | ||
} catch (Throwable $x) { | ||
$exception = $x; | ||
} | ||
restore_error_handler(); | ||
if ($exception !== null) { | ||
throw $exception; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Call a callback and throws a RuntimeException if a $flags warning is thrown. | ||
* | ||
* @param int $flags The flags to be intercepted (eg E_WARNING | E_NOTICE) | ||
* @param callable $callback The callable to be called | ||
* | ||
* @throws RuntimeException | ||
* @throws \Imagine\Exception\RuntimeException | ||
* @throws \Exception | ||
* @throws \Throwable | ||
* | ||
* @return mixed Returns the result of $callback | ||
*/ | ||
public static function throwingRuntimeException($flags, $callback) | ||
{ | ||
set_error_handler( | ||
function ($errno, $errstr, $errfile, $errline) { | ||
if (error_reporting() !== 0) { | ||
throw new RuntimeException($errstr, $errno, new ErrorException($errstr, 0, $errno, $errfile, $errline)); | ||
} | ||
}, | ||
$flags | ||
); | ||
try { | ||
$result = $callback(); | ||
$exception = null; | ||
} catch (Exception $x) { | ||
$exception = $x; | ||
} catch (Throwable $x) { | ||
$exception = $x; | ||
} | ||
restore_error_handler(); | ||
if ($exception !== null) { | ||
throw $exception; | ||
} | ||
|
||
return $result; | ||
} | ||
} |