server: improve add_warning method

Add an exc_info parameter that optionally takes an
exception that is passed to the logging function. This
will log the traceback without an additional logging call.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2024-05-27 08:19:04 -04:00
parent 11d15f96d1
commit 0ff1d79b5b
10 changed files with 25 additions and 24 deletions

View File

@ -318,10 +318,9 @@ class MoonrakerApp:
svr.listen(port, address)
except Exception as e:
svr_type = "HTTPS" if "ssl_options" in args else "HTTP"
logging.exception(f"{svr_type} Server Start Failed")
self.server.add_warning(
f"Failed to start {svr_type} server: {e}. See moonraker.log "
"for more details."
"for more details.", exc_info=e
)
return None
return svr

View File

@ -31,7 +31,7 @@ class ButtonManager:
btn = GpioButton(cfg)
except Exception as e:
msg = f"Failed to load button [{cfg.get_name()}]\n{e}"
self.server.add_warning(msg)
self.server.add_warning(msg, exc_info=e)
continue
self.buttons[btn.name] = btn
self.server.register_notification("button:button_event")

View File

@ -1849,12 +1849,11 @@ class InotifyObserver(BaseFileSystemObserver):
old_root.clear_events()
try:
root_node = InotifyRootNode(self, root, root_path)
except Exception:
logging.exception(f"Inotify: failed to create root node '{root}'")
except Exception as e:
self.server.add_warning(
f"file_manager: Failed to create inotify root node {root}. "
"See moonraker.log for details.",
log=False
exc_info=e
)
return
self.watched_roots[root] = root_node
@ -1877,12 +1876,11 @@ class InotifyObserver(BaseFileSystemObserver):
for root, node in self.watched_roots.items():
try:
evts = node.scan_node()
except Exception:
logging.exception(f"Inotify: failed to scan root '{root}'")
except Exception as e:
self.server.add_warning(
f"file_manager: Failed to scan inotify root node '{root}'. "
"See moonraker.log for details.",
log=False
exc_info=e
)
continue
if not evts:

View File

@ -1712,8 +1712,7 @@ class InstallValidator:
except Exception as e:
has_error = True
msg = f"Failed to validate {name}: {e}"
logging.exception(msg)
self.server.add_warning(msg, log=False)
self.server.add_warning(msg, exc_info=e)
fm.disable_write_access()
else:
self.validation_enabled = False

View File

@ -46,7 +46,7 @@ class Notifier:
logging.info(f"Registered notifier: '{notifier.get_name()}'")
except Exception as e:
msg = f"Failed to load notifier[{cfg.get_name()}]\n{e}"
self.server.add_warning(msg)
self.server.add_warning(msg, exc_info=e)
continue
self.notifiers[notifier.get_name()] = notifier
@ -170,12 +170,12 @@ class NotifierInstance:
fm: FileManager = self.server.lookup_component("file_manager")
try:
rendered = self.attach.render(context)
except self.server.error:
logging.exception(f"notifier {self.name}: Failed to render attachment")
except self.server.error as e:
self.server.add_warning(
f"[notifier {self.name}]: The attachment is not valid. The "
"template failed to render.",
f"notifier {self.name}"
f"notifier {self.name}",
exc_info=e
)
self.attach = None
else:

View File

@ -74,7 +74,7 @@ class PrinterPower:
dev = dev_class(cfg)
except Exception as e:
msg = f"Failed to load power device [{cfg.get_name()}]\n{e}"
self.server.add_warning(msg)
self.server.add_warning(msg, exc_info=e)
continue
self.devices[dev.get_name()] = dev

View File

@ -267,7 +267,7 @@ class Sensors:
except Exception as e:
# Ensures that configuration errors are shown to the user
self.server.add_warning(
f"Failed to configure sensor [{cfg.get_name()}]\n{e}"
f"Failed to configure sensor [{cfg.get_name()}]\n{e}", exc_info=e
)
continue

View File

@ -130,7 +130,8 @@ class UpdateManager:
self.updaters[name] = deployer(cfg, self.cmd_helper)
except Exception as e:
self.server.add_warning(
f"[update_manager]: Failed to load extension {name}: {e}"
f"[update_manager]: Failed to load extension {name}: {e}",
exc_info=e
)
self.cmd_request_lock = asyncio.Lock()

View File

@ -377,7 +377,7 @@ class WLED:
except Exception as e:
# Ensures errors such as "Color not supported" are visible
msg = f"Failed to initialise strip [{cfg.get_name()}]\n{e}"
self.server.add_warning(msg)
self.server.add_warning(msg, exc_info=e)
continue
# Register two remote methods for GCODE

View File

@ -226,13 +226,17 @@ class Server:
logging.info(item)
def add_warning(
self, warning: str, warn_id: Optional[str] = None, log: bool = True
self,
warning: str,
warn_id: Optional[str] = None,
log: bool = True,
exc_info: Optional[BaseException] = None
) -> str:
if warn_id is None:
warn_id = str(id(warning))
self.warnings[warn_id] = warning
if log:
logging.warning(warning)
logging.warning(warning, exc_info=exc_info)
return warn_id
def remove_warning(self, warn_id: str) -> None:
@ -246,9 +250,9 @@ class Server:
if ret is not None:
await ret
except Exception as e:
logging.exception(f"Component [{name}] failed post init")
self.add_warning(f"Component '{name}' failed to load with "
f"error: {e}")
self.add_warning(
f"Component '{name}' failed to load with error: {e}", exc_info=e
)
self.set_failed_component(name)
def load_components(self) -> None: