Last active
July 17, 2019 17:12
-
-
Save dameety/ac1ba0c60d437279f8deb2189d023660 to your computer and use it in GitHub Desktop.
The code for this post: https://medium.com/damilola-jolayemi/when-writing-code-for-handling-exceptions-do-you-repeat-yourself-2dc62078500f
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
<?php | |
class Handler | |
{ | |
protected $exception; | |
public function __construct($exception) | |
{ | |
$this->exception = $exception; | |
} | |
protected $expectedExceptions = [ | |
\Stripe\Error\Card::class, | |
\Stripe\Error\RateLimit::class, | |
\Stripe\Error\InvalidRequest::class, | |
\Stripe\Error\Authentication::class, | |
\Stripe\Error\ApiConnection::class, | |
\Stripe\Error\Base::class | |
]; | |
public function unexpected() | |
{ | |
$exceptionClass = get_class($this->exception); | |
if ( ! in_array($exceptionClass, $this->expectedExceptions) ) { | |
//something unrelated to stripe happened. | |
//handle it here | |
} | |
} | |
public function respond() | |
{ | |
$this->unexpected(); | |
if($this->exception instanceof \Stripe\Error\Card ) { | |
//respond accordingly | |
} elseif( $this->exception instanceof \Stripe\Error\RateLimit ) { | |
//respond accordingly | |
} elseif( $this->exception instanceof \Stripe\Error\InvalidRequestt ) { | |
//respond accordingly | |
dd('getting this now'); | |
} elseif( $this->exception instanceof \Stripe\Error\Authentication ) { | |
//respond accordingly | |
dd('getting auth key issue now'); | |
} elseif( $this->exception instanceof \Stripe\Error\ApiConnection ) { | |
//respond accordingly | |
} elseif( $this->exception instanceof \Stripe\Error\Base ) { | |
//respond accordingly | |
} | |
} | |
} |
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
<?php | |
class Handler2 | |
{ | |
protected $exception; | |
protected $exceptionClass; | |
public function __construct($exception) | |
{ | |
$this->exception = $exception; | |
} | |
protected $expectedExceptions = [ | |
\Stripe\Error\Card::class, | |
\Stripe\Error\RateLimit::class, | |
\Stripe\Error\InvalidRequest::class, | |
\Stripe\Error\Authentication::class, | |
\Stripe\Error\ApiConnection::class, | |
\Stripe\Error\Base::class | |
]; | |
public function respond() | |
{ | |
$this->unexpected(); | |
$expectedExceptionMethodName = lcfirst ( | |
preg_replace("/[^a-zA-Z]/", "", $this->exceptionClass) | |
); | |
$this->$expectedExceptionMethodName(); | |
} | |
public function unexpected() | |
{ | |
$this->exceptionClass = get_class($this->exception); | |
if ( ! in_array($this->exceptionClass, $this->expectedExceptions) ) { | |
//something unrelated to stripe happened | |
//handle it here | |
} | |
} | |
public function stripeErrorCard() | |
{ | |
//handle it | |
} | |
public function stripeErrorInvalidRequest() | |
{ | |
//handle it | |
} | |
public function stripeErrorAuthentication() | |
{ | |
//handle it | |
} | |
public function stripeErrorRateLimit() | |
{ | |
//handle it | |
} | |
public function stripeErrorApiConnection() | |
{ | |
//handle it | |
} | |
public function stripeErrorBase() | |
{ | |
//handle it | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment