-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[ErrorHandler] Added call() method utility to turns any PHP error into \ErrorException #33155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or 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
056f51f
to
fa2c5ea
Compare
ismail1432
reviewed
Aug 14, 2019
fancyweb
reviewed
Aug 14, 2019
Nice! Thanx for working on this :) |
1619843
to
af717e6
Compare
ro0NL
reviewed
Aug 14, 2019
ro0NL
reviewed
Aug 14, 2019
…nto `\ErrorException`
af717e6
to
0faa855
Compare
@ro0NL comments addressed, thanks! |
nicolas-grekas
approved these changes
Aug 14, 2019
fabpot
approved these changes
Aug 18, 2019
Thank you @yceruto. |
fabpot
added a commit
that referenced
this pull request
Aug 18, 2019
…y PHP error into \ErrorException (yceruto) This PR was merged into the 4.4 branch. Discussion ---------- [ErrorHandler] Added call() method utility to turns any PHP error into \ErrorException | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #32936 | License | MIT | Doc PR | symfony/symfony-docs#... **Issue** There is no easy way to catch PHP warnings, though some progress has been made in this area for PHP 8.0 (https://wiki.php.net/rfc/consistent_type_errors). **Before** ```php $file = file_get_contents('unknown.txt'); // PHP Warning: file_get_contents(unknown.txt): failed to open stream: No such file or directory // workaround: $file = @file_get_contents('unknown.txt'); if (false === $file) { $e = error_get_last(); throw new \ErrorException($e['message'], 0, $e['type'], $e['file'], $e['line']); } ``` **After** ```php $file = ErrorHandler::call('file_get_contents', 'unknown.txt'); // or $file = ErrorHandler::call(static function () { return file_get_contents('unknown.txt'); }); // or (PHP 7.4) $file = ErrorHandler::call(fn () => file_get_contents('unknown.txt')); ``` All credits to @nicolas-grekas #32936 (comment) and @vudaltsov for the idea. Commits ------- 0faa855 Added ErrorHandler::call() method utility to turns any PHP warnings into `\ErrorException`
This was referenced Nov 12, 2019
Merged
Merged
PhilETaylor
pushed a commit
to PhilETaylor/symfony
that referenced
this pull request
Sep 6, 2023
…urns any PHP error into \ErrorException (yceruto) This PR was merged into the 4.4 branch. Discussion ---------- [ErrorHandler] Added call() method utility to turns any PHP error into \ErrorException | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#32936 | License | MIT | Doc PR | symfony/symfony-docs#... **Issue** There is no easy way to catch PHP warnings, though some progress has been made in this area for PHP 8.0 (https://wiki.php.net/rfc/consistent_type_errors). **Before** ```php $file = file_get_contents('unknown.txt'); // PHP Warning: file_get_contents(unknown.txt): failed to open stream: No such file or directory // workaround: $file = @file_get_contents('unknown.txt'); if (false === $file) { $e = error_get_last(); throw new \ErrorException($e['message'], 0, $e['type'], $e['file'], $e['line']); } ``` **After** ```php $file = ErrorHandler::call('file_get_contents', 'unknown.txt'); // or $file = ErrorHandler::call(static function () { return file_get_contents('unknown.txt'); }); // or (PHP 7.4) $file = ErrorHandler::call(fn () => file_get_contents('unknown.txt')); ``` All credits to @nicolas-grekas symfony#32936 (comment) and @vudaltsov for the idea. Commits ------- 0faa855 Added ErrorHandler::call() method utility to turns any PHP warnings into `\ErrorException`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
There is no easy way to catch PHP warnings, though some progress has been made in this area for PHP 8.0 (https://wiki.php.net/rfc/consistent_type_errors).
Before
After
All credits to @nicolas-grekas #32936 (comment) and @vudaltsov for the idea.