Skip to content

Commit

Permalink
Added a custom warning for connection warnings that the VSOclient raises
Browse files Browse the repository at this point in the history
  • Loading branch information
nabobalis committed Oct 9, 2023
1 parent d7e0eaa commit 0f87305
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ filterwarnings =
error
# Do not fail on pytest config issues (i.e. missing plugins) but do show them
always::pytest.PytestConfigWarning
ignore:The sunpy.database module is no longer actively maintained
ignore:The sunpy.database module is no longer actively maintainedb
# This is raised when the VSO redirects and we do not want this to stop the CI
ignore::SunpyConnectionWarning
# A list of warnings to ignore follows. If you add to this list, you MUST
# add a comment or ideally a link to an issue that explains why the warning
# is being ignored
Expand Down
2 changes: 0 additions & 2 deletions sunpy/net/helio/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ def link_test(link):
--------
>>> from sunpy.net.helio import parser
>>> result = parser.link_test('http://msslkz.mssl.ucl.ac.uk/helio-hec/HelioService') # doctest: +SKIP
>>> print(parser.link_test('http://rrnx.invalid_url5523.com')) # doctest: +SKIP
None
"""
try:
with closing(urlopen(link, timeout=LINK_TIMEOUT)) as fd:
Expand Down
4 changes: 2 additions & 2 deletions sunpy/net/vso/tests/test_vso.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from sunpy.tests.mocks import MockObject
from sunpy.time import parse_time
from sunpy.util.exceptions import SunpyUserWarning
from sunpy.util.exceptions import SunpyConnectionWarning, SunpyUserWarning


class MockQRRecord:
Expand Down Expand Up @@ -318,7 +318,7 @@ def test_fallback_if_cgi_offline(mocker):
# Now patch out that URL so we can cause it to return an error
mocker.patch('sunpy.net.vso.vso.urlopen', side_effect=partial(fail_to_open_nso_cgi, cgi_url))

with pytest.warns(SunpyUserWarning,
with pytest.warns(SunpyConnectionWarning,
match=f"Connection to {cgi_url} failed with error .* Retrying with different url and port"):
mirror = get_online_vso_url()
assert mirror["url"] != "http://docs.virtualsolar.org/WSDL/VSOi_rpc_literal.wsdl"
Expand Down
8 changes: 4 additions & 4 deletions sunpy/net/vso/vso.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sunpy.net.base_client import BaseClient, QueryResponseRow
from sunpy.net.vso import attrs
from sunpy.net.vso.attrs import _walker as walker
from sunpy.util.exceptions import warn_user
from sunpy.util.exceptions import warn_connection, warn_user
from sunpy.util.net import parse_header, slugify
from sunpy.util.parfive_helpers import Downloader, Results
from .exceptions import (
Expand Down Expand Up @@ -59,7 +59,7 @@ def check_connection(url):
try:
return urlopen(url, timeout=15).getcode() == 200
except (OSError, HTTPError, URLError) as e:
warn_user(f"Connection to {url} failed with error {e}. Retrying with different url and port.")
warn_connection(f"Connection to {url} failed with error {e}. Retrying with different url and port.")
return False


Expand All @@ -74,10 +74,10 @@ def check_cgi_connection(url):
except HTTPError as e:
if e.code == 411:
return True
warn_user(f"Connection to {url} failed with error {e}. Retrying with different url and port.")
warn_connection(f"Connection to {url} failed with error {e}. Retrying with different url and port.")
return False
except (OSError, URLError) as e:
warn_user(f"Connection to {url} failed with error {e}. Retrying with different url and port.")
warn_connection(f"Connection to {url} failed with error {e}. Retrying with different url and port.")
return False


Expand Down
25 changes: 23 additions & 2 deletions sunpy/util/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__all__ = ["NoMapsInFileError",
"SunpyWarning", "SunpyUserWarning", "SunpyDeprecationWarning",
"SunpyPendingDeprecationWarning", "SunpyMetadataWarning",
"warn_user", "warn_deprecated", "warn_metadata"]
"warn_user", "warn_deprecated", "warn_metadata", "warn_connection"]


class NoMapsInFileError(Exception):
Expand Down Expand Up @@ -44,7 +44,6 @@ class SunpyMetadataWarning(UserWarning, SunpyWarning):
stacklevel=3 to show the user where the issue occurred in their code.
"""


class SunpyDeprecationWarning(FutureWarning, SunpyWarning):
"""
A warning class to indicate a deprecated feature.
Expand All @@ -57,6 +56,14 @@ class SunpyPendingDeprecationWarning(PendingDeprecationWarning, SunpyWarning):
"""


class SunpyConnectionWarning(SunpyUserWarning):
"""
A warning class to indicate a connection warning.
This will not fail the CI (via a pytest ignore) as it is not a critical warning.
"""


def warn_metadata(msg, stacklevel=1):
"""
Raise a `SunpyMetadataWarning`.
Expand Down Expand Up @@ -88,6 +95,20 @@ def warn_user(msg, stacklevel=1):
"""
warnings.warn(msg, SunpyUserWarning, stacklevel + 1)

def warn_connection(msg, stacklevel=1):
"""
Raise a `SunpyConnectionWarning`.
Parameters
----------
msg : str
Warning message.
stacklevel : int
This is interpreted relative to the call to this function,
e.g. ``stacklevel=1`` (the default) sets the stack level in the
code that calls this function.
"""
warnings.warn(msg, SunpyConnectionWarning, stacklevel + 1)

def warn_deprecated(msg, stacklevel=1):
"""
Expand Down

0 comments on commit 0f87305

Please sign in to comment.