Recurly

API Validation Errors

If the requested create, update, or delete cannot be performed due to validation errors, the server returns a 422 Unprocessable Entity response with an array of the validation errors:

Status: 422 Unprocessable Entity
Content-Type: application/xml; charset=utf-8
<?xml version="1.0" encoding="UTF-8"?>
<errors>
  <error field="model_name.field_name" symbol="not_a_number" lang="en-US">is not a number</error>
</errors>

Handling Errors with Client Libraries

When using the supported libraries for PHP, Ruby, and Python, Recurly recommends proper exception handling. Not handling exceptions properly could result in unexpected results or sudden termination of your code. Below is a sample example in PHP as how to properly catch exceptions thrown by the Recurly PHP library.

try{
        $subscription = new Recurly_Subscription();
        //this will throw a not found exception if the account_code does not exist
        $account = Recurly_Account::get('1'); 
        $subscription->account = $account;
        //this will throw a validation error exception as there are missing fields
        $subscription->create(); 
}
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':
        		//if there are multiple errors, they are comma delimited
                $messages = explode(',',$e->getMessage()); 
                foreach($messages as $message){
                        print $message . "\n";
                }
                break;
        case 'Recurly_ServerError':
                print 'Problem communicating with Recurly';
        default:
                print get_class($e) . ': ' . $e->getMessage();
        }
}
$transaction->create();