diff --git a/tests/test_console.py b/tests/test_console.py index 0a3b3fa..94dd547 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -3,7 +3,8 @@ import pytest import requests import re -from toot import console, User, App, ConsoleError +from toot import console, User, App +from toot.exceptions import ConsoleError from tests.utils import MockResponse diff --git a/toot/__init__.py b/toot/__init__.py index 2ac3c92..31442f5 100644 --- a/toot/__init__.py +++ b/toot/__init__.py @@ -9,7 +9,3 @@ DEFAULT_INSTANCE = 'mastodon.social' CLIENT_NAME = 'toot - a Mastodon CLI client' CLIENT_WEBSITE = 'https://github.com/ihabunek/toot' - - -class ConsoleError(Exception): - pass diff --git a/toot/api.py b/toot/api.py index 14e5513..b2234c9 100644 --- a/toot/api.py +++ b/toot/api.py @@ -9,22 +9,11 @@ from urllib.parse import urlparse, urlencode from toot import CLIENT_NAME, CLIENT_WEBSITE from toot.utils import domain_exists from toot.logging import log_request, log_response +from toot.exceptions import ApiError, AuthenticationError, NotFoundError SCOPES = 'read write follow' -class ApiError(Exception): - pass - - -class NotFoundError(ApiError): - pass - - -class AuthenticationError(ApiError): - pass - - def _process_response(response): log_response(response) diff --git a/toot/app.py b/toot/app.py index 550670c..0bed2a1 100644 --- a/toot/app.py +++ b/toot/app.py @@ -4,7 +4,7 @@ import webbrowser from textwrap import wrap -from toot import ConsoleError +from toot.exceptions import ConsoleError from toot.utils import format_content # Attempt to load curses, which is not available on windows diff --git a/toot/auth.py b/toot/auth.py index 528643a..57405a1 100644 --- a/toot/auth.py +++ b/toot/auth.py @@ -5,7 +5,8 @@ import webbrowser from builtins import input from getpass import getpass -from toot import api, config, DEFAULT_INSTANCE, User, App, ConsoleError +from toot import api, config, DEFAULT_INSTANCE, User, App +from toot.exceptions import ApiError, ConsoleError from toot.output import print_out @@ -59,7 +60,7 @@ def login_interactive(app, email=None): try: print_out("Authenticating...") response = api.login(app, email, password) - except api.ApiError: + except ApiError: raise ConsoleError("Login failed") return create_user(app, email, response['access_token']) diff --git a/toot/commands.py b/toot/commands.py index 953e07f..1716137 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -6,8 +6,9 @@ from itertools import zip_longest from itertools import chain from textwrap import TextWrapper -from toot import api, config, ConsoleError +from toot import api, config from toot.auth import login_interactive, login_browser_interactive, create_app_interactive +from toot.exceptions import ConsoleError from toot.output import print_out, print_instance, print_account, print_search_results diff --git a/toot/console.py b/toot/console.py index d29a151..4590807 100644 --- a/toot/console.py +++ b/toot/console.py @@ -5,7 +5,8 @@ import logging from argparse import ArgumentParser, FileType from collections import namedtuple -from toot import config, api, commands, ConsoleError, CLIENT_NAME, CLIENT_WEBSITE +from toot import config, commands, CLIENT_NAME, CLIENT_WEBSITE +from toot.exceptions import ApiError, ConsoleError from toot.output import print_out, print_err @@ -311,6 +312,6 @@ def main(): except ConsoleError as e: print_err(str(e)) sys.exit(1) - except api.ApiError as e: + except ApiError as e: print_err(str(e)) sys.exit(1) diff --git a/toot/exceptions.py b/toot/exceptions.py new file mode 100644 index 0000000..2bf495d --- /dev/null +++ b/toot/exceptions.py @@ -0,0 +1,14 @@ +class ApiError(Exception): + """Raised when an API request fails for whatever reason.""" + + +class NotFoundError(ApiError): + """Raised when an API requests returns a 404.""" + + +class AuthenticationError(ApiError): + """Raised when login fails.""" + + +class ConsoleError(Exception): + """Raised when an error occurs which needs to be show to the user."""