Better error reporting from xyzgrid. Resolve #2482.

This commit is contained in:
Griatch 2021-08-03 21:52:30 +02:00
parent dd982e5222
commit b2c263b0c8
3 changed files with 22 additions and 6 deletions

View file

@ -188,7 +188,10 @@ def _option_list(*suboptions):
xymap_data = xyzgrid.grid
if not xymap_data:
print("The XYZgrid is currently empty. Use 'add' to add paths to your map data.")
if xyzgrid.db.map_data:
print("Grid could not load due to errors.")
else:
print("The XYZgrid is currently empty. Use 'add' to add paths to your map data.")
return
if not suboptions:
@ -265,7 +268,12 @@ def _option_add(*suboptions):
print(f" XYMaps from {path}:\n {mapnames}")
xymap_data_list.extend(maps)
grid.add_maps(*xymap_data_list)
print(f"Added (or readded) {len(xymap_data_list)} XYMaps to grid.")
try:
grid.reload()
except Exception as err:
print(err)
else:
print(f"Added (or readded) {len(xymap_data_list)} XYMaps to grid.")
def _option_spawn(*suboptions):

View file

@ -256,8 +256,10 @@ class XYMap:
return "\n".join("".join(line) for line in self.display_map[::-1])
def __repr__(self):
return (f"<XYMap(Z={self.Z}), {self.max_X + 1}x{self.max_Y + 1}, "
f"{len(self.node_index_map)} nodes>")
nnodes = 0
if self.node_index_map:
nnodes = len(self.node_index_map)
return (f"<XYMap(Z={self.Z}), {self.max_X + 1}x{self.max_Y + 1}, {nnodes} nodes>")
def log(self, msg):
if self.xyzgrid:

View file

@ -266,11 +266,14 @@ class XYZGrid(DefaultScript):
xymap.spawn_links(xy=(x, y), directions=directions)
def get_xyzgrid():
def get_xyzgrid(print_errors=True):
"""
Helper for getting the grid. This will create the XYZGrid global script if it didn't
previously exist.
Args:
print_errors (bool, optional): Print errors directly to console rather than to log.
"""
xyzgrid = XYZGrid.objects.all()
if not xyzgrid:
@ -288,5 +291,8 @@ def get_xyzgrid():
if not xyzgrid.ndb.loaded:
xyzgrid.reload()
except Exception as err:
xyzgrid.log(str(err))
if print_errors:
print(err)
else:
xyzgrid.log(str(err))
return xyzgrid