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

Exception code should be type of int #547

Closed
ankurk91 opened this issue Nov 9, 2018 · 3 comments · Fixed by #548
Closed

Exception code should be type of int #547

ankurk91 opened this issue Nov 9, 2018 · 3 comments · Fixed by #548

Comments

@ankurk91
Copy link
Contributor

ankurk91 commented Nov 9, 2018

Hi,

  • Stripe-php : v6.21.0
  • php : v7.1.21

The base exception class is setting error code see

$this->code = $code;

but the error codes can only be type of integer.

// print error code before and after
parent::__construct($description, $httpStatus, $httpBody, $jsonBody, $httpHeaders);
//   die($this->getCode()); // 0
$this->code = $code;
//   die($this->getCode()); // "invalid_grant"

When i am trying to consume this exception in my application like.

// Some controller method
try {
            $response = \Stripe\OAuth::token([
                'grant_type' => 'authorization_code',
                'code' => $_GET['code]
            ]);
        } catch (\Throwable $e) {
            throw new OAuthException($e);
        }
// Custom exception class
class OAuthException extends \Exception
{
     public function __construct(Throwable $exception)
    {
        // need to force convert error code to int
        parent::__construct($exception->getMessage(), (int)$exception->getCode(), $exception);
    }
}

According to php docs

Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException).

A possible solution would be -

 $this->errorCode = $code

Then base class method getErrorCode need to return $this->errorCode value instead.

Thanks for reading.

@ob-stripe
Copy link
Contributor

Hi @ankurk91, thanks for the report. The PHP documentation you quoted explicitly says that the exception code may not always be an integer, but I don't see an issue with implementing your suggestion.

@ankurk91
Copy link
Contributor Author

ankurk91 commented Nov 9, 2018

Thanks, I can submit a PR for the same tomorrow.

@ankurk91
Copy link
Contributor Author

ankurk91 commented Nov 10, 2018

@ob-stripe
Sent a PR, Please have a look

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 a pull request may close this issue.

2 participants