mirror of
https://github.com/ihabunek/toot.git
synced 2024-11-03 04:17:21 -05:00
Extract exceptions
This commit is contained in:
parent
177af4fac9
commit
7bbc98363e
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
13
toot/api.py
13
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)
|
||||
|
||||
|
@ -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
|
||||
|
@ -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'])
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
@ -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
14
toot/exceptions.py
Normal 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."""
|
Loading…
Reference in New Issue
Block a user