Create structured SQL error metadata from an exception. This is the single source of truth for parsing SQL exceptions into metadata.
(
exception: BaseException,
*,
rule_code: str,
node: ast.expr | None = None,
sql_content: str = "",
context: str = "",
)
| 136 | |
| 137 | |
| 138 | def create_sql_error_metadata( |
| 139 | exception: BaseException, |
| 140 | *, |
| 141 | rule_code: str, |
| 142 | node: ast.expr | None = None, |
| 143 | sql_content: str = "", |
| 144 | context: str = "", |
| 145 | ) -> SQLErrorMetadata: |
| 146 | """Create structured SQL error metadata from an exception. |
| 147 | |
| 148 | This is the single source of truth for parsing SQL exceptions into metadata. |
| 149 | """ |
| 150 | exception_msg = str(exception) |
| 151 | sql_line, sql_col = _extract_sql_position(exception_msg) |
| 152 | |
| 153 | # Truncate long SQL content |
| 154 | truncated_sql = sql_content |
| 155 | if sql_content and len(sql_content) > 200: |
| 156 | truncated_sql = sql_content[:200] + "..." |
| 157 | |
| 158 | # Create clean error message (first line only) |
| 159 | clean_message = exception_msg.split("\n", 1)[0] |
| 160 | |
| 161 | # Extract helpful DuckDB hints separately (including multiline hints) |
| 162 | hint = None |
| 163 | lines = exception_msg.split("\n") |
| 164 | hint_lines = [] |
| 165 | |
| 166 | for line in lines[1:]: |
| 167 | hint_lines.append(line.strip()) |
| 168 | |
| 169 | if hint_lines: |
| 170 | hint = "\n".join(hint_lines) |
| 171 | |
| 172 | return SQLErrorMetadata( |
| 173 | lint_rule=rule_code, |
| 174 | error_type=type(exception).__name__, |
| 175 | clean_message=clean_message, |
| 176 | hint=hint, |
| 177 | node_lineno=node.lineno if node else 0, |
| 178 | node_col_offset=node.col_offset if node else 0, |
| 179 | sql_statement=truncated_sql, |
| 180 | sql_line=sql_line, |
| 181 | sql_col=sql_col, |
| 182 | context=context, |
| 183 | ) |
| 184 | |
| 185 | |
| 186 | def metadata_to_sql_error(metadata: SQLErrorMetadata) -> MarimoSQLError: |
no test coverage detected
searching dependent graphs…