Skip to content

Commit 66fb9b9

Browse files
authored
Merge pull request #107 from ImageMarkup/handle-grant-error
Gracefully handle invalid grant errors
2 parents 042f690 + 5dc460b commit 66fb9b9

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

isic_cli/cli/user.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import annotations
22

33
import json
4+
import sys
45
from typing import TYPE_CHECKING
56

7+
from authlib.integrations.base_client.errors import OAuthError
68
import click
79

810
if TYPE_CHECKING:
@@ -22,8 +24,18 @@ def login(obj: IsicContext):
2224
if obj.user:
2325
click.echo(f'Hello {obj.user["email"]}!')
2426
else:
25-
obj.oauth.login()
26-
click.echo("Success!")
27+
try:
28+
obj.oauth.login()
29+
except OAuthError as e:
30+
if e.error == "invalid_grant":
31+
click.secho(
32+
"Logging in timed out or had an unexpected error. Please try again.", fg="red"
33+
)
34+
sys.exit(1)
35+
else:
36+
raise
37+
else:
38+
click.secho("Success!", fg="green")
2739

2840

2941
@user.command()

tests/test_user.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from authlib.integrations.base_client.errors import OAuthError
4+
from girder_cli_oauth_client import GirderCliOAuthClient
35
import pytest
46

57

@@ -8,3 +10,15 @@ def test_user_login_logged_in(cli_run):
810
result = cli_run(["user", "login"])
911
assert result.exit_code == 0
1012
assert "Hello" in result.output
13+
14+
15+
@pytest.mark.usefixtures("_isolated_filesystem")
16+
def test_user_login_oauth_timeout(cli_run, mocker):
17+
mock_login = mocker.patch.object(
18+
GirderCliOAuthClient, "login", side_effect=OAuthError(error="invalid_grant")
19+
)
20+
21+
result = cli_run(["user", "login"])
22+
assert result.exit_code == 1
23+
assert "Logging in timed out or had an unexpected error" in result.output
24+
mock_login.assert_called_once()

0 commit comments

Comments
 (0)