## Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending#""" This module defines a custom exception for the Deephaven Python Integration Package.The custom exception is named DHError. It encapsulates exceptions thrown by the Deephaven engine and thePython/Java integration layer and provides 3 convenient properties: root_cause, compact_traceback, andtraceback for easy debugging."""importreimporttraceback
[docs]classDHError(Exception):""" The custom exception class for the Deephaven Python package. This exception can be raised due to user errors or system errors when Deephaven resources and functions are accessed, for example, during reading a CSV/Parquet file into a Deephaven table or performing an aggregation or join operation on Deephaven tables. It is a good practice for Python code to catch this exception and handle it appropriately. """def__init__(self,cause=None,message=""):ifisinstance(cause,str)andmessage=="":# Saves a lot of debugging headache when library code incorrectly creates something like:# raise DHError("My error message here")message=causecause=Noneself._message=messageself._traceback=traceback.format_exc()self._cause=causetb_lines=self._traceback.splitlines()self._root_cause=""self._compact_tb=[]for_compact_tb=Truefortb_lnintb_lines:iftb_ln.startswith("caused by"):self._root_cause=tb_ln.split("by")[1].strip()iftb_ln.strip().endswith(":"):self._compact_tb.append(tb_ln[:-1].strip())else:self._compact_tb.append(tb_ln)elifre.match("^.*Error:",tb_ln):self._root_cause=tb_lnself._compact_tb.append(tb_ln)for_compact_tb=Falseeliftb_ln.startswith("Exception message"):self._root_cause=tb_ln.split(":")[1]if":"intb_lnelsetb_lnself._root_cause=self._root_cause.strip()self._compact_tb[-1]=self._compact_tb[-1]+f" {self._root_cause}"iffor_compact_tb:self._compact_tb.append(tb_ln)ifnotself._root_cause:self._root_cause=self._message@propertydefroot_cause(self):""" The root cause of the exception. """returnself._root_cause@propertydeftraceback(self):""" The traceback of the exception. """returnself._traceback@propertydefcompact_traceback(self)->str:""" The compact traceback of the exception. """return"\n".join(self._compact_tb)def__str__(self):returnf"{self._message} : {self._root_cause}\n{self._traceback}"