GitHub user dosubot[bot] added a comment to the discussion: Show friendly error when Superset unable to connect to backing database
<!-- Answer --> Yes, you can do this from `superset_config.py` using **`FLASK_APP_MUTATOR`** — though not by directly overriding the engine spec's `custom_errors` patterns (those are hardcoded class attributes and not exposed as a config variable) [[1]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/config.py#L1250). Here's the approach using `FLASK_APP_MUTATOR` to register a custom Flask error handler: ```python # superset_config.py def FLASK_APP_MUTATOR(app): from flask import jsonify from psycopg2 import OperationalError @app.errorhandler(Exception) def handle_db_connection_error(error): # Intercept database connection errors and return a sanitized message error_str = str(error) if "OperationalError" in error_str or "connection to server" in error_str: return jsonify({ "errors": [{ "message": "Unable to connect to the database. Please contact your administrator.", "error_type": "CONNECTION_DATABASE_ERROR", "level": "error", }] }), 502 # Let other errors pass through to default handling raise error ``` However, chart query errors go through a different path (the query execution pipeline) and may not be caught by Flask error handlers at the HTTP level. A more targeted approach is to **monkey-patch the PostgreSQL engine spec's `custom_errors`** from within `FLASK_APP_MUTATOR`: ```python # superset_config.py import re def FLASK_APP_MUTATOR(app): from superset.db_engine_specs.postgres import PostgresEngineSpec from superset.errors import SupersetErrorType # Add/override a catch-all pattern for connection failures CONNECTION_FAILED_REGEX = re.compile( r"connection to server .+ failed" ) PostgresEngineSpec.custom_errors[CONNECTION_FAILED_REGEX] = ( "Unable to connect to the database. Please contact your administrator.", SupersetErrorType.CONNECTION_DATABASE_ERROR, {}, ) ``` This second approach directly extends the engine spec's error patterns at app startup, so when chart queries fail with a connection error, the raw message gets replaced with your friendly text [[2]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/db_engine_specs/base.py#L1252-L1278) [[3]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/db_engine_specs/postgres.py#L121-L176). You can also set `SHOW_STACKTRACE = False` [[4]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/config.py#L284-L287) to ensure stack traces aren't leaked to users. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fsuperset).* --- Share context across your team and agents. Try [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-tagline&utm_term=apache%2Fsuperset). [](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-feedback&utm_term=apache%2Fsuperset&message_id=28ac7918-5bd8-483f-9ec1-d9aeb69e4946) [](https://github.dosu.com/apache/superset?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-learn-repo&utm_term=apache%2Fsuperset) [](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-add-team&utm_term=apache%2Fsuperset) GitHub link: https://github.com/apache/superset/discussions/40881#discussioncomment-17231213 ---- This is an automatically sent email for [email protected]. To unsubscribe, please send an email to: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
