From 2e1a97be201cd82393818196b3d56682d43e2b7e Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Wed, 31 Jul 2024 09:08:27 +0200 Subject: [PATCH] :goal_net: Add warning when planetary body fetching is not returning the same object as expected --- src/ipyaladin/utils/_coordinate_parser.py | 11 +++++++++++ src/ipyaladin/utils/exceptions.py | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/ipyaladin/utils/_coordinate_parser.py b/src/ipyaladin/utils/_coordinate_parser.py index d16511f0..7335cc3f 100644 --- a/src/ipyaladin/utils/_coordinate_parser.py +++ b/src/ipyaladin/utils/_coordinate_parser.py @@ -1,9 +1,12 @@ +import warnings from typing import Tuple import requests from astropy.coordinates import SkyCoord, Angle import re +from ipyaladin.utils.exceptions import NameResolverWarning + def parse_coordinate_string(string: str, body: str = "sky") -> SkyCoord: """Parse a string containing coordinates. @@ -64,9 +67,17 @@ def _from_name_on_planet(string: str, body: str) -> SkyCoord: if request.status_code != requests.codes.ok: raise ValueError(f"Invalid coordinate string: {string}") data = request.json() + identifier = data["data"][0][1] lat = data["data"][0][5] lon = data["data"][0][6] system = data["data"][0][11] + if identifier != string: + warnings.warn( + f"Nothing found for '{string}', but found close" + f" name '{identifier}'. Moving to {identifier}.", + NameResolverWarning, + stacklevel=2, + ) if "+West" in system: lon = 360 - lon return SkyCoord(ra=lon, dec=lat, frame="icrs", unit="deg") diff --git a/src/ipyaladin/utils/exceptions.py b/src/ipyaladin/utils/exceptions.py index a77b3e73..c7254ef9 100644 --- a/src/ipyaladin/utils/exceptions.py +++ b/src/ipyaladin/utils/exceptions.py @@ -4,3 +4,11 @@ class WidgetCommunicationError(OSError): def __init__(self, message: str) -> None: self.message = message super(WidgetCommunicationError, self).__init__(message) + + +class NameResolverWarning(UserWarning): + """Warning raised when a name resolver is used.""" + + def __init__(self, message: str) -> None: + self.message = message + super(NameResolverWarning, self).__init__(message)