Patch by Piers Lauder: make exceptions classes.

Take opportunity to add more explanatory messages to exceptions.
This commit is contained in:
Guido van Rossum 1999-01-15 03:23:55 +00:00
parent 1d7b0fa9ca
commit 40233ea70a
1 changed files with 13 additions and 14 deletions

View File

@ -37,10 +37,6 @@
>>> s.getreply() >>> s.getreply()
(250, "Somebody OverHere <somebody@here.my.org>") (250, "Somebody OverHere <somebody@here.my.org>")
>>> s.quit() >>> s.quit()
Bugs/TODO:
- Exceptions should be classes
''' '''
import socket import socket
@ -53,10 +49,11 @@
CRLF="\r\n" CRLF="\r\n"
# used for exceptions # used for exceptions
SMTPServerDisconnected="Server not connected" class SMTPException(Exception): pass
SMTPSenderRefused="Sender address refused" class SMTPServerDisconnected(SMTPException): pass
SMTPRecipientsRefused="All Recipients refused" class SMTPSenderRefused(SMTPException): pass
SMTPDataError="Error transmitting message data" class SMTPRecipientsRefused(SMTPException): pass
class SMTPDataError(SMTPException): pass
def quoteaddr(addr): def quoteaddr(addr):
"""Quote a subset of the email addresses defined by RFC 821. """Quote a subset of the email addresses defined by RFC 821.
@ -171,9 +168,9 @@ def send(self, str):
try: try:
self.sock.send(str) self.sock.send(str)
except socket.error: except socket.error:
raise SMTPServerDisconnected raise SMTPServerDisconnected('Server not connected')
else: else:
raise SMTPServerDisconnected raise SMTPServerDisconnected('please run connect() first')
def putcmd(self, cmd, args=""): def putcmd(self, cmd, args=""):
"""Send a command to the server.""" """Send a command to the server."""
@ -245,7 +242,7 @@ def ehlo(self, name=''):
# MTA's will disconnect on an ehlo. Toss an exception if # MTA's will disconnect on an ehlo. Toss an exception if
# that happens -ddm # that happens -ddm
if code == -1 and len(msg) == 0: if code == -1 and len(msg) == 0:
raise SMTPServerDisconnected raise SMTPServerDisconnected("Server not connected")
self.ehlo_resp=msg self.ehlo_resp=msg
if code<>250: if code<>250:
return code return code
@ -388,7 +385,7 @@ def sendmail(self, from_addr, to_addrs, msg, mail_options=[],
(code,resp) = self.mail(from_addr, esmtp_opts) (code,resp) = self.mail(from_addr, esmtp_opts)
if code <> 250: if code <> 250:
self.rset() self.rset()
raise SMTPSenderRefused raise SMTPSenderRefused('%s: %s' % (from_addr, resp))
senderrs={} senderrs={}
if type(to_addrs) == types.StringType: if type(to_addrs) == types.StringType:
to_addrs = [to_addrs] to_addrs = [to_addrs]
@ -399,11 +396,13 @@ def sendmail(self, from_addr, to_addrs, msg, mail_options=[],
if len(senderrs)==len(to_addrs): if len(senderrs)==len(to_addrs):
# the server refused all our recipients # the server refused all our recipients
self.rset() self.rset()
raise SMTPRecipientsRefused raise SMTPRecipientsRefused(string.join(
map(lambda x:"%s: %s" % (x[0], x[1][1]), senderrs.items()),
'; '))
code=self.data(msg) code=self.data(msg)
if code <>250 : if code <>250 :
self.rset() self.rset()
raise SMTPDataError raise SMTPDataError('data transmission error: %s' % code)
#if we got here then somebody got our mail #if we got here then somebody got our mail
return senderrs return senderrs