Extract exceptions

This commit is contained in:
Ivan Habunek 2017-12-30 13:32:52 +01:00
parent 177af4fac9
commit 7bbc98363e
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
8 changed files with 26 additions and 23 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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'])

View File

@ -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

View File

@ -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)

14
toot/exceptions.py Normal file
View File

@ -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."""