Skip to content

Commit 7c411fd

Browse files
committed
♻️ Improve how we handle invalid tokens
Shortcake-Parent: isolate-tests-from-config
1 parent 19256d8 commit 7c411fd

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

src/fastapi_cloud_cli/commands/logs.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717
from fastapi_cloud_cli.utils.apps import AppConfig, get_app_config
1818
from fastapi_cloud_cli.utils.auth import Identity
19-
from fastapi_cloud_cli.utils.cli import get_rich_toolkit
19+
from fastapi_cloud_cli.utils.cli import get_rich_toolkit, handle_invalid_token_error
2020

2121
logger = logging.getLogger(__name__)
2222

@@ -87,18 +87,15 @@ def _process_log_stream(
8787
toolkit.print_line()
8888
return
8989
except StreamLogError as e:
90-
error_msg = str(e)
91-
if "HTTP 401" in error_msg or "HTTP 403" in error_msg:
92-
toolkit.print(
93-
"The specified token is not valid. Use [blue]`fastapi login`[/] to generate a new token.",
94-
)
95-
elif "HTTP 404" in error_msg:
90+
if e.status_code in (401, 403):
91+
toolkit.print(handle_invalid_token_error())
92+
elif e.status_code == 404:
9693
toolkit.print(
9794
"App not found. Make sure to use the correct account.",
9895
)
9996
else:
10097
toolkit.print(
101-
f"[red]Error:[/] {escape(error_msg)}",
98+
f"[red]Error:[/] {escape(str(e))}",
10299
)
103100
raise typer.Exit(1) from None
104101
except (TooManyRetriesError, TimeoutError):

src/fastapi_cloud_cli/utils/api.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
class StreamLogError(Exception):
3333
"""Raised when there's an error streaming logs (build or app logs)."""
3434

35-
pass
35+
def __init__(self, message: str, *, status_code: Optional[int] = None) -> None:
36+
super().__init__(message)
37+
self.status_code = status_code
3638

3739

3840
class TooManyRetriesError(Exception):
@@ -100,7 +102,8 @@ def _backoff() -> None:
100102
except Exception:
101103
error_detail = "(response body unavailable)"
102104
raise StreamLogError(
103-
f"HTTP {error.response.status_code}: {error_detail}"
105+
f"HTTP {error.response.status_code}: {error_detail}",
106+
status_code=error.response.status_code,
104107
) from error
105108

106109

src/fastapi_cloud_cli/utils/cli.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from rich_toolkit.progress import Progress
1111
from rich_toolkit.styles import MinimalStyle, TaggedStyle
1212

13-
from .auth import Identity
13+
from .auth import Identity, delete_auth_config
1414

1515
logger = logging.getLogger(__name__)
1616

@@ -68,6 +68,20 @@ def get_rich_toolkit(minimal: bool = False) -> RichToolkit:
6868
return RichToolkit(theme=theme)
6969

7070

71+
def handle_invalid_token_error() -> str:
72+
identity = Identity()
73+
74+
message = "The specified token is not valid. "
75+
76+
if identity.auth_mode == "user":
77+
delete_auth_config()
78+
message += "Use `fastapi login` to generate a new token."
79+
else:
80+
message += "Make sure to use a valid token."
81+
82+
return message
83+
84+
7185
@contextlib.contextmanager
7286
def handle_http_errors(
7387
progress: Progress,
@@ -91,14 +105,7 @@ def handle_http_errors(
91105
logger.debug(e.response.json()) # pragma: no cover
92106

93107
if isinstance(e, HTTPStatusError) and e.response.status_code in (401, 403):
94-
message = "The specified token is not valid. "
95-
96-
identity = Identity()
97-
98-
if identity.auth_mode == "user":
99-
message += "Use `fastapi login` to generate a new token."
100-
else:
101-
message += "Make sure to use a valid token."
108+
message = handle_invalid_token_error()
102109

103110
else:
104111
message = (

0 commit comments

Comments
 (0)