Skip to content

Commit

Permalink
remote.nextstrain_dot_org: Include an explicit origin in error messages
Browse files Browse the repository at this point in the history
Otherwise, for remote origins other than the default of nextstrain.org,
the error messages suggest incorrect/misleading possible solutions in
the form of commands to run.

Motivated by @jameshadfield's comments in Slack.¹

¹ <https://bedfordlab.slack.com/archives/C01LCTT7JNN/p1723760025178909?thread_ts=1723701856.612259&cid=C01LCTT7JNN>
  • Loading branch information
tsibley committed Aug 20, 2024
1 parent a3b4b14 commit 94a1f81
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ development source code and as such may not be routinely kept up to date.

# __NEXT__

## Bug fixes

* The suggested commands to run (i.e. potential solutions) in expected errors
from `nextstrain remote` now explicitly include the remote origin to avoid
being incorrect or misleading for origins other than nextstrain.org. For
example, if the error message suggested running `nextstrain login`, it now
suggests `nextstrain login https://nextstrain.org`.
([#390](https://github.com/nextstrain/cli/pull/390))


# 8.5.1 (31 July 2024)

Expand Down
36 changes: 25 additions & 11 deletions nextstrain/cli/remote/nextstrain_dot_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
from email.message import EmailMessage
from pathlib import Path, PurePosixPath
from requests.utils import parse_dict_header
from shlex import quote as shquote
from tempfile import NamedTemporaryFile
from textwrap import indent, wrap
from typing import Dict, Iterable, List, NamedTuple, Optional, Tuple, Union
Expand Down Expand Up @@ -264,7 +265,7 @@ def put(endpoint, file, media_type):
else:
raise

raise_for_status(response)
raise_for_status(origin, response)

# Upload datasets
for dataset, files in datasets.items():
Expand Down Expand Up @@ -377,7 +378,7 @@ def download(url: URL, local_path: Path, recursively: bool = False, dry_run: boo
continue

# Check for bad response
raise_for_status(response)
raise_for_status(origin, response)

if content_media_type(response) != subresource.media_type:
raise UserError(f"Path {path} does not seem to be a {subresource}.")
Expand Down Expand Up @@ -540,7 +541,7 @@ def _ls(origin: Origin, path: NormalizedPath, recursively: bool = False, http: r
params = {"prefix": str(path)},
headers = {"Accept": "application/json"})

raise_for_status(response)
raise_for_status(origin, response)

available = response.json()

Expand Down Expand Up @@ -613,7 +614,7 @@ def delete(url: URL, recursively: bool = False, dry_run: bool = False) -> Iterab

response = http.delete(endpoint)

raise_for_status(response)
raise_for_status(origin, response)

assert response.status_code == 204

Expand Down Expand Up @@ -807,7 +808,7 @@ def __call__(self, request: requests.PreparedRequest) -> requests.PreparedReques
return request


def raise_for_status(response: requests.Response) -> None:
def raise_for_status(origin: Origin, response: requests.Response) -> None:
"""
Human-centered error handling for nextstrain.org API responses.
Expand Down Expand Up @@ -875,26 +876,39 @@ def raise_for_status(response: requests.Response) -> None:
# enough to handle things like re-seeking streams (which
# may not be possible without cooperation).
# -trs, 10 May 2022
raise UserError("""
raise UserError(f"""
Login credentials appear to be out of date.
Please run `nextstrain login --renew` and then retry your command.
Please run
nextstrain login --renew {shquote(origin)}
and then retry your command.
""") from err
else:
raise UserError(f"""
Permission denied.
Are you logged in as the correct user? Current user: {user.username}.
Are you logged in as the correct user?
Current user: {user.username}
If your permissions were recently changed (e.g. new group
membership), it might help to run `nextstrain login --renew`
membership), it might help to run
nextstrain login --renew {shquote(origin)}
and then retry your command.
""") from err
else:
raise UserError("""
raise UserError(f"""
Permission denied.
Logging in with `nextstrain login` might help?
Logging in with
nextstrain login {shquote(origin)}
might help?
""") from err

elif status == 404:
Expand Down

0 comments on commit 94a1f81

Please sign in to comment.