<s> from __future__ import unicode_literals <EOL> import httplib <EOL> import wac <EOL> class BalancedError ( Exception ) : <EOL> def __str__ ( self ) : <EOL> attrs = '<STR_LIT:U+002CU+0020>' . join ( [ <EOL> '<STR_LIT>' . format ( k , repr ( v ) ) <EOL> for k , v in self . __dict__ . iteritems ( ) <EOL> ] ) <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , attrs ) <EOL> class ResourceError ( BalancedError ) : <EOL> pass <EOL> class NoResultFound ( BalancedError ) : <EOL> pass <EOL> class MultipleResultsFound ( BalancedError ) : <EOL> pass <EOL> class FundingSourceNotCreditable ( Exception ) : <EOL> pass <EOL> def convert_error ( ex ) : <EOL> if not hasattr ( ex . response , '<STR_LIT:data>' ) : <EOL> return ex <EOL> return HTTPError . from_response ( ** ex . response . data ) ( ex ) <EOL> class HTTPError ( BalancedError , wac . Error ) : <EOL> class __metaclass__ ( type ) : <EOL> def __new__ ( meta_cls , name , bases , dikt ) : <EOL> cls = type . __new__ ( meta_cls , name , bases , dikt ) <EOL> cls . types = [ <EOL> getattr ( cls , k ) <EOL> for k in dir ( cls ) <EOL> if k . isupper ( ) and isinstance ( getattr ( cls , k ) , basestring ) <EOL> ] <EOL> cls . type_to_error . update ( zip ( cls . types , [ cls ] * len ( cls . types ) ) ) <EOL> return cls <EOL> def __init__ ( self , requests_ex ) : <EOL> super ( wac . Error , self ) . __init__ ( requests_ex ) <EOL> self . status_code = requests_ex . response . status_code <EOL> data = getattr ( requests_ex . response , '<STR_LIT:data>' , { } ) <EOL> for k , v in data . get ( '<STR_LIT>' , [ { } ] ) [ <NUM_LIT:0> ] . iteritems ( ) : <EOL> setattr ( self , k , v ) <EOL> @ classmethod <EOL> def format_message ( cls , requests_ex ) : <EOL> data = getattr ( requests_ex . response , '<STR_LIT:data>' , { } ) <EOL> status = httplib . responses [ requests_ex . response . status_code ] <EOL> error = data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> status = error . pop ( '<STR_LIT:status>' , status ) <EOL> status_code = error . pop ( '<STR_LIT>' , <EOL> requests_ex . response . status_code ) <EOL> desc = error . pop ( '<STR_LIT:description>' , None ) <EOL> message = '<STR_LIT>' . join ( str ( v ) for v in [ status , status_code , desc ] if v ) <EOL> return message <EOL> @ classmethod <EOL> def from_response ( cls , ** data ) : <EOL> try : <EOL> err = data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> exc = cls . type_to_error . get ( err [ '<STR_LIT>' ] , HTTPError ) <EOL> except : <EOL> exc = HTTPError <EOL> return exc <EOL> type_to_error = { } <EOL> class FundingInstrumentVerificationFailure ( HTTPError ) : <EOL> pass <EOL> class BankAccountVerificationFailure ( FundingInstrumentVerificationFailure ) : <EOL> AUTH_NOT_PENDING = '<STR_LIT>' <EOL> AUTH_FAILED = '<STR_LIT>' <EOL> AUTH_DUPLICATED = '<STR_LIT>' </s>
