Skip to content

Commit 8059bb9

Browse files
author
Nazar F
authored
fix: wrapped exceptions, making text more UI friendly (#320)
* fix: wrapped exceptions to be more human readbale * fix message for sdk * remove too generic test
1 parent 9134b26 commit 8059bb9

3 files changed

Lines changed: 63 additions & 25 deletions

File tree

openhexa/cli/api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@
3333
def handle_ssl_error(e):
3434
"""Handle SSL certificate verification errors with helpful message."""
3535
if "SSL certificate verification failed" in str(e) or "CERTIFICATE_VERIFY_FAILED" in str(e):
36-
raise GraphQLError(
36+
error_msg = (
3737
"SSL certificate verification failed. "
3838
"If you want to disable SSL verification, set the environment variable: HEXA_VERIFY_SSL=false"
3939
)
40+
if settings.debug:
41+
raise GraphQLError(error_msg) from e
42+
else:
43+
raise GraphQLError(error_msg)
4044

4145

4246
class InvalidDefinitionError(Exception):

openhexa/sdk/utils.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,36 @@
66
import os
77
import typing
88

9+
import httpx
10+
import requests
11+
912
from openhexa.graphql import BaseOpenHexaClient
1013
from openhexa.utils import create_requests_session
1114

1215

16+
class SSLError(Exception):
17+
"""Raised when SSL certificate verification fails."""
18+
19+
pass
20+
21+
22+
def handle_ssl_error(e: Exception) -> None:
23+
"""Handle SSL certificate verification errors with helpful message.
24+
25+
Args:
26+
e: The exception to handle.
27+
"""
28+
if "SSL certificate verification failed" in str(e) or "CERTIFICATE_VERIFY_FAILED" in str(e):
29+
error_msg = (
30+
"SSL certificate verification failed. "
31+
"If you want to disable SSL verification, set the environment variable: HEXA_VERIFY_SSL=false"
32+
)
33+
if Settings.debug():
34+
raise SSLError(error_msg) from e
35+
else:
36+
raise SSLError(error_msg)
37+
38+
1339
class Settings:
1440
"""Centralized settings for the OpenHexa SDK."""
1541

@@ -45,21 +71,24 @@ def get_environment():
4571

4672
def graphql(operation: str, variables: dict[str | typing.Any] | None = None) -> dict[str | typing.Any]:
4773
"""Performa GraphQL query."""
48-
auth_token = os.environ[
49-
"HEXA_TOKEN"
50-
] # Works for notebooks with the membership token and pipelines with the run token
74+
auth_token = os.environ["HEXA_TOKEN"]
5175
headers = {"Authorization": f"Bearer {auth_token}"}
5276
session = create_requests_session(verify=Settings.verify_ssl())
5377

54-
req = session.post(
55-
f"{os.environ['HEXA_SERVER_URL'].rstrip('/')}/graphql/",
56-
headers=headers,
57-
json={
58-
"query": operation,
59-
"variables": variables if variables is not None else {},
60-
},
61-
)
62-
req.raise_for_status()
78+
try:
79+
req = session.post(
80+
f"{os.environ['HEXA_SERVER_URL'].rstrip('/')}/graphql/",
81+
headers=headers,
82+
json={
83+
"query": operation,
84+
"variables": variables if variables is not None else {},
85+
},
86+
)
87+
req.raise_for_status()
88+
except (requests.exceptions.SSLError, httpx.ConnectError) as e:
89+
handle_ssl_error(e)
90+
raise
91+
6392
body = req.json()
6493
if "errors" in body:
6594
raise Exception(body["errors"])
@@ -80,7 +109,11 @@ def __init__(self, token: str | None = None, server_url: str | None = None):
80109
url = server_url or f"{os.environ['HEXA_SERVER_URL'].rstrip('/')}/graphql/"
81110
token = token or os.getenv("HEXA_TOKEN")
82111

83-
super().__init__(url=url, token=token, verify=Settings.verify_ssl())
112+
try:
113+
super().__init__(url=url, token=token, verify=Settings.verify_ssl())
114+
except (requests.exceptions.SSLError, httpx.ConnectError) as e:
115+
handle_ssl_error(e)
116+
raise
84117

85118

86119
class Iterator(metaclass=abc.ABCMeta):

tests/test_ssl_errors.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""SSL error handling test module."""
22

33

4+
import pytest
45
import requests
56

7+
from openhexa.sdk.utils import SSLError, handle_ssl_error
68
from openhexa.utils.session import create_requests_session
79

810

@@ -14,17 +16,16 @@ def test_create_requests_session_verify_default(self):
1416
session = create_requests_session()
1517
assert session.verify is True
1618

17-
def test_ssl_error_handling_logic(self):
18-
"""Test that SSL errors are properly converted to GraphQLError."""
19+
def test_ssl_error_handling_raises_custom_exception(self):
1920
ssl_error = requests.exceptions.SSLError("CERTIFICATE_VERIFY_FAILED error")
2021

21-
if "CERTIFICATE_VERIFY_FAILED" in str(ssl_error):
22-
expected_msg = (
23-
"SSL certificate verification failed. "
24-
"If you want to disable SSL verification, set the environment variable: HEXA_VERIFY_SSL=false"
25-
)
26-
assert "SSL certificate verification failed" in expected_msg
27-
assert "HEXA_VERIFY_SSL=false" in expected_msg
22+
with pytest.raises(SSLError) as exc_info:
23+
handle_ssl_error(ssl_error)
2824

29-
other_ssl_error = requests.exceptions.SSLError("Some other SSL error")
30-
assert "Some other SSL error" in str(other_ssl_error)
25+
error_msg = str(exc_info.value)
26+
assert "SSL certificate verification failed" in error_msg
27+
assert "HEXA_VERIFY_SSL=false" in error_msg
28+
29+
def test_non_ssl_error_passes_through(self):
30+
non_ssl_error = requests.exceptions.RequestException("Not an SSL error")
31+
handle_ssl_error(non_ssl_error)

0 commit comments

Comments
 (0)