[docs]classMetatensorError(Exception):"""This class is used to throw exceptions for all errors in metatensor."""def__init__(self,message,status=None):super(Exception,self).__init__(message)self.message:str=message"""error message for this exception"""self.status:Optional[int]=status"""status code for this exception"""
LAST_EXCEPTION=Nonedef_save_exception(e):globalLAST_EXCEPTIONLAST_EXCEPTION=edef_check_status(status):ifstatus==MTS_SUCCESS:returnelifstatus>MTS_SUCCESS:raiseMetatensorError(last_error(),status)elifstatus<MTS_SUCCESS:globalLAST_EXCEPTIONe=LAST_EXCEPTIONLAST_EXCEPTION=NoneraiseMetatensorError(last_error(),status)fromedef_check_pointer(pointer):ifnotpointer:globalLAST_EXCEPTIONifLAST_EXCEPTIONisnotNone:e=LAST_EXCEPTIONLAST_EXCEPTION=NoneraiseMetatensorError(last_error())fromeelse:raiseMetatensorError(last_error())deflast_error():"""Get the last error message on this thread"""from._c_libimport_get_librarylib=_get_library()message=lib.mts_last_error()returnmessage.decode("utf8")