From f056f7c73430c2e182cd821a6f6184b4d2e28ab6 Mon Sep 17 00:00:00 2001 From: Alexander Druz Date: Fri, 9 Feb 2024 15:44:40 +0100 Subject: [PATCH] Report full traceback and path at exceptions --- renumics/spotlight/app.py | 7 ++++--- renumics/spotlight/reporting.py | 12 +++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/renumics/spotlight/app.py b/renumics/spotlight/app.py index 2425ca79..d39ea2ab 100644 --- a/renumics/spotlight/app.py +++ b/renumics/spotlight/app.py @@ -205,12 +205,12 @@ def _() -> None: self.include_router(plugin_api.router, prefix="/api/plugins") @self.exception_handler(Exception) - async def _(_: Request, e: Exception) -> JSONResponse: + async def _(request: Request, e: Exception) -> JSONResponse: if settings.verbose: logger.exception(e) else: logger.info(e) - emit_exception_event() + emit_exception_event(request.url.path) class_name = type(e).__name__ title = re.sub(r"([a-z])([A-Z])", r"\1 \2", class_name) return JSONResponse( @@ -219,11 +219,12 @@ async def _(_: Request, e: Exception) -> JSONResponse: ) @self.exception_handler(Problem) - async def _(_: Request, problem: Problem) -> JSONResponse: + async def _(request: Request, problem: Problem) -> JSONResponse: if settings.verbose: logger.exception(problem) else: logger.info(problem) + emit_exception_event(request.url.path) return JSONResponse( { "title": problem.title, diff --git a/renumics/spotlight/reporting.py b/renumics/spotlight/reporting.py index 787ec3c1..f7b8f1b3 100644 --- a/renumics/spotlight/reporting.py +++ b/renumics/spotlight/reporting.py @@ -7,6 +7,7 @@ import sys import threading import time +import traceback from functools import wraps from os import environ from typing import Any, Callable, Dict, Optional, Union @@ -171,14 +172,15 @@ def emit_exit_event() -> None: report_event({"type": "spotlight_exit"}) -def emit_exception_event() -> None: +def emit_exception_event(path: Optional[str] = None) -> None: """ Emit an exception event. """ - ex_type, ex_value, _ = sys.exc_info() - detail = str(ex_value) - if ex_type is not None: - detail = f"{ex_type.__name__}: {ex_value}" + detail = traceback.format_exc() + + if path: + detail = f"Path: {path}\n{detail}" + report_event( { "type": "spotlight_exception",