S3/Exceptions.py
6bb385f2
 ## Amazon S3 manager - Exceptions library
 ## Author: Michal Ludvig <michal@logix.cz>
 ##         http://www.logix.cz/michal
 ## License: GPL Version 2
 
 from logging import debug, info, warning, error
 
 try:
 	import xml.etree.ElementTree as ET
 except ImportError:
 	import elementtree.ElementTree as ET
 
fa0dfb92
 class S3Exception(Exception):
 	def __str__(self):
 		## Is this legal?
 		return unicode(self)
 
 	def __unicode__(self):
 		return self.message
 
 class S3Error (S3Exception):
6bb385f2
 	def __init__(self, response):
 		self.status = response["status"]
 		self.reason = response["reason"]
 		self.info = {}
 		debug("S3Error: %s (%s)" % (self.status, self.reason))
 		if response.has_key("headers"):
 			for header in response["headers"]:
 				debug("HttpHeader: %s: %s" % (header, response["headers"][header]))
 		if response.has_key("data"):
 			tree = ET.fromstring(response["data"])
 			for child in tree.getchildren():
 				if child.text != "":
 					debug("ErrorXML: " + child.tag + ": " + repr(child.text))
 					self.info[child.tag] = child.text
 
fa0dfb92
 	def __unicode__(self):
6bb385f2
 		retval = "%d (%s)" % (self.status, self.reason)
 		try:
 			retval += (": %s" % self.info["Code"])
 		except (AttributeError, KeyError):
 			pass
 		return retval
 
fa0dfb92
 class S3UploadError(S3Exception):
6bb385f2
 	pass
 
fa0dfb92
 class S3DownloadError(S3Exception):
ed27a45e
 	pass
 
fa0dfb92
 class ParameterError(S3Exception):
6bb385f2
 	pass