From 8773120a450f2daaff6ec10132200097ea669d53 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Tue, 17 Sep 2024 15:03:17 +0200 Subject: [PATCH 1/2] Define client parameters --- backtracepython/client.py | 41 +++++++++++++++++++-------------- backtracepython/report_queue.py | 5 ++-- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/backtracepython/client.py b/backtracepython/client.py index db68711..3c95796 100644 --- a/backtracepython/client.py +++ b/backtracepython/client.py @@ -14,10 +14,7 @@ class globs: - endpoint = None next_except_hook = None - debug_backtrace = False - worker = None attachments = [] handler = None @@ -40,7 +37,8 @@ def create_and_send_report(ex_type, ex_value, ex_traceback): report = BacktraceReport() report.set_exception(ex_type, ex_value, ex_traceback) report.set_attribute("error.type", "Unhandled exception") - globs.handler.process(report.get_data(), globs.attachments) + if globs.handler: + globs.handler.process(report.get_data(), globs.attachments) def bt_except_hook(ex_type, ex_value, ex_traceback): @@ -65,27 +63,36 @@ def bt_except_hook(ex_type, ex_value, ex_traceback): globs.next_except_hook(ex_type, ex_value, ex_traceback) -def initialize(**kwargs): - globs.endpoint = construct_submission_url( - kwargs["endpoint"], kwargs.get("token", None) - ) - globs.debug_backtrace = kwargs.get("debug_backtrace", False) - globs.attachments = kwargs.get("attachments", []) - attribute_manager.add(kwargs.get("attributes", {})) +def initialize( + endpoint, + token=None, + debug_backtrace=False, + attachments=[], + attributes={}, + timeout=4, + ignore_ssl_certificate=False, + tab_width=8, + context_line_count=200, + collect_source_code=True, + disable_global_handler=False, +): + globs.endpoint = construct_submission_url(endpoint, token) + globs.debug_backtrace = debug_backtrace + globs.attachments = attachments + attribute_manager.add(attributes) globs.handler = ReportQueue( BacktraceRequestHandler( globs.endpoint, - kwargs.get("timeout", 4), - kwargs.get("ignore_ssl_certificate", False), + timeout, + ignore_ssl_certificate, globs.debug_backtrace, ), - SourceCodeHandler( - kwargs.get("tab_width", 8), kwargs.get("context_line_count", 200) - ), + SourceCodeHandler(tab_width, context_line_count) + if collect_source_code + else None, ) - disable_global_handler = kwargs.get("disable_global_handler", False) if not disable_global_handler: globs.next_except_hook = sys.excepthook sys.excepthook = bt_except_hook diff --git a/backtracepython/report_queue.py b/backtracepython/report_queue.py index e263cf0..a42048d 100644 --- a/backtracepython/report_queue.py +++ b/backtracepython/report_queue.py @@ -8,7 +8,7 @@ class ReportQueue: - def __init__(self, request_handler, source_code_handler): + def __init__(self, request_handler, source_code_handler=None): self.request_handler = request_handler self.source_code_handler = source_code_handler @@ -37,7 +37,8 @@ def add(self, report, attachments): # Immediately process the report and skip the queue process # Use this method to handle importa data before application exit def process(self, report, attachments): - self.source_code_handler.collect(report) + if self.source_code_handler is not None: + self.source_code_handler.collect(report) self.request_handler.send(report, attachments) def __del__(self): From adc65b45d14694a8b1aca6f5fa41f8addf99073a Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Tue, 17 Sep 2024 15:27:09 +0200 Subject: [PATCH 2/2] Format client --- backtracepython/client.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backtracepython/client.py b/backtracepython/client.py index 3c95796..1b74240 100644 --- a/backtracepython/client.py +++ b/backtracepython/client.py @@ -88,9 +88,11 @@ def initialize( ignore_ssl_certificate, globs.debug_backtrace, ), - SourceCodeHandler(tab_width, context_line_count) - if collect_source_code - else None, + ( + SourceCodeHandler(tab_width, context_line_count) + if collect_source_code + else None + ), ) if not disable_global_handler: