oauth2-client icon indicating copy to clipboard operation
oauth2-client copied to clipboard

AbstractProvider - unexpected, undocumented exceptions

Open lukaszmakuch opened this issue 4 years ago • 3 comments

Hello! 👋

First of all, thank you for this project. It has saved me tons of time! 👍

I'm opening this issue to discuss the exceptions thrown by the AbstractProvider class.

When working with a provider, like the GenericProvider, I'd expect to catch some IdentityProviderExceptions. Its nicely documented:

* Exception thrown if the provider response contains errors.

However, much to my surprise, some methods such as getAccessToken or getResourceOwner may throw an UnexpectedValueException when there's something wrong with the response received from the Identity Provider. Sadly, this is not documented.

To be honest, I expected to catch nothing but IdentityProviderExceptions and my code broke because there was no catch block for UnexpectedValueExceptions.

What are your thoughts on this matter? 🤔 Should the fact that an UnexpectedValueException may be thrown be documented? Or maybe we can risk some breaking changes and modify the code to throw IdentityProviderExceptions instead of UnexpectedValueExceptions?

Cheers!

lukaszmakuch avatar Mar 19 '21 08:03 lukaszmakuch

You can probably adjust your code to catch the different exceptions?


try {
    $accessToken = $provider->getAccessToken($grant, array $options = [])
} catch (IdentityProviderException $e) {
    // handle IdentityProviderException exceptions here
} catch (UnexpectedValueException $e) {
    // handle UnexpectedValueException  exceptions here
}

OR


try {
    $accessToken = $provider->getAccessToken($grant, array $options = [])
} catch (IdentityProviderException | UnexpectedValueException $e) {
    // handle IdentityProviderException and UnexpectedValueException exceptions here
} 

See: https://www.php.net/manual/en/language.exceptions.php#language.exceptions.catch

cloudcogsio avatar Aug 03 '21 07:08 cloudcogsio

Thank you for your reply.

You’re right and it’s pretty straightforward to catch all the exceptions once you know they may be thrown.

What I’d like to see improved is the clarity of the documentation (or the documented code). I believe it’d be great if developers could write all the necessary catch blocks based solely on the documentation, without the need of debugging their code, analyzing logs etc.

On 3. Aug 2021, at 09:13, cloudcogsio @.***> wrote:

 You can probably adjust your code to catch the different exceptions?

try { $accessToken = $provider->getAccessToken($grant, array $options = []) } catch (IdentityProviderException $e) { // handle IdentityProviderException exceptions here } catch (UnexpectedValueException $e) { // handle UnexpectedValueException exceptions here } OR

try { $accessToken = $provider->getAccessToken($grant, array $options = []) } catch (IdentityProviderException | UnexpectedValueException $e) { // handle IdentityProviderException and UnexpectedValueException exceptions here } See: https://www.php.net/manual/en/language.exceptions.php#language.exceptions.catch

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

lukaszmakuch avatar Aug 03 '21 11:08 lukaszmakuch

Thanks for the suggestion, @lukaszmakuch. We'll keep this open until we've added appropriate @throws tags to all the docblocks. I hope that will help.

ramsey avatar Dec 22 '21 16:12 ramsey