A more structured exception handling
This commit is contained in:
parent
a91f0939f6
commit
6b8b90da0d
4 changed files with 63 additions and 14 deletions
|
@ -3,10 +3,16 @@ import time
|
|||
from datetime import datetime
|
||||
from operator import itemgetter
|
||||
|
||||
from pocket import Pocket, PocketException
|
||||
from pocket import (
|
||||
Pocket,
|
||||
PocketException,
|
||||
PocketAutException
|
||||
)
|
||||
|
||||
from progress.spinner import Spinner
|
||||
|
||||
from .config import Configs
|
||||
from .exceptions import AppException, AppNotConfigured
|
||||
from .storage import Storage
|
||||
|
||||
|
||||
|
@ -49,7 +55,7 @@ class PocketApp:
|
|||
try:
|
||||
return self._pocket.add(url, title, tags)
|
||||
except PocketException as e:
|
||||
return self._check_exception(e)
|
||||
raise self._check_exception(e) from e
|
||||
|
||||
def get_articles(self, limit=None, order=None):
|
||||
if self._storage.is_empty():
|
||||
|
@ -61,7 +67,7 @@ class PocketApp:
|
|||
try:
|
||||
self._pocket.archive(int(item_id)).commit()
|
||||
except PocketException as e:
|
||||
return self._check_exception(e)
|
||||
raise self._check_exception(e) from e
|
||||
|
||||
def find_article(self, item_id):
|
||||
index = self._storage.read()
|
||||
|
@ -98,8 +104,7 @@ class PocketApp:
|
|||
)
|
||||
except PocketException as e:
|
||||
spinner.finish()
|
||||
self._check_exception(e)
|
||||
return
|
||||
raise self._check_exception(e) from e
|
||||
|
||||
if not articles['list']:
|
||||
break
|
||||
|
@ -147,9 +152,7 @@ class PocketApp:
|
|||
return int(time.mktime(date.timetuple()))
|
||||
|
||||
def _check_exception(self, e):
|
||||
if int(e.error_code) == 136:
|
||||
print('Application is not configured')
|
||||
print('Run `pocket-cli configure` to be able to use this app')
|
||||
return
|
||||
print(e.message)
|
||||
return False
|
||||
if isinstance(e, PocketAutException):
|
||||
raise AppNotConfigured('Application is not configured')
|
||||
|
||||
raise AppException(e.message)
|
||||
|
|
|
@ -6,6 +6,7 @@ import webbrowser
|
|||
import click
|
||||
|
||||
from .app import PocketApp
|
||||
from .exceptions import AppNotConfigured, AppException
|
||||
from .utils import format_article
|
||||
|
||||
|
||||
|
@ -73,7 +74,19 @@ def add_article(url, title, tags):
|
|||
type=click.Choice(['asc', 'desc']),
|
||||
help='Order of items to return')
|
||||
def list_articles(limit, order):
|
||||
articles = pocket_app.get_articles(limit, order)
|
||||
try:
|
||||
articles = pocket_app.get_articles(limit, order)
|
||||
except AppNotConfigured:
|
||||
app_not_configured()
|
||||
return
|
||||
except AppException as e:
|
||||
exception_occured(e)
|
||||
return
|
||||
|
||||
if not articles:
|
||||
print('Articles index is empty,'
|
||||
'run pocket-cli fetch to index your articles')
|
||||
return
|
||||
|
||||
try:
|
||||
pager = subprocess.Popen(['less'],
|
||||
|
@ -140,13 +153,35 @@ def random_article(browser, archive):
|
|||
|
||||
@click.command()
|
||||
def fetch():
|
||||
pocket_app.fetch_articles(True)
|
||||
try:
|
||||
pocket_app.fetch_articles(True)
|
||||
except AppNotConfigured:
|
||||
app_not_configured()
|
||||
except AppException as e:
|
||||
exception_occured(e)
|
||||
|
||||
|
||||
@click.command(name='archive')
|
||||
@click.argument('article_id')
|
||||
def archive_article(article_id):
|
||||
pocket_app.archive_article(int(article_id))
|
||||
try:
|
||||
pocket_app.archive_article(int(article_id))
|
||||
except AppNotConfigured:
|
||||
app_not_configured()
|
||||
except AppException as e:
|
||||
exception_occured(e)
|
||||
|
||||
|
||||
def app_not_configured():
|
||||
print('App is not configured')
|
||||
print('Run `pocket-cli configure` to be able to use the app')
|
||||
|
||||
|
||||
def exception_occured(exception):
|
||||
print('An error occured while '
|
||||
'trying to perform requested action: {}'.format(
|
||||
exception.message
|
||||
))
|
||||
|
||||
|
||||
main.add_command(configure)
|
||||
|
|
8
pocket_cli/exceptions.py
Normal file
8
pocket_cli/exceptions.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
class AppNotConfigured(Exception):
|
||||
def __init__(self, message):
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
class AppException(Exception):
|
||||
def __init__(self, message):
|
||||
super().__init__(message)
|
|
@ -34,6 +34,9 @@ class Storage:
|
|||
def read(self, limit=10, order='asc'):
|
||||
index = []
|
||||
|
||||
if not os.path.exists(self._filename):
|
||||
return index
|
||||
|
||||
row_counter = 0
|
||||
with open(self._filename, 'r') as csv_file:
|
||||
reader = csv.DictReader(csv_file)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue