Created
November 19, 2012 19:19
-
-
Save mbeale/4113026 to your computer and use it in GitHub Desktop.
Recurly Error Handling PHP Best practices
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
try{ | |
$subscription = new Recurly_Subscription(); | |
$account = Recurly_Account::get('1'); //this will throw a not found exception if the account_code does not exist | |
$subscription->account = $account; | |
$subscription->create(); //this will throw a validation error exception as there are missing fields | |
} | |
catch (Exception $e) { | |
//for a list of possible exception types go to | |
//https://github.com/recurly/recurly-client-php/blob/master/lib/recurly/errors.php | |
//you could use send these messages to a log for later analysis | |
switch(get_class($e)){ | |
case 'Recurly_NotFoundError': | |
print 'Record could not be found'; | |
case 'Recurly_ValidationError': | |
$messages = explode(',',$e->getMessage()); //if there are multiple errors, they are comma delimited | |
foreach($messages as $message){ | |
print $message . "\n"; | |
} | |
break; | |
case 'Recurly_ServerError': | |
print 'Problem communicating with Recurly'; | |
default: | |
print get_class($e) . ': ' . $e->getMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should we also show how to handle the
Recurly_NotFoundError
error? Maybe just add it to the top of the switch statement?