Skip to content

Commit 73947d5

Browse files
committed
Better error for non-existing user, closes #37
1 parent 9d61313 commit 73947d5

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

twitter_to_sqlite/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
source_re = re.compile('<a href="(?P<url>.*?)".*?>(?P<name>.*?)</a>')
3535

3636

37+
class UserDoesNotExist(click.ClickException):
38+
def __init__(self, identifier):
39+
super().__init__("User '{}' does not exist".format(identifier))
40+
41+
3742
def open_database(db_path):
3843
db = sqlite_utils.Database(db_path)
3944
# Only run migrations if this is an existing DB (has tables)
@@ -120,7 +125,10 @@ def get_profile(db, session, user_id=None, screen_name=None):
120125
url = "https://api.twitter.com/1.1/users/show.json"
121126
if args:
122127
url += "?" + urllib.parse.urlencode(args)
123-
profile = session.get(url).json()
128+
response = session.get(url)
129+
if response.status_code == 404:
130+
raise UserDoesNotExist(screen_name or user_id)
131+
profile = response.json()
124132
save_users(db, [profile])
125133
return profile
126134

0 commit comments

Comments
 (0)