Skip to content

Commit a2db5a4

Browse files
committed
add error handling for commands
1 parent 6521258 commit a2db5a4

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

inoreader/main.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import threading
1111
from queue import Queue
1212
from uuid import uuid4
13-
from functools import partial
13+
from functools import partial, wraps
1414
from logging.config import dictConfig
1515
from collections import defaultdict, Counter
1616

@@ -23,7 +23,7 @@
2323
from inoreader import InoreaderClient
2424
from inoreader.filter import get_filter
2525
from inoreader.sim import sim_of, InvIndex
26-
from inoreader.exception import NotLoginError
26+
from inoreader.exception import NotLoginError, APIError
2727
from inoreader.config import InoreaderConfigManager
2828
from inoreader.consts import DEFAULT_APPID, DEFAULT_APPKEY
2929

@@ -80,6 +80,22 @@ def get_client():
8080
return client
8181

8282

83+
def catch_error(func):
84+
85+
@wraps(func)
86+
def wrapper(*args, **kwargs):
87+
try:
88+
return func(*args, **kwargs)
89+
except NotLoginError:
90+
print('Error: Please login first!')
91+
sys.exit(1)
92+
except APIError as exception:
93+
print("Error:", str(exception))
94+
sys.exit(1)
95+
96+
return wrapper
97+
98+
8399
@click.group(context_settings=dict(help_option_names=['-h', '--help']))
84100
def main():
85101
pass
@@ -150,6 +166,7 @@ def redirect():
150166

151167

152168
@main.command("list-folders")
169+
@catch_error
153170
def list_folders():
154171
"""List all folders"""
155172
client = get_client()
@@ -163,6 +180,7 @@ def list_folders():
163180

164181

165182
@main.command("list-tags")
183+
@catch_error
166184
def list_tags():
167185
"""List all tags"""
168186
client = get_client()
@@ -183,6 +201,7 @@ def list_tags():
183201
type=click.Choice(['json', 'csv', 'plain', 'markdown', 'org-mode']),
184202
default='json',
185203
help='Format of output file, default: json')
204+
@catch_error
186205
def fetch_unread(folder, tags, outfile, out_format):
187206
"""Fetch unread articles"""
188207
client = get_client()
@@ -253,6 +272,7 @@ def apply_action(articles, client, action, tags):
253272

254273
@main.command("filter")
255274
@click.option("-r", "--rules-file", required=True, help='YAML file with your rules')
275+
@catch_error
256276
def filter_articles(rules_file):
257277
"""Select articles and do something"""
258278
client = get_client()
@@ -329,6 +349,7 @@ def filter_articles(rules_file):
329349
@click.option("--out-format",
330350
type=click.Choice(["json", "csv"]), default="csv",
331351
help="Format of output, default: csv")
352+
@catch_error
332353
def get_subscriptions(outfile, folder, out_format):
333354
"""Get your subscriptions"""
334355
client = get_client()
@@ -366,6 +387,7 @@ def get_subscriptions(outfile, folder, out_format):
366387
type=click.Choice(["json", "csv", 'plain', 'markdown', 'org-mode']),
367388
default="json",
368389
help="Format of output, default: json")
390+
@catch_error
369391
def fetch_articles(outfile, stream_id, out_format):
370392
"""Fetch articles by stream id"""
371393
client = get_client()
@@ -406,6 +428,7 @@ def fetch_articles(outfile, stream_id, out_format):
406428
@click.option("-f", "--folder", help="Folder you want to deduplicate")
407429
@click.option("-t", "--thresh", type=float, default=0.8,
408430
help="Minimum similarity score")
431+
@catch_error
409432
def dedupe(folder, thresh):
410433
"""Deduplicate articles"""
411434
client = get_client()

0 commit comments

Comments
 (0)