bito-code-review[bot] commented on code in PR #37345: URL: https://github.com/apache/superset/pull/37345#discussion_r2714689202
########## docs/scripts/extract_custom_errors.py: ########## @@ -0,0 +1,296 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +Extract custom_errors from database engine specs for documentation. + +This script parses engine spec files to extract error handling information +that can be displayed on database documentation pages. + +Usage: python scripts/extract_custom_errors.py +Output: JSON mapping of engine spec module names to their custom errors +""" + +import ast +import json # noqa: TID251 - standalone docs script, not part of superset +import sys +from pathlib import Path +from typing import Any + +# Map SupersetErrorType values to human-readable categories and issue codes +ERROR_TYPE_INFO = { + "CONNECTION_INVALID_USERNAME_ERROR": { + "category": "Authentication", + "description": "Invalid username", + "issue_codes": [1012], + }, + "CONNECTION_INVALID_PASSWORD_ERROR": { + "category": "Authentication", + "description": "Invalid password", + "issue_codes": [1013], + }, + "CONNECTION_ACCESS_DENIED_ERROR": { + "category": "Authentication", + "description": "Access denied", + "issue_codes": [1014, 1015], + }, + "CONNECTION_INVALID_HOSTNAME_ERROR": { + "category": "Connection", + "description": "Invalid hostname", + "issue_codes": [1007], + }, + "CONNECTION_PORT_CLOSED_ERROR": { + "category": "Connection", + "description": "Port closed or refused", + "issue_codes": [1008], + }, + "CONNECTION_HOST_DOWN_ERROR": { + "category": "Connection", + "description": "Host unreachable", + "issue_codes": [1009], + }, + "CONNECTION_UNKNOWN_DATABASE_ERROR": { + "category": "Connection", + "description": "Unknown database", + "issue_codes": [1015], + }, + "CONNECTION_DATABASE_PERMISSIONS_ERROR": { + "category": "Permissions", + "description": "Insufficient permissions", + "issue_codes": [1017], + }, + "CONNECTION_MISSING_PARAMETERS_ERROR": { + "category": "Configuration", + "description": "Missing parameters", + "issue_codes": [1018], + }, + "CONNECTION_DATABASE_TIMEOUT": { + "category": "Connection", + "description": "Connection timeout", + "issue_codes": [1001, 1009], + }, + "COLUMN_DOES_NOT_EXIST_ERROR": { + "category": "Query", + "description": "Column not found", + "issue_codes": [1003, 1004], + }, + "TABLE_DOES_NOT_EXIST_ERROR": { + "category": "Query", + "description": "Table not found", + "issue_codes": [1003, 1005], + }, + "SCHEMA_DOES_NOT_EXIST_ERROR": { + "category": "Query", + "description": "Schema not found", + "issue_codes": [1003, 1016], + }, + "SYNTAX_ERROR": { + "category": "Query", + "description": "SQL syntax error", + "issue_codes": [1030], + }, + "OBJECT_DOES_NOT_EXIST_ERROR": { + "category": "Query", + "description": "Object not found", + "issue_codes": [1029], + }, + "GENERIC_DB_ENGINE_ERROR": { + "category": "General", + "description": "Database engine error", + "issue_codes": [1002], + }, +} + + +def extract_string_from_call(node: ast.Call) -> str | None: + """Extract string from __() or _() translation calls.""" + if not node.args: + return None + arg = node.args[0] + if isinstance(arg, ast.Constant) and isinstance(arg.value, str): + return arg.value + elif isinstance(arg, ast.JoinedStr): + # f-string - try to reconstruct + parts = [] + for value in arg.values: + if isinstance(value, ast.Constant): + parts.append(str(value.value)) + elif isinstance(value, ast.FormattedValue): + # Just use a placeholder + parts.append("{...}") + return "".join(parts) + return None + + +def extract_custom_errors_from_file(filepath: Path) -> dict[str, list[dict[str, Any]]]: + """ + Extract custom_errors definitions from a Python engine spec file. + + Returns a dict mapping class names to their custom errors list. + """ + results = {} + + try: + with open(filepath, "r", encoding="utf-8") as f: + source = f.read() + + tree = ast.parse(source) + + for node in ast.walk(tree): + if isinstance(node, ast.ClassDef): + class_name = node.name + + for item in node.body: + # Look for custom_errors = { ... } + if ( + isinstance(item, ast.AnnAssign) + and isinstance(item.target, ast.Name) + and item.target.id == "custom_errors" + and isinstance(item.value, ast.Dict) + ): + errors = extract_errors_from_dict(item.value, source) + if errors: + results[class_name] = errors + + # Also handle simple assignment: custom_errors = { ... } + elif ( + isinstance(item, ast.Assign) + and len(item.targets) == 1 + and isinstance(item.targets[0], ast.Name) + and item.targets[0].id == "custom_errors" + and isinstance(item.value, ast.Dict) + ): + errors = extract_errors_from_dict(item.value, source) + if errors: + results[class_name] = errors + + except Exception as e: + print(f"Error parsing {filepath}: {e}", file=sys.stderr) + + return results Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Avoid catching blind Exception</b></div> <div id="fix"> Replace the broad `Exception` catch with specific exception types (e.g., `OSError`, `SyntaxError`, `ValueError`) to handle only expected errors. </div> <details> <summary> <b>Code suggestion</b> </summary> <blockquote>Check the AI-generated fix before applying</blockquote> <div id="code"> ````suggestion if errors: results[class_name] = errors except (OSError, SyntaxError, ValueError) as e: print(f"Error parsing {filepath}: {e}", file=sys.stderr) return results ```` </div> </details> </div> <small><i>Code Review Run #179764</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
