Skip to content
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

Catch Throwable in order to support both PHP 5 and PHP 7+ #82

Merged
merged 1 commit into from
Nov 23, 2023

Conversation

tfrommen
Copy link
Contributor

@tfrommen tfrommen commented Jun 2, 2023

This PR updates the three existing trycatch blocks to try and catch a Throwable first (which will work in PHP 7+) before the current code targeting an Exception is executed.

This works on PHP 5-8 (see here), and allows all the Error instances thrown by OneLogin to be caught on PHP 7 or 8 (e.g., #78).

@tfrommen
Copy link
Contributor Author

👋 Hi there!

I was just wondering if there was anything blocking this, other than limited resources, I guess...?

Being able to use this plugin on PHP 8+ without issues (related to the things addressed in this PR) sounds like a good thing, no? 🙂

Comment on lines +365 to 370
} catch ( \Throwable $e ) {
/* translators: %s = error message */
return new \WP_Error( 'invalid-saml', sprintf( esc_html__( 'Error: Could not parse the authentication response, please forward this error to your administrator: "%s"', 'wp-simple-saml' ), esc_html( $e->getMessage() ) ) );
} catch ( \Exception $e ) {
/* translators: %s = error message */
return new \WP_Error( 'invalid-saml', sprintf( esc_html__( 'Error: Could not parse the authentication response, please forward this error to your administrator: "%s"', 'wp-simple-saml' ), esc_html( $e->getMessage() ) ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tfrommen My understanding of this is that in order to be caught the exception must be of type throwable or it would not work, in which case this catch will be triggered for all exceptions 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for merging this, Tom!

Just to follow up on your question: The idea of this code is to catch any and all things PHP may throw, in any old or current version. Overall, we may be looking at \Exception instances, which is what everyone knows, but also \Error instances, or custom implementations of \Throwable. Before this PR, the plugin only caught \Exception, but that does not cover \Error instances, that newer version of OneLogin throw. On newer versions of PHP, both is covered by \Throwable. But older versions do not know about that, so we need to still have the \Exception catch in place.

Irrelevant of the PHP version, only one catch block will execute, both they are both doing the same, of course.

Hope that helps.

@tomjn tomjn merged commit 0fc8645 into humanmade:master Nov 23, 2023
@tfrommen tfrommen deleted the throwable branch November 23, 2023 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants