Skip to content

Commit

Permalink
Provide better error checking for connection and authentication issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomir committed Nov 22, 2015
1 parent 898d457 commit 0c60046
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
7 changes: 6 additions & 1 deletion gmailfilter/_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ def run_new_filter():
print(e)
sys.exit(2)

connection = IMAPConnection(s)
try:
connection = IMAPConnection(s)
except RuntimeError as e:
print("Error: %s" % e)
sys.exit(3)

rule_processor = _rules.SimpleRuleProcessor(
rules,
connection.get_connection_proxy()
Expand Down
37 changes: 27 additions & 10 deletions gmailfilter/_connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contextlib import contextmanager
import configparser
import functools
import imaplib
import logging
import os
import os.path
Expand Down Expand Up @@ -74,18 +75,34 @@ def get_message_part(self, part_name):

class IMAPConnection(object):

"""A low-level connection to an imap server. """

def __init__(self, server_info):
self._client = IMAPClient(
host=server_info.host,
port=server_info.port,
use_uid=False,
ssl=server_info.use_ssl
)
"""Create an IMAPConnection object.
This method connects to the server, and attempts to log in.
:raises RuntimeError: If the connection or login steps could not be
completed.
"""
try:
self._client = IMAPClient(
host=server_info.host,
port=server_info.port,
use_uid=False,
ssl=server_info.use_ssl
)
except imaplib.IMAP4.error as e:
raise RuntimeError("Failed to connect: %s" % e)
# self._client.debug = True
self._client.login(
server_info.username,
server_info.password,
)
try:
self._client.login(
server_info.username,
server_info.password,
)
except imaplib.IMAP4.error as e:
raise RuntimeError("Failed to authenticate: %s" % e)

def get_messages(self):
"""A generator that yields Message instances, one for every message
Expand Down

0 comments on commit 0c60046

Please sign in to comment.