Error Handling in python

Hi,
when I call a python script and this script has an error, then the processing stops, the basic macro editor is called and I see the error description in a popup.
I tried to avoid this by using try: in my python script - that works ok, the error is caught and the python script keeps running.
My question is now, I am missing the error description I would see in the basic popup - is there a way/variable in python to read the error description?

Thank you

Hello,

Try this → How to catch libreoffice macro errors and forward to a Python log file?

Thank you, not exactly what I was looking for, but close enough to amend

.
There are multiple links on this post → Msgbox python
.
Also see → Input/Output to Screen

I amended the code with my own “file handler” - outside LO that works fine.

class LogHandler(logging.Handler):
"""
Class to write log files in a self defined source (string or calc sheet)
"""
def __init__(self):
    logging.Handler.__init__(self)

def format(self, record: logging.LogRecord) -> str:
   return record.__dict__

def emit(self, record):
    print(format(record))
    print(type(format(record)))

def close(self):
    logging.Handler.close(self)

class Logger_decorator():

def __init__(self, function):
    LOGLEVEL = 10
    self.function = function
    self.logger = logging.getLogger(self.function.__name__)
    #self.fh = logging.FileHandler(log_path, mode='w')
    self.fh = LogHandler()                                             
    #self.fh.setLevel(LOGLEVEL)
    formatter = logging.Formatter(fmt='%(asctime)s:%(levelname)s:%(filename)s:%(lineno)d:%(message)s',
                          datefmt='%Y-%m-%d-%H:%M:%S')
    self.fh.setFormatter(formatter)
    self.format = formatter
    self.logger.addHandler(self.fh)

def __call__(self, *args, **kwargs):
    try:
        return self.function(*args,**kwargs)
    except Exception as ex:
        self.logger.exception(ex)

But when I replace the print statements in emit with writing into calc-sheets I get a type error. I hope this only a conversion issue

Don’t understand how it got here from:

Hi,
I am doing this:

class LogHandler(logging.Handler):
"""
Class to write log files in a self defined source (string or calc sheet)
"""
def __init__(self):
    logging.Handler.__init__(self)
    self.doc = XSCRIPTCONTEXT.getDocument()
    if 'ActivityLog' in self.doc.Sheets:
        self.sheet = self.doc.Sheets['ActivityLog']
    else:
        self.doc.Sheets.insertNewByName('ActivityLog', 0)  # ('ErrorLog', postiton)
        self.sheet = self.doc.Sheets['ActivityLog']
        self.sheet["A1"].String = "Timestamp"
        self.sheet["B1"].String = "Routine"
        self.sheet["C1"].String = "Error Msg"
        self.sheet.TabColor = get_color(255,0,0)

    self.cursor = self.sheet.createCursorByRange(self.sheet["A1"])
    self.cursor.gotoEnd()
    self.row = self.cursor.RangeAddress.EndRow + 1

def format(self, record: logging.LogRecord) -> str:
    return record.__dict__

def emit(self, record: logging.LogRecord):
    self.now = datetime.now()
    self.sheet[self.row, 0].String = self.now.strftime("%d.%m.%Y, %H:%M:%S")
    self.sheet[self.row, 1].String = "ErrorHandler"
    self.sheet[self.row, 2].String = format(record)
    self.row = self.row + 1

def close(self):
    logging.Handler.close(self)

But I found my error. When I use the decorator, it catches the error but stops the entire function causing a downstream error in the calling function.
When I use try: within the function, I still can allow the function to return a valid value.