Skip to content

Instantly share code, notes, and snippets.

@mbeale
Created November 19, 2012 19:19
Show Gist options
  • Save mbeale/4113026 to your computer and use it in GitHub Desktop.
Save mbeale/4113026 to your computer and use it in GitHub Desktop.
Recurly Error Handling PHP Best practices
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();
}
}
@kainosnoema
Copy link

Should we also show how to handle the Recurly_NotFoundError error? Maybe just add it to the top of the switch statement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment