Skip to content

Commit

Permalink
Fix OpenTTD#3: Show more meaningful errors to cli users
Browse files Browse the repository at this point in the history
  • Loading branch information
erenes committed Apr 10, 2021
1 parent 2a39719 commit 859c851
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
16 changes: 13 additions & 3 deletions bananas_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import click
import logging

from aiohttp.client_exceptions import ClientConnectorError
from .authentication import authenticate
from .helpers import task
from .session import Session
from .exceptions import Exit

log = logging.getLogger(__name__)
pass_session = click.make_pass_decorator(Session)
Expand All @@ -19,9 +21,10 @@
@click.option("--tus-url", help="BaNaNaS tus URL. (normally the same as --api-url)", metavar="URL")
@click.option("--client-id", help="Client-id to use for authentication", default="ape", show_default=True)
@click.option("--audience", help="Audience to use for authentication", default="github", show_default=False)
@click.option("--verbose", help="Enable verbose output for errors, showing tracebacks", is_flag=True)
@click.pass_context
@task
async def cli(ctx, api_url, tus_url, client_id, audience):
async def cli(ctx, api_url, tus_url, client_id, audience, verbose):
"""
A CLI tool to list, upload, and otherwise modify BaNaNaS content.
Expand All @@ -40,7 +43,7 @@ async def cli(ctx, api_url, tus_url, client_id, audience):
if not tus_url:
tus_url = api_url

session = Session(api_url, tus_url)
session = Session(api_url, tus_url, verbose)
ctx.obj = session

await session.start()
Expand All @@ -49,7 +52,14 @@ async def cli(ctx, api_url, tus_url, client_id, audience):
if "-h" in os_args or "--help" in os_args:
return

await authenticate(session, client_id, audience)
try:
await authenticate(session, client_id, audience)
except (ClientConnectorError, NameError) as e:
if verbose:
log.exception(e)
else:
log.error(e)
raise Exit


@task
Expand Down
3 changes: 3 additions & 0 deletions bananas_cli/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def show_validation_errors(data):
@pass_session
@task
async def upload(session, version, name, description, url, license, files):
if len(files) == 0:
log.error("No files specified for upload")
return
parts = files[0].split("/")[:-1]
for filename in files:
check_parts = filename.split("/")
Expand Down
10 changes: 7 additions & 3 deletions bananas_cli/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@


class Session:
def __init__(self, api_url, tus_url):
def __init__(self, api_url, tus_url, verbose):
self.session = None
self.api_url = f"{api_url}/"
self.tus_url = f"{tus_url}/"

self._headers = {}
self.verbose = verbose

async def start(self):
self.session = aiohttp.ClientSession()
Expand Down Expand Up @@ -64,8 +65,11 @@ def tus_upload(self, upload_token, fullpath, filename):
metadata={"filename": filename, "upload-token": upload_token},
)
uploader.upload()
except TusCommunicationError:
log.exception(f"Failed to upload file '{filename}'")
except TusCommunicationError as e:
if self.verbose:
log.exception(f"Failed to upload file '{filename}'")
else:
log.error(f"Failed to upload file '{filename}': {e}")
raise Exit

def set_header(self, header, value):
Expand Down

0 comments on commit 859c851

Please sign in to comment.