Skip to content

Commit

Permalink
🥅 Improve error management with new '_ready' traitlets
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Jul 25, 2024
1 parent 1b61cb7 commit e719a82
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions js/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function initAladinLite(model, el) {
y: twoAxisFoV[1],
});
model.set("_wcs", aladin.getViewWCS());
model.set("_ready", true);
model.save_changes();

el.appendChild(aladinDiv);
Expand Down
13 changes: 1 addition & 12 deletions src/ipyaladin/utils/_coordinate_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
from astropy.coordinates import SkyCoord, Angle
import re

from astropy.coordinates.name_resolve import NameResolveError

from ipyaladin.utils.exceptions import WidgetCommunicationError

OK_STATUS_CODE = 200


Expand All @@ -30,14 +26,7 @@ def parse_coordinate_string(string: str, body: str = "sky") -> SkyCoord:
"""
if not _is_coordinate_string(string):
if body == "sky":
try:
return SkyCoord.from_name(string)
except NameResolveError as e:
raise WidgetCommunicationError(
f"Either '{string}' is not a valid object name, or "
f"the survey body type is not yet defined so you "
f"need to set the target from another cell."
) from e
return SkyCoord.from_name(string)
return _from_name_on_planet(string, body)
coordinates: Tuple[str, str] = _split_coordinate_string(string)
# Parse ra and dec to astropy Angle objects
Expand Down
15 changes: 14 additions & 1 deletion src/ipyaladin/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import anywidget
from astropy.coordinates import SkyCoord, Angle
from astropy.coordinates.name_resolve import NameResolveError
from astropy.table.table import QTable
from astropy.table import Table
from astropy.io import fits as astropy_fits
Expand Down Expand Up @@ -190,6 +191,10 @@ class Aladin(anywidget.AnyWidget):
)

# Values
_ready = Bool(
False,
help="A private trait that stores the readiness of the widget.",
).tag(sync=True)
_wcs = traitlets.Dict().tag(sync=True)
_fov_xy = traitlets.Dict().tag(sync=True)

Expand Down Expand Up @@ -345,7 +350,15 @@ def target(self) -> SkyCoord:
@target.setter
def target(self, target: Union[str, SkyCoord]) -> None:
if isinstance(target, str): # If the target is str, parse it
target = parse_coordinate_string(target, self._survey_body)
try:
target = parse_coordinate_string(target, self._survey_body)
except NameResolveError as e:
if not self._ready:
raise WidgetCommunicationError(
f"Either '{target}' is not a valid object name, or "
f"the survey body type is not yet defined so you "
f"need to set the target from another cell."
) from e
elif not isinstance(target, SkyCoord): # If the target is not str or SkyCoord
raise ValueError(
"target must be a string or an astropy.coordinates.SkyCoord object"
Expand Down

0 comments on commit e719a82

Please sign in to comment.