Skip to content

Commit

Permalink
Adds __str__ to Suberror
Browse files Browse the repository at this point in the history
Resolves #217
  • Loading branch information
bhelx committed Nov 28, 2017
1 parent 0fea3d7 commit a4108d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions recurly/errors.py
Expand Up @@ -217,6 +217,9 @@ def __init__(self, field, symbol, message):
self.symbol = symbol
self.message = message

def __str__(self):
return self.__unicode__()

def __unicode__(self):
return six.u('%s: %s %s') % (self.symbol, self.field, self.message)

Expand Down Expand Up @@ -254,9 +257,15 @@ def errors(self):
return suberrors

def __unicode__(self):
return six.u('; ').join(six.text_type(error)
for error in six.itervalues(self.errors))

all_error_strings = []
all_error_keys = sorted(self.errors.keys())
for error_key in all_error_keys:
error = self.errors[error_key]
if isinstance(error, (tuple, list)):
all_error_strings += [six.text_type(e) for e in error] # multiple errors on field
else:
all_error_strings.append(six.text_type(error))
return six.u('; ').join(all_error_strings)

class ServerError(ResponseError):
"""An error resulting from a problem creating the server's response
Expand Down
2 changes: 1 addition & 1 deletion tests/test_resources.py
Expand Up @@ -936,7 +936,7 @@ def test_subscribe_multiple_errors(self):
# than one error on a field
sub_errs = err.errors['subscription.account.account_code']
self.assertEqual(len(sub_errs), 2)
self.assertEqual(type(sub_errs[1]), recurly.errors.ValidationError.Suberror)
self.assertEqual(str(err), "blank: subscription.account.account_code can't be blank; invalid: subscription.account.account_code is invalid; empty: subscription.account.billing_info.address1 can't be empty; empty: subscription.account.billing_info.city can't be empty; empty: subscription.account.billing_info.country can't be empty; blank: subscription.account.billing_info.first_name can't be blank; blank: subscription.account.billing_info.last_name can't be blank; required: subscription.account.billing_info.number is required; empty: subscription.account.billing_info.zip can't be empty; invalid: subscription.plan_code is invalid; not_a_number: subscription.unit_amount_in_cents is not a number")
except e:
self.fail("Failed subscription did not raise a Validation error")

Expand Down

0 comments on commit a4108d5

Please sign in to comment.