Skip to content

Commit fc0780b

Browse files
committed
authn.configuration: Improve error handling in openid_configuration()
@victorlin suggested¹ we improve error handling in this code path, and he's right. Catch and improve the output for the three most common errors we'll encounter: connection errors, HTTP errors, and JSON decoding errors. The original error is summarized in the error output, and by chaining the new exception from the original, the full chain in all its detail will be printed when NEXTSTRAIN_DEBUG=1 for troubleshooting. ¹ <#333 (comment)>
1 parent 2497224 commit fc0780b

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

nextstrain/cli/authn/configuration.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,31 @@ def openid_configuration(origin: Origin):
2020
assert origin
2121

2222
with requests.Session() as http:
23-
response = http.get(origin.rstrip("/") + "/.well-known/openid-configuration")
23+
try:
24+
response = http.get(origin.rstrip("/") + "/.well-known/openid-configuration")
25+
response.raise_for_status()
26+
return response.json()
2427

25-
if response.status_code == 404:
28+
except requests.exceptions.ConnectionError as err:
2629
raise UserError(f"""
27-
Failed to retrieve authentication metadata for {origin}.
30+
Could not connect to {origin} to retrieve
31+
authentication metadata:
32+
33+
{type(err).__name__}: {err}
34+
35+
That remote may be invalid or you may be experiencing network
36+
connectivity issues.
37+
""") from err
38+
39+
except (requests.exceptions.HTTPError, requests.exceptions.JSONDecodeError) as err:
40+
raise UserError(f"""
41+
Failed to retrieve authentication metadata for {origin}:
42+
43+
{type(err).__name__}: {err}
2844
2945
That remote seems unlikely to be an alternate nextstrain.org
3046
instance or an internal Nextstrain Groups Server instance.
31-
""")
32-
33-
response.raise_for_status()
34-
return response.json()
47+
""") from err
3548

3649

3750
def client_configuration(origin: Origin):

0 commit comments

Comments
 (0)