You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "aminghadersohi (via GitHub)" <gi...@apache.org> on 2026/08/19 14:46:01 UTC
[PR] fix(dao): don't mask transient OperationalError as a "not found" result [superset]
aminghadersohi opened a new pull request, #43335:
URL: https://github.com/apache/superset/pull/43335
## Why
A transient connection-level failure — in production, `psycopg2.OperationalError: SSL connection has been closed unexpectedly` — was being masked by three DAO lookups in `superset/daos/base.py`, so a server-side database blip was reported to users as a client error about a record that does not exist:
- **`find_by_ids`** caught `SQLAlchemyError` and re-raised it as `DAOFindFailedError`, which carries `status = 400`. A real "record doesn't exist" never reaches this handler (`.all()` returns `[]`), so in practice it only fired on genuine execution failures — turning a connection drop into *"AnnotationLayer 5 doesn't exist"* with **HTTP 400**, sending users and support chasing a misconfiguration that does not exist.
- **`find_by_id_or_uuid`** and **`_find_by_column`** catch `StatementError` to absorb *type-coercion* errors (e.g. a non-UUID string) and return `None`. Because `OperationalError` is a subclass of `StatementError` (MRO: `OperationalError → DatabaseError → DBAPIError → StatementError → SQLAlchemyError`), these two also swallowed a transient connection failure and returned `None` — which every caller reads as *"the record does not exist."* This is arguably worse than the `find_by_ids` case: it is a confidently wrong answer with **no error at all**, invisible in logs and error tracking.
All three share one root cause and one remedy, so they are fixed together.
## What
Add a narrow, earlier `except OperationalError: raise` ahead of the existing broad handler at each of the three sites. Connection-level failures now propagate as themselves (surfacing as a 5xx) while the intended behavior is preserved exactly for every other subtype — other `SQLAlchemyError`s still become `DAOFindFailedError`, and genuine coercion `StatementError`s still return `None`.
Order matters: the `OperationalError` clause must precede the broader `StatementError` / `SQLAlchemyError` clause.
**Why `OperationalError` as the boundary?** It covers both mid-query connection drops and initial-connect failures, and is simpler and safer than narrowing on `DBAPIError.connection_invalidated`, which misses initial-connect failures (a connection that was never established cannot be invalidated). Some drivers do raise a few non-connection problems as `OperationalError`; the cost of letting one of those through is a 500 instead of a 400 on an already-failing request — the safer direction, since it fails loudly rather than fabricating a "not found".
## Blast radius
`superset/daos/base.py` only — the shared `BaseDAO` lookup helpers used across the app. Behavior changes solely on the error path: transient DB connection failures now surface as 5xx instead of a misleading 400 / silent `None`. The happy path and all non-connection error handling are unchanged.
## How to test
Regression tests added in `tests/unit_tests/dao/base_dao_test.py`, covering both legs at each affected site:
- `find_by_ids`: an `OperationalError` from `query.all()` **propagates as `OperationalError`**; a non-connection `SQLAlchemyError` still raises `DAOFindFailedError` (existing tests).
- `find_by_id_or_uuid` / `_find_by_column`: an `OperationalError` **propagates**, while a genuine coercion `StatementError` **still returns `None`**.
Each negative test asserts the exception **type**, not merely that something was raised. Reverting the production change makes every propagation test fail (the `OperationalError` is masked as `DAOFindFailedError` or `None` instead).
```
pytest tests/unit_tests/dao/base_dao_test.py
```
## Risk & rollback
Very low. The change is additive (one import + three identical 5-line guards) and only redirects an already-failing request from a misleading 400/`None` to an honest 5xx/propagated error. Rollback is a plain revert. No migration, no feature flag, no schema change.
--
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: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dao): don't mask transient OperationalError as a "not found" result [superset]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #43335:
URL: https://github.com/apache/superset/pull/43335#issuecomment-5343912697
## [Codecov](https://app.codecov.io/gh/apache/superset/pull/43335?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `14.28571%` with `6 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 57.26%. Comparing base ([`097c99b`](https://app.codecov.io/gh/apache/superset/commit/097c99b19cb889061a7423fcc119c0637ce1203a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`4059311`](https://app.codecov.io/gh/apache/superset/commit/405931153cd055630ed21af23bc5e16ab04b30b9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
| [Files with missing lines](https://app.codecov.io/gh/apache/superset/pull/43335?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [superset/daos/base.py](https://app.codecov.io/gh/apache/superset/pull/43335?src=pr&el=tree&filepath=superset%2Fdaos%2Fbase.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvZGFvcy9iYXNlLnB5) | 14.28% | [6 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/43335?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
> :exclamation: There is a different number of reports uploaded between BASE (097c99b) and HEAD (4059311). Click for more details.
>
> <details><summary>HEAD has 100 uploads less than BASE</summary>
>
>| Flag | BASE (097c99b) | HEAD (4059311) |
>|------|------|------|
>|python|78|2|
>|presto|13|1|
>|hive|13|1|
></details>
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #43335 +/- ##
==========================================
- Coverage 66.67% 57.26% -9.41%
==========================================
Files 2876 2876
Lines 164007 164013 +6
Branches 37834 37834
==========================================
- Hits 109347 93928 -15419
- Misses 52514 69192 +16678
+ Partials 2146 893 -1253
```
| [Flag](https://app.codecov.io/gh/apache/superset/pull/43335/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [hive](https://app.codecov.io/gh/apache/superset/pull/43335/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `38.10% <14.28%> (-0.01%)` | :arrow_down: |
| [mysql](https://app.codecov.io/gh/apache/superset/pull/43335/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [postgres](https://app.codecov.io/gh/apache/superset/pull/43335/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [presto](https://app.codecov.io/gh/apache/superset/pull/43335/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `40.04% <14.28%> (-0.01%)` | :arrow_down: |
| [python](https://app.codecov.io/gh/apache/superset/pull/43335/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `40.09% <14.28%> (-19.09%)` | :arrow_down: |
| [sqlite](https://app.codecov.io/gh/apache/superset/pull/43335/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [unit](https://app.codecov.io/gh/apache/superset/pull/43335/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/superset/pull/43335?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
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: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dao): don't mask transient OperationalError as a "not found" result [superset]
Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on code in PR #43335:
URL: https://github.com/apache/superset/pull/43335#discussion_r3815898315
##########
tests/unit_tests/dao/base_dao_test.py:
##########
@@ -260,6 +260,129 @@ def test_find_by_ids_none_id_column():
assert results == []
+def _make_operational_error() -> OperationalError:
+ """Build an OperationalError resembling a transient connection drop."""
+ return OperationalError(
+ "SELECT 1",
+ {},
+ Exception("SSL connection has been closed unexpectedly"),
+ )
+
+
+def test_find_by_ids_operational_error_propagates():
+ """A transient OperationalError from query.all() must propagate as itself,
+ not be masked as a 400 DAOFindFailedError ("record doesn't exist")."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ ):
Review Comment:
This pattern works as written. `unittest.mock.patch("superset.daos.base.getattr")` resolves the original via `getattr(module, "getattr")`, which returns the builtin, and then binds a `getattr` name into the module globals so the function under test picks it up (name resolution: globals before builtins). It is restored on exit. Evidence: the pre-existing tests in this file already patch `superset.daos.base.getattr` the same way and pass, and the `unit-tests-required` job on this PR is green with these new tests included. Isolated repro confirms it too:
```python
import types, sys
m = types.ModuleType("t")
exec("def f(o,n):\n return getattr(o,n) if hasattr(o,n) else None", m.__dict__)
sys.modules["t"] = m
from unittest.mock import patch
with patch("t.getattr") as g, patch("t.hasattr", return_value=True):
g.return_value = "PATCHED"
assert m.f(object(), "x") == "PATCHED" # passes
```
Keeping as-is.
##########
tests/unit_tests/dao/base_dao_test.py:
##########
@@ -260,6 +260,129 @@ def test_find_by_ids_none_id_column():
assert results == []
+def _make_operational_error() -> OperationalError:
+ """Build an OperationalError resembling a transient connection drop."""
+ return OperationalError(
+ "SELECT 1",
+ {},
+ Exception("SSL connection has been closed unexpectedly"),
+ )
+
+
+def test_find_by_ids_operational_error_propagates():
+ """A transient OperationalError from query.all() must propagate as itself,
+ not be masked as a 400 DAOFindFailedError ("record doesn't exist")."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ ):
+ mock_session = Mock()
+ mock_db.session = mock_session
+
+ mock_id_col = Mock()
+ mock_id_col.in_.return_value = Mock()
+ mock_getattr.return_value = mock_id_col
+
+ mock_query = Mock()
+ mock_session.query.return_value = mock_query
+ mock_query.filter.return_value = mock_query
+ mock_query.all.side_effect = _make_operational_error()
+
+ with pytest.raises(OperationalError):
+ TestDAO.find_by_ids([1, 2])
+
+
+def test_find_by_id_or_uuid_operational_error_propagates():
+ """find_by_id_or_uuid catches StatementError to absorb coercion errors;
+ an OperationalError (a StatementError subclass) must still propagate."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ ):
+ mock_session = Mock()
+ mock_db.session = mock_session
+ mock_getattr.return_value = Mock()
+
+ mock_query = Mock()
+ mock_session.query.return_value = mock_query
+ mock_query.filter.return_value = mock_query
+ mock_query.one_or_none.side_effect = _make_operational_error()
+
+ with pytest.raises(OperationalError):
+ TestDAO.find_by_id_or_uuid("1")
+
+
+def test_find_by_id_or_uuid_statement_error_still_returns_none():
+ """A genuine coercion StatementError is still absorbed as None (unchanged)."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ ):
+ mock_session = Mock()
+ mock_db.session = mock_session
+ mock_getattr.return_value = Mock()
+
+ mock_query = Mock()
+ mock_session.query.return_value = mock_query
+ mock_query.filter.return_value = mock_query
+ mock_query.one_or_none.side_effect = StatementError(
+ "invalid input", "SELECT 1", {}, Exception("coercion")
+ )
+
+ assert TestDAO.find_by_id_or_uuid("not-a-uuid") is None
+
+
+def test_find_by_column_operational_error_propagates():
+ """_find_by_column catches StatementError to absorb coercion errors;
+ an OperationalError (a StatementError subclass) must still propagate."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ patch("superset.daos.base.hasattr", return_value=True),
+ patch.object(TestDAO, "_apply_base_filter", side_effect=lambda q, *a, **k: q),
+ patch.object(TestDAO, "_convert_value_for_column", return_value="value"),
+ ):
Review Comment:
Same as the thread on line 279 — patching the builtin name on the module works (mock binds it into module globals, restored on exit), and these tests pass in the green `unit-tests-required` job. Keeping as-is.
##########
superset/daos/base.py:
##########
@@ -255,6 +255,11 @@ def find_by_id_or_uuid(
filter = uuid_column == model_id_or_uuid
try:
return query.filter(filter).one_or_none()
+ except OperationalError:
+ # A transient connection-level failure (e.g. the server dropping the
+ # connection mid-query) surfaces as OperationalError, a StatementError
+ # subclass. Let it propagate instead of masking it as a "not found".
Review Comment:
I looked at extracting a `_raise_if_operational_error(ex)` helper but it reads worse than the inline form: the guard is a two-line `except OperationalError: raise` whose whole value is being explicit and local at each call site, and a helper that conditionally re-raises hides control flow (readers have to trust that a normal-looking call sometimes raises). The comment is duplicated because the three sites are genuinely independent and each is clearer with its rationale next to it. Preferring readability over de-duplication here, so leaving the three explicit guards.
--
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: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dao): don't mask transient OperationalError as a "not found" result [superset]
Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #43335:
URL: https://github.com/apache/superset/pull/43335#issuecomment-5347929857
AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).
<details>
<summary>
Bito Usage Guide
</summary>
**Commands**
Type the following command in the pull request comment and save the comment.
- `/review` - Manually triggers a full AI review.
- `/pause` - Pauses automatic reviews on this pull request.
- `/resume` - Resumes automatic reviews.
- `/resolve` - Marks all Bito-posted review comments as resolved.
- `/abort` - Cancels all in-progress reviews.
Refer to the <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/available-commands" target="_blank" rel="noopener noreferrer">documentation</a> for additional commands.
**Configuration**
This repository uses `Superset` You can customize the agent settings <a href="https://alpha.bito.ai/home/ai-agents/code-review-agent" target="_blank" rel="noopener noreferrer">here</a> or contact your Bito workspace admin at evan@preset.io.
**Documentation & Help**
- <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/getting-started/install-run-using-bito-cloud/create-or-customize-an-agent-instance" target="_blank" rel="noopener noreferrer">Customize agent settings</a>
- <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/implementing-custom-code-review-rules" target="_blank" rel="noopener noreferrer">Review rules</a>
- <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent" target="_blank" rel="noopener noreferrer">General documentation</a>
- <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/faqs" target="_blank" rel="noopener noreferrer">FAQ</a>
</details>
--
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: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dao): don't mask transient OperationalError as a "not found" result [superset]
Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on PR #43335:
URL: https://github.com/apache/superset/pull/43335#issuecomment-5348269327
Thanks @bito-code-review — went through the three suggestions:
**"OperationalError is not a StatementError subclass" (`base.py` L260/L351)** — It is, transitively, and that transitivity is the whole point of the comment:
```python
>>> from sqlalchemy.exc import OperationalError, StatementError
>>> issubclass(OperationalError, StatementError)
True
```
MRO: `OperationalError → DatabaseError → DBAPIError → StatementError → SQLAlchemyError`. `DBAPIError` itself subclasses `StatementError`, so the `except StatementError` on the next line *does* catch `OperationalError` — which is exactly why the earlier `except OperationalError: raise` guard is required. The comment describes the relationship that governs the control flow, so it's accurate as written. Keeping as-is.
**Move `FavStar` import to module level (`dashboards/dao_tests.py` L26)** — These fixtures import Superset models inside the fixture by design; the very next line (`from superset.models.dashboard import Dashboard`) does the same, as does every test's inline DAO import. The inline import keeps model registration ordered relative to the in-memory metadata setup, and hoisting only `FavStar` would be inconsistent with the surrounding file. Keeping as-is.
The logic in all cases was already flagged as correct — these are comment/style notes, so no code change.
--
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: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dao): don't mask transient OperationalError as a "not found" result [superset]
Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #43335:
URL: https://github.com/apache/superset/pull/43335#issuecomment-5343778214
AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).
--
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: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dao): don't mask transient OperationalError as a "not found" result [superset]
Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi closed pull request #43335: fix(dao): don't mask transient OperationalError as a "not found" result
URL: https://github.com/apache/superset/pull/43335
--
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: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dao): don't mask transient OperationalError as a "not found" result [superset]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #43335:
URL: https://github.com/apache/superset/pull/43335#discussion_r3815600338
##########
tests/unit_tests/dao/base_dao_test.py:
##########
@@ -260,6 +260,129 @@ def test_find_by_ids_none_id_column():
assert results == []
+def _make_operational_error() -> OperationalError:
+ """Build an OperationalError resembling a transient connection drop."""
+ return OperationalError(
+ "SELECT 1",
+ {},
+ Exception("SSL connection has been closed unexpectedly"),
+ )
+
+
+def test_find_by_ids_operational_error_propagates():
+ """A transient OperationalError from query.all() must propagate as itself,
+ not be masked as a 400 DAOFindFailedError ("record doesn't exist")."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ ):
Review Comment:
`getattr`/`hasattr` are Python builtins, and `BaseDAO` likely calls them as builtins (not as `superset.daos.base.getattr/hasattr`). Patching `patch("superset.daos.base.getattr")` / `patch("superset.daos.base.hasattr")` will fail unless `superset.daos.base` defines those names. Patch `builtins.getattr` / `builtins.hasattr` instead, or avoid patching them by configuring `TestDAO.model_cls`/attributes such that the real `getattr`/`hasattr` behavior is exercised.
##########
tests/unit_tests/dao/base_dao_test.py:
##########
@@ -260,6 +260,129 @@ def test_find_by_ids_none_id_column():
assert results == []
+def _make_operational_error() -> OperationalError:
+ """Build an OperationalError resembling a transient connection drop."""
+ return OperationalError(
+ "SELECT 1",
+ {},
+ Exception("SSL connection has been closed unexpectedly"),
+ )
+
+
+def test_find_by_ids_operational_error_propagates():
+ """A transient OperationalError from query.all() must propagate as itself,
+ not be masked as a 400 DAOFindFailedError ("record doesn't exist")."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ ):
+ mock_session = Mock()
+ mock_db.session = mock_session
+
+ mock_id_col = Mock()
+ mock_id_col.in_.return_value = Mock()
+ mock_getattr.return_value = mock_id_col
+
+ mock_query = Mock()
+ mock_session.query.return_value = mock_query
+ mock_query.filter.return_value = mock_query
+ mock_query.all.side_effect = _make_operational_error()
+
+ with pytest.raises(OperationalError):
+ TestDAO.find_by_ids([1, 2])
+
+
+def test_find_by_id_or_uuid_operational_error_propagates():
+ """find_by_id_or_uuid catches StatementError to absorb coercion errors;
+ an OperationalError (a StatementError subclass) must still propagate."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ ):
+ mock_session = Mock()
+ mock_db.session = mock_session
+ mock_getattr.return_value = Mock()
+
+ mock_query = Mock()
+ mock_session.query.return_value = mock_query
+ mock_query.filter.return_value = mock_query
+ mock_query.one_or_none.side_effect = _make_operational_error()
+
+ with pytest.raises(OperationalError):
+ TestDAO.find_by_id_or_uuid("1")
+
+
+def test_find_by_id_or_uuid_statement_error_still_returns_none():
+ """A genuine coercion StatementError is still absorbed as None (unchanged)."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ ):
+ mock_session = Mock()
+ mock_db.session = mock_session
+ mock_getattr.return_value = Mock()
+
+ mock_query = Mock()
+ mock_session.query.return_value = mock_query
+ mock_query.filter.return_value = mock_query
+ mock_query.one_or_none.side_effect = StatementError(
+ "invalid input", "SELECT 1", {}, Exception("coercion")
+ )
+
+ assert TestDAO.find_by_id_or_uuid("not-a-uuid") is None
+
+
+def test_find_by_column_operational_error_propagates():
+ """_find_by_column catches StatementError to absorb coercion errors;
+ an OperationalError (a StatementError subclass) must still propagate."""
+
+ with (
+ patch("superset.daos.base.db") as mock_db,
+ patch("superset.daos.base.getattr") as mock_getattr,
+ patch("superset.daos.base.hasattr", return_value=True),
+ patch.object(TestDAO, "_apply_base_filter", side_effect=lambda q, *a, **k: q),
+ patch.object(TestDAO, "_convert_value_for_column", return_value="value"),
+ ):
Review Comment:
`getattr`/`hasattr` are Python builtins, and `BaseDAO` likely calls them as builtins (not as `superset.daos.base.getattr/hasattr`). Patching `patch("superset.daos.base.getattr")` / `patch("superset.daos.base.hasattr")` will fail unless `superset.daos.base` defines those names. Patch `builtins.getattr` / `builtins.hasattr` instead, or avoid patching them by configuring `TestDAO.model_cls`/attributes such that the real `getattr`/`hasattr` behavior is exercised.
##########
superset/daos/base.py:
##########
@@ -255,6 +255,11 @@ def find_by_id_or_uuid(
filter = uuid_column == model_id_or_uuid
try:
return query.filter(filter).one_or_none()
+ except OperationalError:
+ # A transient connection-level failure (e.g. the server dropping the
+ # connection mid-query) surfaces as OperationalError, a StatementError
+ # subclass. Let it propagate instead of masking it as a "not found".
Review Comment:
The same multi-line explanatory comment is duplicated at three catch sites. Consider shortening it (e.g., a single line noting that `OperationalError` should propagate) or extracting a small private helper (e.g., `_raise_if_operational_error(ex)`) to keep the intent while reducing repeated comment blocks that are harder to maintain consistently.
--
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: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [PR] fix(dao): don't mask transient OperationalError as a "not found" result [superset]
Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on PR #43335:
URL: https://github.com/apache/superset/pull/43335#issuecomment-5349718280
cc @mikebridge — you have the most recent substantive work on `BaseDAO`'s lookup helpers in `superset/daos/base.py`, so flagging this for your eyes on the except-clause ordering and the `OperationalError` / `StatementError` MRO argument. (Couldn't add you as a formal reviewer — not a collaborator on this repo.)
--
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: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org