Coverage for mindsdb / utilities / exception.py: 86%

49 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1from textwrap import indent 

2 

3 

4from mindsdb.api.mysql.mysql_proxy.libs.constants.mysql import ERR 

5 

6 

7class MindsDBError(Exception): 

8 pass 

9 

10 

11class BaseEntityException(MindsDBError): 

12 """Base exception for entitys errors 

13 

14 Attributes: 

15 message (str): error message 

16 entity_name (str): entity name 

17 """ 

18 

19 def __init__(self, message: str, entity_name: str = None) -> None: 

20 self.message = message 

21 self.entity_name = entity_name or "unknown" 

22 

23 def __str__(self) -> str: 

24 return f"{self.message}: {self.entity_name}" 

25 

26 

27class EntityExistsError(BaseEntityException): 

28 """Raise when entity exists, but should not""" 

29 

30 def __init__(self, message: str = None, entity_name: str = None) -> None: 

31 if message is None: 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true

32 message = "Entity exists error" 

33 super().__init__(message, entity_name) 

34 

35 

36class EntityNotExistsError(BaseEntityException): 

37 """Raise when entity not exists, but should""" 

38 

39 def __init__(self, message: str = None, entity_name: str = None) -> None: 

40 if message is None: 40 ↛ 41line 40 didn't jump to line 41 because the condition on line 40 was never true

41 message = "Entity does not exists error" 

42 super().__init__(message, entity_name) 

43 

44 

45class ParsingError(MindsDBError): 

46 pass 

47 

48 

49class QueryError(MindsDBError): 

50 def __init__( 

51 self, 

52 db_name: str | None = None, 

53 db_type: str | None = None, 

54 db_error_msg: str | None = None, 

55 failed_query: str | None = None, 

56 is_external: bool = True, 

57 is_expected: bool = False, 

58 ) -> None: 

59 self.mysql_error_code = ERR.ER_UNKNOWN_ERROR 

60 self.db_name = db_name 

61 self.db_type = db_type 

62 self.db_error_msg = db_error_msg 

63 self.failed_query = failed_query 

64 self.is_external = is_external 

65 self.is_expected = is_expected 

66 

67 def __str__(self) -> str: 

68 return format_db_error_message( 

69 db_name=self.db_name, 

70 db_type=self.db_type, 

71 db_error_msg=self.db_error_msg, 

72 failed_query=self.failed_query, 

73 is_external=self.is_external, 

74 ) 

75 

76 

77def format_db_error_message( 

78 db_name: str | None = None, 

79 db_type: str | None = None, 

80 db_error_msg: str | None = None, 

81 failed_query: str | None = None, 

82 is_external: bool = True, 

83) -> str: 

84 """Format the error message for the database query. 

85 

86 Args: 

87 db_name (str | None): The name of the database. 

88 db_type (str | None): The type of the database. 

89 db_error_msg (str | None): The error message. 

90 failed_query (str | None): The failed query. 

91 is_external (bool): True if error appeared in external database, False if in internal duckdb 

92 

93 Returns: 

94 str: The formatted error message. 

95 """ 

96 error_message = "Failed to execute external database query during query processing." 

97 if is_external: 

98 error_message = ( 

99 "An error occurred while executing a derived query on the external " 

100 "database during processing of your original SQL query." 

101 ) 

102 else: 

103 error_message = ( 

104 "An error occurred while processing an internally generated query derived from your original SQL statement." 

105 ) 

106 if db_name is not None or db_type is not None: 106 ↛ 113line 106 didn't jump to line 113 because the condition on line 106 was always true

107 error_message += "\n\nDatabase Details:" 

108 if db_name is not None: 

109 error_message += f"\n- Name: {db_name}" 

110 if db_type is not None: 110 ↛ 113line 110 didn't jump to line 113 because the condition on line 110 was always true

111 error_message += f"\n- Type: {db_type}" 

112 

113 if db_error_msg is not None: 113 ↛ 116line 113 didn't jump to line 116 because the condition on line 113 was always true

114 error_message += f"\n\nError:\n{indent(db_error_msg, ' ')}" 

115 

116 if failed_query is not None: 116 ↛ 119line 116 didn't jump to line 119 because the condition on line 116 was always true

117 error_message += f"\n\nFailed Query:\n{indent(failed_query, ' ')}" 

118 

119 return error_message